2016-03-08 83 views
0

我想繪製從我的mapViewGMSMapView)上的點A到點B的路線。在GMSMapView(Google Maps iOS SDK)上繪製路線

我一直試圖做這樣的:

GMSMutablePath *path=[GMSMutablePath path]; 
    [path addCoordinate:self.locationManager.location.coordinate]; 
    [path addCoordinate:marker.position]; 

    GMSPolyline *rectangle=[GMSPolyline polylineWithPath:path]; 
    rectangle.strokeWidth=2.f; 
    rectangle.map=self.mapView; 

,但它只是畫了一條線A點連接到B點,但不是路由本身(通過街道的方向,道路等.. )。

我想在地圖上畫出從步行到達點B的最佳方式。

我該怎麼做?

謝謝!

+0

深入瞭解谷歌地圖API(https://developers.google.com/maps/documentation/directions/intro#DirectionsRequests)你的答案 – Hima

+0

@Hima你可以給我一個代碼示例,我該怎麼做?謝謝! –

+0

他們已經給出了關於如何調用API的查詢方法,請查找「https://maps.googleapis.com/maps/ api/directions/json?origin = sydney,au&destination = perth,au&waypoints = via:-37.81223%2C144.96254%7C-34.92788%2C138.60008&key = YOUR_API_KEY 「in the Waypoints ** topic in the page。 – Hima

回答

0

使用谷歌地圖方向API。使用NSURLConnection或任何其他第三方庫(如AFNetworking等)將起始起始位置和目標位置指向它。使用XML或JSON獲取響應並解析它。

0

{ dispatch_async(dispatch_get_main_queue()^ {

 PlayBackObject *displayobj=[[PlayBackObject alloc]init]; 

     displayobj=[playBackMutArray objectAtIndex:0]; 

     double distanceLatitude = [displayobj.latitudeStr doubleValue]; 
     double distanceLongtitude =[displayobj.longitudeStr doubleValue]; 

     CLLocationDegrees lat = distanceLatitude; 
     CLLocationDegrees lon =distanceLongtitude; 

     GMSCameraPosition* camera = [GMSCameraPosition 
            cameraWithLatitude:lat 
            longitude:lon 
            zoom: 9]; 


     GMSMapView *ListAlertMultimap; 



      ListAlertMultimap =[GMSMapView mapWithFrame: CGRectMake(0, 0, 414,736) camera: camera]; 
      [GmapPlayView addSubview:ListAlertMultimap]; 



      path = [GMSMutablePath path]; 

     for (int i = 0; i < [playBackMutArray count]-1; i++) 

     { 

      double lat = [[[playBackMutArray objectAtIndex:i]valueForKey:@"latitudeStr" ] doubleValue]; 
      double lng = [[[playBackMutArray objectAtIndex:i]valueForKey:@"longitudeStr" ] doubleValue]; 

      marker = [[GMSMarker alloc] init]; 

      marker.position = CLLocationCoordinate2DMake(lat,lng); 

      marker.appearAnimation = kGMSMarkerAnimationPop; 
      NSString *title=[[playBackMutArray objectAtIndex:i]valueForKey:@"trackeridStr"]; 
      NSString *speed=[[playBackMutArray objectAtIndex:i]valueForKey:@"speedStr"]; 
      NSString *direction=[[playBackMutArray objectAtIndex:i]valueForKey:@"directionStr"]; 

      marker.title=[NSString stringWithFormat:@"Tracker ID : %@",title]; 
      marker.snippet=[NSString stringWithFormat:@"Speed : %@km/hr Direction : %@",speed,direction]; 

//   marker.icon = [UIImage imageNamed:@"marker_start"]; 

      [ListAlertMultimap setMinZoom:2 maxZoom:25]; 
      marker.map = ListAlertMultimap; 

      [path addLatitude:lat longitude:lng]; 
     } 
      NSLog(@"Direction path"); 
      polyline = [GMSPolyline polylineWithPath:path]; 
      polyline.strokeColor = [UIColor blueColor]; 
      polyline.strokeWidth = 5.f; 
      polyline.map=ListAlertMultimap; 

    }); 

} 
+0

這爲我工作 –