我想知道如何借助google place API在两个地方之间绘制地图方向。
请提供帮助如何操作。
答案 0 :(得分:2)
要创建两个位置的地图方向,首先需要相应的2个纬度和经度,然后需要调用calculateRoutesFrom
。在这里你可以将纬度和经度传递给两个地方......
-(NSArray*) calculateRoutesFrom:(CLLocationCoordinate2D) f to: (CLLocationCoordinate2D) t {
NSString* saddr = [NSString stringWithFormat:@"%f,%f", f.latitude, f.longitude];
NSString* daddr = [NSString stringWithFormat:@"%f,%f", t.latitude, t.longitude];
NSString* apiUrlStr = [NSString stringWithFormat:@"http://maps.google.com/maps?output=dragdir&saddr=%@&daddr=%@", saddr, daddr];
NSURL* apiUrl = [NSURL URLWithString:apiUrlStr];
NSLog(@"api url: %@", apiUrl);
NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl encoding:NSStringEncodingConversionAllowLossy error:Nil];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"points:\\\"([^\\\"]*)\\\"" options:0 error:NULL];
NSTextCheckingResult *match = [regex firstMatchInString:apiResponse options:0 range:NSMakeRange(0, [apiResponse length])];
NSString *encodedPoints = [apiResponse substringWithRange:[match rangeAtIndex:1]];
//NSString* encodedPoints = [apiResponse stringByMatching:@"points:\\\"([^\\\"]*)\\\"" capture:1L];
return [self decodePolyLine:[encodedPoints mutableCopy]];
}
通过使用-(NSMutableArray *)decodePolyLine: (NSMutableString *)encoded
,您可以获得给定的两个纬度和经度之间的几个纬度和经度。
-(NSMutableArray *)decodePolyLine: (NSMutableString *)encoded {
[encoded replaceOccurrencesOfString:@"\\\\" withString:@"\\"
options:NSLiteralSearch
range:NSMakeRange(0, [encoded length])];
NSInteger len = [encoded length];
NSInteger index = 0;
NSMutableArray *array = [[NSMutableArray alloc] init] ;
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 *latitude = [[NSNumber alloc] initWithFloat:lat * 1e-5] ;
NSNumber *longitude = [[NSNumber alloc] initWithFloat:lng * 1e-5] ;
Place * pp = [[Place alloc] init];
pp.name = @"place name";
pp.latitude = [latitude doubleValue];
pp.longitude = [longitude doubleValue];
PlaceMark* from = [[PlaceMark alloc] initWithPlace:pp] ;
[annotations addObject:from];
printf("[%f,", [latitude doubleValue]);
printf("%f]", [longitude doubleValue]);
CLLocation *loc = [[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]] ;
[array addObject:loc];
}
return array;
}
将从此方法返回纬度和经度数组。
答案 1 :(得分:0)
只需4个简单步骤即可添加折线
代码片段如上所述
GMSMutablePath *RoutePath = [GMSMutablePath path];
[RoutePath addCoordinate:CLLocationCoordinate2DMake(-46.85, 129.10)];
[RoutePath addCoordinate:CLLocationCoordinate2DMake(-46.70, 129.30)];
GMSPolyline *polyline = [GMSPolyline polylineWithPath:RoutePath];
polyline.map = YourMapViewObject;
使用Google地图删除和添加Ios中的折线时,Google自己提供了一个良好的文档,其中包含以下链接
how-to-draw-a-map-direction-between-two-places-by-google-place-api
希望有所帮助
如果您在IOS中使用GoogleMap Sdk
,则上述内容可行如果你想在IOs Map(MKMapView)上绘制折线,那么转到这个示例项目......