-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. iphoneの地図で現在地から目的地までの方向を表示する方法
- 2. イオンコードバを使用して現在地から目的地までの経路を教えてください。
- 3. イオン2の目的地の経路
- 4. Google Direction Service - 現在地から指定された目的地までの経路
- 5. 現在地をgoogleMapsの目的地にマッピングします
- 6. アンドロイドで現在地と目的地の間の方向を表示するにはどうすればよいですか?
- 7. swiftとMapkitを使用すると、ユーザーの現在地と特定の場所との間の経路を表示します。
- 8. 出発地と目的地の緯度と経度を指定して2つの地点間の道路距離を計算します.netまたはjavascript?
- 9. 現在の場所と目的地が表示されない方向があります
- 10. AndroidスタジオGoogleが現在地を目的地にマップします
- 11. mapkitの方向と所要時間(目的地までの所要時間)
- 12. 現在iphoneのmapviewまたはGoogleマップを使用している目的地への所在地iOS Xcode 4
- 13. 2つのマーカー - MapBoxの現在の位置と目的地Android
- 14. 目的地c自分の現在地を見つけますか?
- 15. (Google map javascript api) - ユーザーの現在地を表示しない
- 16. 目的C地図上の現在の位置ピンとのユーザーのやりとりをブロックする方法
- 17. 目的地点にポイントを取得して経路を描く
- 18. MKPointAnnotationとユーザーの現在地の間のルートを迅速に表示する方法2
- 19. ユーザーの世界地図上の地理的位置を表示
- 20. Azure SQLの現地時間を表示
- 21. 複数のユーザーを表示する方法アンドロイドのGoogleマップの現在地
- 22. ユーザーの所在地が地図上に表示されない
- 23. iOS Swift - 現在の現地時間と日付のタイムスタンプを取得
- 24. チタンマップビューでユーザーの現在地を重視していますか?
- 25. Android:ユーザーの現在地とビーコンとの距離を調べる
- 26. 現在のユーザーをLaravelで経路指定しますか?
- 27. ユーザーがオンになったとき現在地のマーカーを表示
- 28. iOS SDKの現在地のベアリング
- 29. RouteMeコードを使用して現在のユーザーの場所で地図を表示
- 30. iPhone:MapKitと現在の地図尺度を表示
https://stackoverflow.com/questions/22550849/drawing-route- between-two-places-on-gmsmapview-in-ios –