在iOS App中绘制用户当前位置到目的地之间的路线方向? [复制]
Posted
技术标签:
【中文标题】在iOS App中绘制用户当前位置到目的地之间的路线方向? [复制]【英文标题】:Draw route direction between user's current location to destination in iOS App? [duplicate] 【发布时间】:2017-08-11 09:48:01 【问题描述】:我想使用谷歌地图方向 API 绘制从当前位置到目的地的路线方向。
【问题讨论】:
***.com/questions/22550849/… 【参考方案1】:试试这个代码Swift 3.0-
var lat = #your latitude#
var lng = #your longitude#
var location = "your title"
let camera = GMSCameraPosition.camera(withLatitude: lat, longitude: lng, zoom: 9.0)
var mapView:GMSMapView = GMSMapView.map(withFrame: CGRect(origin: CGPoint(x: 0, y: 0), size: view.frame.size), camera: camera)
view.addSubview(mapView)
let markerStart = GMSMarker()
markerStart.position = CLLocationCoordinate2D(latitude: lat, longitude: lng)
markerStart.title = location
markerStart.snippet = location
markerStart.map = mapView
var latEnd = #your end latitude#
var lngEnd = #your end longitude#
let markerEnd = GMSMarker()
markerEnd.position = CLLocationCoordinate2D(latitude: latEnd, longitude: lngEnd)
markerEnd.title = location
markerEnd.snippet = location
markerEnd.map = mapView
let bounds = GMSCoordinateBounds(coordinate: markerStart.position, coordinate: markerEnd.position)
let boundUpdate = GMSCameraUpdate.fit(bounds, withPadding: 40)
mapView.animate(with: boundUpdate)
drawPath(currentLocation: markerStart.position, destinationLoc: markerEnd.position)
func drawPath(currentLocation:CLLocationCoordinate2D,destinationLoc:CLLocationCoordinate2D)
let origin = "\(currentLocation.latitude),\(currentLocation.longitude)"
let destination = "\(destinationLoc.latitude),\(destinationLoc.longitude)"
let url = "https://maps.googleapis.com/maps/api/directions/json?origin=\(origin)&destination=\(destination)&mode=driving"
Alamofire.request(url).responseJSON response in
let json = JSON(data: response.data!)
let routes = json["routes"].arrayValue
for route in routes
let routeOverviewPolyline = route["overview_polyline"].dictionary
let points = routeOverviewPolyline?["points"]?.stringValue
let path = GMSPath.init(fromEncodedPath: points!)
let polyline = GMSPolyline.init(path: path)
polyline.strokeColor = UIColor.red
polyline.strokeWidth = 3
polyline.map = self.mapView
目标 C
double lat = #your latitude#;
double lng = #your longitude#;
double latEnd = #your end latitude#;
double lngEnd = #your end longitude#;
NSString *directionsUrlString = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/directions/json?&origin=%f,%f&destination=%f,%f&mode=driving", lat, lng, latEnd, lngEnd];
NSURL *directionsUrl = [NSURL URLWithString:directionsUrlString];
NSURLSessionDataTask *mapTask = [[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)
NSDictionary *routeDict = [routesArray objectAtIndex:0];
NSDictionary *routeOverviewPolyline = [routeDict objectForKey:@"overview_polyline"];
NSString *points = [routeOverviewPolyline objectForKey:@"points"];
GMSPath *path = [GMSPath pathFromEncodedPath:points];
polyline = [GMSPolyline polylineWithPath:path];
// run completionHandler on main thread
dispatch_sync(dispatch_get_main_queue(), ^
if(completionHandler)
completionHandler(polyline);
);
];
[mapTask resume];
【讨论】:
你能给我同样的Objective c代码吗? @Abhishek以上是关于在iOS App中绘制用户当前位置到目的地之间的路线方向? [复制]的主要内容,如果未能解决你的问题,请参考以下文章
Android- Google Maps V2:跟踪从当前位置到另一个目的地的路线