如何使用Google Maps ios SDK跟踪用户的位置并显示行进路径

lai*_*bug 5 google-maps polyline ios google-maps-sdk-ios

我目前正在构建一个ios应用程序,我希望实现一个功能,其中用户的位置显示在Google Map视图上,当他们移动折线显示时,用户到目前为止已经行进的路径.这显然需要实时发生.

到目前为止,我已经初始化了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;
    }
}
Run Code Online (Sandbox Code Playgroud)

这在实践中不会在地图上产生任何折线,所以任何帮助/建议将非常感谢!

raj*_*aji 7

- (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_;
}}
Run Code Online (Sandbox Code Playgroud)