如何根据gps位置纬度动态扩展谷歌地图的折线
Posted
技术标签:
【中文标题】如何根据gps位置纬度动态扩展谷歌地图的折线【英文标题】:How to extend polyline of google maps based on gps location latitudes and langitudes dynamically 【发布时间】:2016-04-04 12:28:03 【问题描述】:最初创建了折线并为其添加了一些路径。创建这条折线后,我需要根据 gps 位置添加一些其他点并再次绘制折线。是否再次重绘路径。
_path = [GMSMutablePath path];
[_path addLatitude:12.9716 longitude:77.5946]; // bangalore
[_path addLatitude:13.3710 longitude:76.6413]; // Fiji
[_path addLatitude:15.3173 longitude:75.7139]; // Hawaii
[_path addLatitude:15.3647 longitude:75.1240]; // Mountain View
GMSMarker *marker1 = [[GMSMarker alloc] init];
marker1.position = CLLocationCoordinate2DMake(12.9716, 77.5946);
marker1.title = @"Bangalore";
marker1.groundAnchor = CGPointMake(0.2, 0.9);
marker1.appearAnimation = kGMSMarkerAnimationPop;
marker1.icon = [UIImage imageNamed:@"Flag Filled -50.png"];
marker1.snippet = @"India";
marker1.map = _mapView;
GMSMarker *marker2 = [[GMSMarker alloc] init];
marker2.position = CLLocationCoordinate2DMake(13.3710, 76.6413);
marker2.title = @"Tumkur";
marker2.groundAnchor = CGPointMake(0.3, 0.9);
marker2.snippet = @"India";
marker2.icon = [UIImage imageNamed:@"Flag Filled -50.png"];
marker2.map = _mapView;
GMSMarker *marker3 = [[GMSMarker alloc] init];
marker3.position = CLLocationCoordinate2DMake(15.3173, 75.7139);
marker3.title = @"Mysore";
marker3.groundAnchor = CGPointMake(0.3, 0.9);
marker3.snippet = @"India";
marker3.icon = [UIImage imageNamed:@"Flag Filled -50.png"];
marker3.map = _mapView;
GMSMarker *marker4 = [[GMSMarker alloc] init];
marker4.position = CLLocationCoordinate2DMake(15.3647, 75.1240);
marker4.title = @" Hubli";
marker4.groundAnchor = CGPointMake(0.3, 0.9);
marker4.snippet = @"India";
marker4.icon = [UIImage imageNamed:@"Flag Filled -50.png"];
marker4.map = _mapView;
marker4.rotation = 180;
CLLocationCoordinate2D circleCenter1 = CLLocationCoordinate2DMake(15.3647, 75.1240);
GMSCameraUpdate *updatedCamera = [GMSCameraUpdate setTarget:circleCenter1 zoom:10];
[_mapView animateWithCameraUpdate:updatedCamera];
_polyline = [GMSPolyline polylineWithPath:_path];
_polyline.strokeColor = [UIColor blueColor];
_polyline.strokeWidth = 1.f;
_polyline.map = _mapView;
【问题讨论】:
【参考方案1】:-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
static CLLocation *lastKnownLocation = nil;
if(lastKnownLocation == nil) lastKnownLocation = [locations lastObject];
else
CLLocation *currentLoc = [locations lastObject];
// Forming GMSPath from User's last location to User's current location.
GMSMutablePath *path = [GMSMutablePath new];
[path addCoordinate:[lastKnownLocation coordinate]];
[path addCoordinate:[currentLoc coordinate]];
GMSPolyline *polyLine = [GMSPolyline polylineWithPath:path];
polyLine.strokeColor = [UIColor redColor];
polyLine.strokeWidth = 2.f;
polyLine.map = googleMap;
// Saving current loc as last known loc,so that we can draw another GMSPolyline from last location to current location when you recieved another callback for this method.
lastKnownLocation = currentLoc;
当应用获得新的位置更新时(从第二次开始),上面的代码将在谷歌地图上绘制路径。
希望对你有帮助,
如果您需要更深入的信息,请回复我。
【讨论】:
在您的代码中,每次 GMSMutablePath 都会创建新实例,从而导致内存泄漏..感谢帮助 但是 GMSMutablePath 对象仅是本地的,并且会在 else 块完成时消失,如果需要可以在使用后将路径实例设置为 nil。 还有一件事是直到 & 除非我们使用 ARC,否则与 GMSMutablePath 相关的代码不会导致任何内存泄漏,根据您的需要重组代码,我只是想给您一个关于需求的想法.以上是关于如何根据gps位置纬度动态扩展谷歌地图的折线的主要内容,如果未能解决你的问题,请参考以下文章