2017-08-11 11 views

答えて

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]; 
} 
+0

samのObjective cコードを教えてもらえますか? e。 @Abhishek – Mukesh

+0

はい、私はあなたの答えをupvoted。あなたも私の質問をupvote。 – Mukesh

関連する問題