2013-08-24 61 views
5

我目前正在構建一個iOS應用程序,我希望實現一個功能,用戶的位置顯示在Google Map視圖上,以及它們何時移動一個多段線節目是用戶到目前爲止所走過的路徑。這顯然需要實時發生。如何跟蹤用戶的位置並顯示使用谷歌地圖移動的路徑ios SDK

到目前爲止,我已經初始化了Google Maps視圖,並且可以使用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 Array。 – ChenSmile

2

如果要繪製兩條路徑,則使用此方法。其演示swift方法來查找兩個地方的路線。

//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 
     } 

    } 
} 
相關問題