gpt4 book ai didi

ios - Google Map API 给出了两点之间方向的错误坐标

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:58:21 26 4
gpt4 key购买 nike

我正在使用 GoogleMap API 显示 GoogleMap 上两点之间的绘制方向。

我想在 iOS 6.1 上提供支持,所以我使用 GoogleMap 我知道 iOS7 可以恢复它。

使用以下代码解析并获取坐标在 map 上绘制折线的步骤:

NSString *str=@"http://maps.googleapis.com/maps/api/directions/json?origin=bharuch,gujarat&destination=vadodara,gujarat&sensor=false";

NSURL *url=[[NSURL alloc]initWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSError* error;

NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

NSArray* latestRoutes = [json objectForKey:@"routes"];

NSMutableDictionary *legs=[[[latestRoutes objectAtIndex:0] objectForKey:@"legs"] objectAtIndex:0];

NSArray *steps=[legs objectForKey:@"steps"];

NSString *startLocation,*endLocation,*totalDistance,*totalDuration;
CLLocationCoordinate2D startLoc,endLoc;
startLocation = [legs objectForKey:@"start_address"];
endLocation = [legs objectForKey:@"end_address"];
totalDistance = [[legs objectForKey:@"distance"] objectForKey:@"text"];
totalDuration = [[legs objectForKey:@"duration"] objectForKey:@"text"];
startLoc=CLLocationCoordinate2DMake([[[legs objectForKey:@"start_location"] objectForKey:@"lat"] doubleValue], [[[legs objectForKey:@"start_location"] objectForKey:@"lng"] doubleValue]);
endLoc=CLLocationCoordinate2DMake([[[legs objectForKey:@"end_location"] objectForKey:@"lat"] doubleValue], [[[legs objectForKey:@"end_location"] objectForKey:@"lng"] doubleValue]);

NSMutableDictionary *tempDict;
if ([steps count]!=0) {
GMSMutablePath *path = [GMSMutablePath path];
for(int idx = 0; idx < [steps count]+2; idx++){

CLLocationCoordinate2D workingCoordinate;

if (idx==0) {

workingCoordinate=startLoc;

[path addCoordinate:workingCoordinate];

}
else if (idx==[steps count]+1){

workingCoordinate=endLoc;

[path addCoordinate:workingCoordinate];
}
else{

workingCoordinate=CLLocationCoordinate2DMake([[[[steps objectAtIndex:idx-1] objectForKey:@"start_location"] objectForKey:@"lat"] floatValue], [[[[steps objectAtIndex:idx-1] objectForKey:@"start_location"] objectForKey:@"lng"] floatValue]);

[path addCoordinate:workingCoordinate];

}
tempDict = nil;
}
// create the polyline based on the array of points.

GMSPolyline *rectangle = [GMSPolyline polylineWithPath:path];

rectangle.strokeWidth=5.0;

rectangle.map = mapView_;
}

它只给出了 24 个步骤,意味着只有 24 个坐标用于在 map 上创建点和绘制线,如下图所示:

after drawing line on map and zoom to few level

您可以看到那条线路不在正确的道路上,那么我该怎么做才能解决这个问题?我也想在 map 上显示方向。

最佳答案

以下答案是在@A Báo 的帮助下实现两个位置之间的平滑路径:

-(void)viewDidLoad {

// Create a GMSCameraPosition that tells the map to display the

GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:21.718472 longitude:73.030422 zoom:6];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.delegate=self;
mapView_.myLocationEnabled = YES;
mapView_.settings.myLocationButton=YES;
mapView_.settings.indoorPicker=NO;
mapView_.settings.compassButton=YES;

self.view = mapView_;

NSString *str=@"http://maps.googleapis.com/maps/api/directions/json?origin=Bharuch,gujarat&destination=vadodara,gujarat&sensor=false";

NSURL *url=[[NSURL alloc]initWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

NSArray* latestRoutes = [json objectForKey:@"routes"];

NSString *points=[[[latestRoutes objectAtIndex:0] objectForKey:@"overview_polyline"] objectForKey:@"points"];

@try {
// TODO: better parsing. Regular expression?

NSArray *temp= [self decodePolyLine:[points mutableCopy]];

GMSMutablePath *path = [GMSMutablePath path];

for(int idx = 0; idx < [temp count]; idx++)
{
CLLocation *location=[temp objectAtIndex:idx];

[path addCoordinate:location.coordinate];

}
// create the polyline based on the array of points.

GMSPolyline *rectangle = [GMSPolyline polylineWithPath:path];

rectangle.strokeWidth=5.0;

rectangle.map = mapView_;

}
@catch (NSException * e) {
// TODO: show error
}
}

-(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] ;
printf("[%f,", [latitude doubleValue]);
printf("%f]", [longitude doubleValue]);
CLLocation *loc = [[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]] ;
[array addObject:loc];
}

return array;
}

此代码的屏幕截图,您可以将其与我在提问时使用的之前的屏幕截图进行比较: enter image description here

关于ios - Google Map API 给出了两点之间方向的错误坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22091271/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com