2013-08-24 2 views
5

현재 iOS 앱을 만들고 있는데 Google지도보기에 사용자의 위치가 표시되는 기능을 구현하고 싶습니다. 지금까지 사용자가 이동 한 폴리선 쇼를 이동합니다. 이것은 분명히 실시간으로 발생해야합니다.사용자 위치를 추적하고 Google Maps iOS SDK를 사용하여 이동 한 경로를 표시하는 방법

지금까지 Google지도보기를 초기화했으며 observeValueForKeyPath 기능을 사용하여 사용자의 현재 위치를 표시 할 수 있습니다. 사용자가 이동할 때보기 및 위치 표식이 업데이트됩니다.

이렇게하는 가장 좋은 방법은지도 카메라가 새 위치로 업데이트 될 때마다 사용자의 현재 위치를 추가 한 다음 해당 경로에서 폴리 라인을 만드는 GMSMutablePath 개체를 만드는 것입니다. 나는 이것을 위해 사용했던 코드는 다음과 같습니다 :

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 
{ 
    if ([keyPath isEqualToString:@"myLocation"] && [object isKindOfClass:[GMSMapView class]]) 
    { 
     [self.myMapView animateToCameraPosition:[GMSCameraPosition cameraWithLatitude:self.myMapView.myLocation.coordinate.latitude 
                       longitude:self.myMapView.myLocation.coordinate.longitude 
                         zoom:18]]; 


     [trackedPath addCoordinate:CLLocationCoordinate2DMake(self.myMapView.myLocation.coordinate.latitude, self.myMapView.myLocation.coordinate.longitude)]; 
     GMSPolyline *testPoly = [GMSPolyline polylineWithPath:trackedPath]; 
     testPoly.strokeWidth = 8; 
     testPoly.strokeColor = [UIColor redColor]; 
     testPoly.map = myMapView; 
    } 
} 

이 실제로 그래서 어떤 도움/조언을 많이 주시면 감사하겠습니다지도상의 폴리 라인을 생성하지 않습니다!

답변

6
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{ NSString *pointString=[NSString stringWithFormat:@"%f,%f",newLocation.coordinate.latitude,newLocation.coordinate.longitude]; 
    [self.points addObject:pointString]; 
GMSMutablePath *path = [GMSMutablePath path]; 
for (int i=0; i<self.points.count; i++) 
{   
    NSArray *latlongArray = [[self.points objectAtIndex:i]componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]]; 

[path addLatitude:[[latlongArray objectAtIndex:0] doubleValue] longitude:[[latlongArray objectAtIndex:1] doubleValue]]; 
} 

if (self.points.count>2) 
{ 
    GMSPolyline *polyline = [GMSPolyline polylineWithPath:path]; 
    polyline.strokeColor = [UIColor blueColor]; 
    polyline.strokeWidth = 5.f; 
    polyline.map = mapView_; 
    self.view = mapView_; 
}} 
+0

코드의 내용은 무엇입니까? –

+0

@sandeeptomar point는 NSMutable 배열입니다. – ChenSmile

2

두 경로 BTW 두 곳을 그리려면이 방법을 사용하십시오. 그것의 민주당 원 빠른 방법은 2 개의 장소의 대강을 찾아 낸다.

//MARK: API CALL 
func GetRoughtofTwoLocation(){ 
    let originString: String = "\(23.5800),\(72.5853)" 
    let destinationString: String = "\(24.5836),\(72.5853)" 
    let directionsAPI: String = "https://maps.googleapis.com/maps/api/directions/json?" 
    let directionsUrlString: String = "\(directionsAPI)&origin=\(originString)&destination=\(destinationString)&mode=driving" 

    APICall().callApiUsingWithFixURLGET(directionsUrlString, withLoader: true) { (responceData) -> Void in 
     let json = responceData as! NSDictionary 
     let routesArray: [AnyObject] = (json["routes"] as! [AnyObject]) 
     var polyline: GMSPolyline? = nil 
     if routesArray.count > 0 { 
      let routeDict: [NSObject : AnyObject] = routesArray[0] as! [NSObject : AnyObject] 
      var routeOverviewPolyline: [NSObject : AnyObject] = (routeDict["overview_polyline"] as! [NSObject : AnyObject]) 
      let points: String = (routeOverviewPolyline["points"] as! String) 
      let path: GMSPath = GMSPath(fromEncodedPath: points)! 
      polyline = GMSPolyline(path: path) 
      polyline!.strokeWidth = 2.0 
      polyline!.map = self.mapView 
     } 

    } 
}