我已经将我的IOS应用程序与谷歌地图集成。我需要使用谷歌地图绘制从我当前位置到某个(XYZ)目的地的最佳路线路径(减去距离)。而且我还需要这种运输方式(如:驾驶,步行,运输)
有人可以帮我做这个。任何在两点之间绘制线条的框架(最佳路径绘制)并在我点击云端时导航该路径(每2秒更新一次路径)
答案 0 :(得分:0)
第1步
向Google方向URL request
制作API
。
第2步
当您收到JSON file
时,请完成所有步骤。
第3步
解码点对象。
首先获取路线上的所有点坐标,然后在路径中添加这些点(该点的纬度和经度),然后您可以根据该绘制路径...尝试:
GMSCameraPosition *position=[GMSCameraPosition cameraWithLatitude:15.5203 longitude:68.8567 zoom:10];
_mView =[GMSMapView mapWithFrame:CGRectZero camera:position];
_mView.myLocationEnabled=YES;
GMSMarker *marker=[[GMSMarker alloc]init];
marker.position=CLLocationCoordinate2DMake(15.5203, 68.8567);
marker.icon=[UIImage imageNamed:@"a.png"] ;
marker.groundAnchor=CGPointMake(0.5,0.5);
marker.map=_mView;
GMSMutablePath *path = [GMSMutablePath path];
[path addCoordinate:CLLocationCoordinate2DMake(@(15.520).doubleValue,@(68.856).doubleValue)];
[path addCoordinate:CLLocationCoordinate2DMake(@(15.7).doubleValue,@(73.8567).doubleValue)];
GMSPolyline *rect = [GMSPolyline polylineWithPath:path];
rect.strokeWidth = 2.f;
rect.map = _mView;
self.view=_mView;
<强>更新强>
NSString *urlString = [NSString stringWithFormat:
@"%@?origin=%f,%f&destination=%f,%f&sensor=true&key=%@",
@"https://maps.googleapis.com/maps/api/directions/json",
mapView.myLocation.coordinate.latitude,
mapView.myLocation.coordinate.longitude,
destLatitude,
destLongitude,
@"Enter Google API String Here"];
NSURL *directionsURL = [NSURL URLWithString:urlString];
ASIHTTPRequest *req = [ASIHTTPRequest requestWithURL:directionsURL];
[req startSynchronous];
NSError *error = [req error];
if (!error) {
NSString *response = [req responseString];
NSLog(@"%@",response);
NSDictionary *json =[NSJSONSerialization JSONObjectWithData:[req responseData] options:NSJSONReadingMutableContainers error:&error];
GMSPath *path =[GMSPath pathFromEncodedPath:json[@"routes"][0][@"overview_polyline"][@"points"]];
GMSPolyline *singleLine = [GMSPolyline polylineWithPath:path];
singleLine.strokeWidth = 7;
singleLine.strokeColor = [UIColor greenColor];
singleLine.map = self.mapView;
}
else NSLog(@"%@",[req error]);
答案 1 :(得分:0)
希望它可以帮到你
NSString *originString = [NSString stringWithFormat:@"%f,%f", origin.coordinate.latitude, origin.coordinate.longitude];
NSString *destinationString = [NSString stringWithFormat:@"%f,%f", destination.coordinate.latitude, destination.coordinate.longitude];
NSString *directionsAPI = @"https://maps.googleapis.com/maps/api/directions/json?";
NSString *directionsUrlString = [NSString stringWithFormat:@"%@&origin=%@&destination=%@&mode=driving", directionsAPI, originString, destinationString];
NSURL *directionsUrl = [NSURL URLWithString:directionsUrlString];
NSURLSessionDataTask *fetchDirectionsTask = [[NSURLSession sharedSession] dataTaskWithURL:directionsUrl completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error)
{
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if(error)
{
if(completionHandler)
completionHandler(nil);
return;
}
NSArray *routesArray = [json objectForKey:@"routes"];
GMSPolyline *polyline = nil;
if ([routesArray count] > 0)
{
dispatch_async(dispatch_get_main_queue(), ^{
NSDictionary *routeDict = [routesArray objectAtIndex:0];
NSDictionary *routeOverviewPolyline = [routeDict objectForKey:@"overview_polyline"];
NSString *points = [routeOverviewPolyline objectForKey:@"points"];
GMSPolyline *polyline = [GMSPolyline polylineWithPath: [GMSPath pathFromEncodedPath: points]];
polyline.map = _mView;
polyline.strokeColor = [UIColor colorWithRed:0/255.0 green:4/255.0 blue:255/255.0 alpha:1.0];
polyline.strokeWidth = 4.0f;
});
};
if(completionHandler)
completionHandler(polyline);
}];
[fetchDirectionsTask resume];
}