gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 04:23:45 36 4
gpt4 key购买 nike

我目前正在构建一个 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/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com