-1
A
回答
1
尝试此代码夫特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];
}
相关问题
- 1. 如何绘制从当前位置到目的地的路线方向
- 2. 在android中当前位置到另一个位置之间绘制路线?
- 3. 如何在地图视图中绘制从当前位置到目的位置的路线xamarin iOS
- 4. 在iOS中的GMSMapView无限位置之间绘制路线
- 5. 如何绘制当前位置和另一个位置之间的路线
- 6. 如何在Mapkit Swift 3中绘制当前位置和目的地之间的路线?
- 7. 使用谷歌API从我的位置到目的地位置绘制路线
- 8. 使用基于当前位置的MapKit在地图上绘制路线
- 9. 如何绘制从我的位置到目的地的路线?
- 10. 使用GPS提供商在地图上使用当前位置绘制路线
- 11. 如何在iPhone中的两个位置之间绘制路线?
- 12. 无法在Android中的两个位置之间绘制路线
- 13. 在HTML中的地图上的两个位置之间绘制路线
- 14. 机器人:如何绘制航线方向谷歌地图API V2从当前位置到目的地
- 15. 如何在我的当前位置与MKMapView上的所需位置之间绘制路线?
- 16. 如何分配当前地理坐标以绘制从当前位置到目标位置的路径?
- 17. Google定向服务 - 从当前位置到指定目的地的路线
- 18. 如何在CloudMade上的两个位置之间绘制路线
- 19. 如何在iOS应用程序中使用Bing Map API在两个位置之间绘制路线?
- 20. 使用当前位置显示使用在应用程序MapView的目的地的方向ios 6
- 21. 谷歌地图/ gmap3 - 绘制从用户的地理位置到已知目的地的路线 - 需要帮助
- 22. 如何绘制从当前位置到地图片段上用户输入位置的路径?
- 23. 当前位置与搜索地点之间的公路/路线图
- 24. 在Xcode的Google MapKit上绘制用户位置和指定位置之间的路线
- 25. 谷歌从当前位置到已知位置的方向路线
- 26. 如何在Google地图上显示用户输入的两个位置并绘制它们之间的路线
- 27. 使用Google方向API在地图控件中绘制路线?
- 28. 用户位置和目标之间的错误启动方向
- 29. 获取当前位置和绘制路径的谷歌地图
- 30. osmdroid绘制当前位置离线
https://stackoverflow.com/questions/22550849/drawing-route- between-two-places-on-gmsmapview-in-ios –