- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我需要在谷歌地图 View 中找到一个地方到另一个地方的路径。如何使用 google map sdk iOS 绘制方向图。
谁能给出源代码。然后解释如何实现这一目标。
下面我还附上了图片,我需要使用 Google Map SDK iOS 在 iPhone 应用程序中实现这一点。
谢谢,
最佳答案
-(void)showRoute:(id)routeDict {
NSString *pointStr = [[[[routeDict objectForKey:@"routes"] objectAtIndex:0] objectForKey:@"overview_polyline"] objectForKey:@"points"];
float max_long = 0.0;
float min_long = 0.0;
float max_lat = 0.0;
float min_lat = 0.0;
NSMutableArray *routeArr = [self decodePolyLine:pointStr];
CLLocationCoordinate2D commuterLotCoords[[routeArr count]];
CLLocation *loc;
if ([routeArr count]) {
loc = [routeArr objectAtIndex:0];
max_long = loc.coordinate.longitude;
min_long = loc.coordinate.longitude;
max_lat = loc.coordinate.latitude;
min_lat = loc.coordinate.latitude;
}
for (int i=0; i<[routeArr count]; i++) {
CLLocation *loc = [routeArr objectAtIndex:i];
commuterLotCoords[i] = loc.coordinate;
if (loc.coordinate.latitude > max_lat) {
max_lat = loc.coordinate.latitude;
}
if (loc.coordinate.latitude < min_lat) {
min_lat = loc.coordinate.latitude;
}
if (loc.coordinate.longitude > max_long) {
max_long = loc.coordinate.longitude;
}
if (loc.coordinate.longitude < min_long) {
min_long = loc.coordinate.longitude;
}
}
MKPolyline *overflowRoutePolygon = [MKPolyline polylineWithCoordinates:commuterLotCoords count:[routeArr count]];
[mapView addOverlay:overflowRoutePolygon];
//NSLog(@"%f %f %f %f",min_lat,max_lat,min_long,max_long);
if ( max_lat == 0.0 || min_lat == 0.0 || max_long == 0.0 || min_long == 0.0 ) {
} else {
//calculate center of map
float center_long = (max_long + min_long) / 2;
float center_lat = (max_lat + min_lat) / 2;
//calculate deltas
float deltaLat = max_lat - min_lat + .00032;
float deltaLong = max_long - min_long + .00032;
//NSLog(@"%f %f %f %f",center_lat,center_long,deltaLat,deltaLong);
//create new region and set map
CLLocationCoordinate2D cordinate;
cordinate.latitude = center_lat;
cordinate.longitude = center_long;
MKCoordinateSpan span = MKCoordinateSpanMake(deltaLat, deltaLong);
MKCoordinateRegion region = {cordinate, span};
[mapView setRegion:region];
}
}
-(void)showRoute
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
float sourceLat = 0.00;
float sourceLong = 0.00;
float destinationLat = 0.00;
float destinationLong = 0.00;
id record = [routeArray objectAtIndex:selectedIndex];
sourceLat = [[[[record objectForKey:@"To"] objectForKey:@"Latitude"] objectForKey:@"text"] floatValue];
sourceLong = [[[[record objectForKey:@"To"] objectForKey:@"Longitude"] objectForKey:@"text"] floatValue];
destinationLat = [[[[record objectForKey:@"From"] objectForKey:@"Latitude"] objectForKey:@"text"] floatValue];
destinationLong = [[[[record objectForKey:@"From"] objectForKey:@"Longitude"] objectForKey:@"text"] floatValue];
if ( sourceLat == 0.00 || sourceLong == 0.00 || destinationLat == 0.00 || destinationLong == 0.00 ) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Direction Error"
message: @"One of your destination is not valid."
delegate: self
cancelButtonTitle: @"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
return;
}
NSString *urlStr = [NSString stringWithFormat: @"http://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination=%f,%f&sensor=false",
sourceLat, sourceLong,
destinationLat,destinationLong];
NSLog(@"urlStr : %@",urlStr);
NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:urlStr]];
//NSLog(@"direction response : %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
JSONDecoder *jsonKitDecoder = [JSONDecoder decoder];
NSMutableDictionary *routeDic = [[jsonKitDecoder objectWithData:data] copy];
if ( ![[[routeDic objectForKey:@"status"] uppercaseString] isEqualToString:@"OK"] ) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Direction Error"
message: @"Not able to find direction from your default location."
delegate: self
cancelButtonTitle: @"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
[pool release];
return;
}
[self performSelectorOnMainThread:@selector(showRoute:) withObject:routeDic waitUntilDone:YES];
[pool release];
return;
}
-(NSMutableArray *)decodePolyLine:(NSString *)encodedStr
{
NSMutableString *encoded = [[NSMutableString alloc] initWithCapacity:[encodedStr length]];
[encoded appendString:encodedStr];
[encoded replaceOccurrencesOfString:@"\\\\" withString:@"\\" options:NSLiteralSearch range:NSMakeRange(0, [encoded length])];
NSInteger len = [encoded length];
NSInteger index = 0;
NSMutableArray *array = [[[NSMutableArray alloc] init] autorelease];
NSInteger lat = 0;
NSInteger lng = 0;
while (index < len) {
NSInteger b;
NSInteger shift = 0;
NSInteger result = 0;
do {
b = [encoded characterAtIndex:index++] - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
NSInteger dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = [encoded characterAtIndex:index++] - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
NSInteger dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
lng += dlng;
NSNumber *latitute = [[[NSNumber alloc] initWithFloat:lat * 1e-5] autorelease];
NSNumber *longitute = [[[NSNumber alloc] initWithFloat:lng * 1e-5] autorelease];
CLLocation *loc = [[[CLLocation alloc] initWithLatitude:[latitute floatValue] longitude:[longitute floatValue]] autorelease];
[array addObject:loc];
}
[encoded release];
return array;
}
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
HAnnotation *ann = (HAnnotation *)annotation;
static NSString *AnnotationViewID = @"annotationViewID";
HAnnotationView *annotationView = (HAnnotationView *)[map dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
if (annotationView == nil)
{
annotationView = [[[HAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID type:ann.type] autorelease];
}
annotationView.type = ann.type;
annotationView.annotation = annotation;
[annotationView setNeedsDisplay];
return annotationView;
}
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
MKAnnotationView *aV;
for (aV in views) {
MKAnnotationView* annotationView = aV;
annotationView.canShowCallout = NO;
}
}
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id < MKOverlay >)overlay
{
if( [overlay isKindOfClass:[MKPolyline class]] ) {
MKPolylineView *view = [[MKPolylineView alloc] initWithOverlay:overlay];
view.lineWidth=2;
view.strokeColor=[UIColor blueColor];
view.fillColor=[[UIColor blueColor] colorWithAlphaComponent:0.5];
return [view autorelease];
} else {
MKPolygonView *view = [[MKPolygonView alloc] initWithOverlay:overlay];
view.lineWidth=2;
view.strokeColor=[UIColor yellowColor];
view.fillColor=[[UIColor yellowColor] colorWithAlphaComponent:0.3];
return [view autorelease];
}
return nil;
}
试试这个代码 我在我的项目中使用了这个代码,它工作正常。首先,在 google api 中传递源和目的地的纬度和经度,您将获得该地点之间的所有点,并在使用折线法解析后绘制线。只需使用此方法根据您的变量和数据进行更改。
关于objective-c - 如何在 google map sdk iOS 中绘制一个地方到另一个地方的路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16811965/
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
操作无法完成。 Places API 库中发生内部错误。如果您认为此错误代表错误,请使用我们社区和支持页面 (https://developers.google.com/places/support)
我正在尝试在我的项目中使用 google places,我将其设置在 fragment 中而不是 Activity 中,我的自动完成 fragment 在 fragment 中。但是,当我尝试搜索它时
我的目的是使用R来查询google api。 我有一个地址和名称列表(属于商店、餐馆等),我需要为每个地址和名称存储: “纬度”、“经度”、“业务类型” 我的想法是使用 google place ap
我正在寻找设置一个自动完成的谷歌地方小部件。 我有一个带有“searchFieldText”id 的输入类型文本。 这是我的 JS 代码: var inputsec = document.getEle
是否可以使用图形 API(或地址/ zip )按纬度/经度和半径获取地点?我在文档中的任何地方都看不到它 最佳答案 搜索 URL 的以下格式将返回某个位置附近的地点列表: https://graph.
我正在探索 Google API,主要是 Places API。由于对 Google Places API 的请求数限制为 100,000,因此我正在寻找方法来最大限度地减少发送到 API 的请求数。
伙计们,我在我的应用程序中有一个功能,可以使用 GetFiles 在特定目录中搜索特定文件。方法 System.IO.Directory.GetFiles(string path, string
我已经在 Laravel 5.3 上使用 where 查询成功创建了许多函数,但是这次发生了一些奇怪的事情。 public function show($id){ $artikel = Art
我正在为我的 iPhone 应用程序使用 Facebook 图形 API 来获取附近地点的列表,我使用带有一些参数的“搜索”请求。我得到的响应是一个包含以下信息的地点列表:“纬度”、“经度”、“名称”
我有一个 Android 应用程序,我在其中使用 Google map 显示附近的地方,如加油站、药店等。我正在使用 map 和地点 API。 https://maps.googleapis.com/
我是一名优秀的程序员,十分优秀!