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

标签 ios google-maps google-maps-sdk-ios polyline

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

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

我想最好的方法是创建一个 GMSMutablePath 对象,每次 map 相机更新到新位置时都会添加用户的当前位置,然后从该路径创建折线。我为此使用的代码如下:

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

这实际上不会在 map 上产生任何折线,因此非常感谢任何帮助/建议!

最佳答案

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

关于ios - 如何使用 Google Maps ios SDK 跟踪用户的位置并显示行进的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18422826/

相关文章:

ios - 字符串中的特定颜色

ios - 如何让SKSpriteNode不旋转纹理

javascript - 谷歌地图API加载灰色 map 而不是 map 图像

javascript - 在谷歌地图中使用 ngClass 与 map 缩放事件没有绑定(bind)

ios - 在 Google Cloud Platform 中为 iOS 启用 Places API

ios - GMSMapView 不显示任何内容

ios - Firebase:它如何知道我的 OAuth token 与哪个帐户相关联?

ios - 在不设置自动旋转的情况下强制进入横向模式

javascript - 通过单击按钮和带有 jQ​​uery 的单独 JS 文件,在 Google map 标记上触发单击事件

ios - 找不到 GMSMarker 的协议(protocol)声明