我已經將我的IOS應用程序與谷歌地圖集成在一起。我需要使用谷歌地圖從當前位置到某個(XYZ)目的地繪製最佳路線路徑(較少距離)。我還需要有這種運輸模式(如:駕駛,步行,運輸)IOS:使用谷歌地圖繪製最佳路線路徑
有人可以幫我做到這一點。任何框架得出兩點間線(最佳路徑平局),當我(在每2秒更新路徑)的驅動器點擊
我已經將我的IOS應用程序與谷歌地圖集成在一起。我需要使用谷歌地圖從當前位置到某個(XYZ)目的地繪製最佳路線路徑(較少距離)。我還需要有這種運輸模式(如:駕駛,步行,運輸)IOS:使用谷歌地圖繪製最佳路線路徑
有人可以幫我做到這一點。任何框架得出兩點間線(最佳路徑平局),當我(在每2秒更新路徑)的驅動器點擊
第1步
做一個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]);
希望它幫助你
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];
}
入住這https://developers.google.com/maps/documentation/directions/ – kb920