gpt4 book ai didi

ios - 如何在 iOS 的谷歌地图上使用 CLLocationManager 生成我的旅行路径?

转载 作者:行者123 更新时间:2023-11-28 19:46:50 25 4
gpt4 key购买 nike

用户必须在生成路线的任何地方一步一步地在谷歌地图上创建自己的路径。

请看我的代码片段:

- (void)locationManager:(CLLocationManager )manager didUpdateLocations:(NSArray )locations
{

CLLocation* location1 = [locations lastObject];

if (self.endPointArray == nil)
{
self.endPointArray = [[NSMutableArray alloc]init];
}
NSString *pointString=[NSString stringWithFormat:@"%f,%f",location1.coordinate.latitude,location1.coordinate.longitude];
[self.endPointArray addObject:pointString];
NSLog(@"end point array :%@",self.endPointArray);
GMSMutablePath *path = [GMSMutablePath path];
for (int i=0; i<self.endPointArray.count; i++)
{
NSArray *latlongArray = [[self.endPointArray objectAtIndex:i]componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]];

[path addLatitude:[[latlongArray objectAtIndex:0] doubleValue] longitude:[[latlongArray objectAtIndex:1] doubleValue]];
}

if (self.endPointArray.count>2)
{
GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
polyline.strokeColor = [UIColor redColor];
polyline.strokeWidth = 3.0f;
polyline.map = _mapView;
}
}

我已经在上述代码的帮助下创建了路线。但有时我们会从 CLLocation 获得意外的坐标。如果我们在路上(路线)它会给出完美的但如果我们不像在家里那样在路上它会给出错误的坐标

最佳答案

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *location = [locations lastObject];
NSTimeInterval age = -[location.timestamp timeIntervalSinceNow];
if (age > 120) return; // ignore old (cached) updates
if (location.horizontalAccuracy < 0) return; // ignore invalid updates
if(self.oldLocation == nil)
{
self.oldLocation = location;
return
}
CLLocationDistance distance = [location distanceFromLocation: self.oldLocation];
if (distance >= 10 && location.horizontalAccuracy <=20) //you can change 10 to 20 if you want more frequent updates
{
// add location to path
self.oldLocation = location; // save newLocation for next time
}
}

尝试使用此代码。它来 self 的项目,并且工作正常。根据您的需要使用它。希望对你有帮助。

关于ios - 如何在 iOS 的谷歌地图上使用 CLLocationManager 生成我的旅行路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31669512/

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