如何使用Google Maps ios SDK跟踪用户的位置并显示path

我目前正在构build一个iOS应用程序,我希望实现一个function,用户的位置显示在Google Map视图上,当他们移动一个折线显示的path已经由用户到目前为止。 这显然需要实时发生。

到目前为止,我已经初始化一个Google Maps视图,并且可以使用observeValueForKeyPath函数显示用户的当前位置。 视图和位置标记随用户移动而更新。

我想最好的方法是创build一个GMSMutablePath对象,每当地图摄像机更新到新位置时添加用户的当前位置,然后从该path创build多段线。 我用这个代码如下:

- (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; } } 

这不会在地图上产生任何折线,所以任何帮助/build议将非常感激!

 - (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_; }} 

如果要绘制两条path,则使用此方法。 其演示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 } } }