如何在MKMapview上跟踪用户位置并在用户路径上绘制线条

时间:2013-11-21 19:01:33

标签: ios mkmapview

当用户点击地图上的按钮并沿路径绘制蓝线或红线时,我想在mapView上跟踪我的用户路径,如下图所示。另外,我想测量用户行进的距离。目前我正在使用MKMapView。使用iOS地图套件可以完成此任务,还是应该继续使用Google地图SDK。我刚刚开始学习iOS开发,如果你发现问题无序,请耐心等待。 enter image description here先谢谢...;)

2 个答案:

答案 0 :(得分:1)

google ios sdk https://developers.google.com/maps/documentation/ios/关注此事。

#import <GoogleMaps/GoogleMaps.h>
#import "DemoViewController.h"

@implementation DemoViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:0
                                                          longitude:-165
                                                               zoom:2];
  GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];

  GMSMutablePath *path = [GMSMutablePath path];
  [path addLatitude:-33.866 longitude:151.195]; // Sydney
  [path addLatitude:-18.142 longitude:178.431]; // Fiji
  [path addLatitude:21.291 longitude:-157.821]; // Hawaii
  [path addLatitude:37.423 longitude:-122.091]; // Mountain View

  GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
  polyline.strokeColor = [UIColor blueColor];
  polyline.strokeWidth = 5.f;
  polyline.map = mapView;

  self.view = mapView;
}

@end

答案 1 :(得分:1)

可能有点太晚了,但为了相关性,这里是@Rinju在Swift中的代码(我在这里也添加了更多信息):

override func viewDidLoad() {
super.viewDidLoad()
//This is a dummy location, you'd add locations to it using the 
//  func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
let location:CLLocation = CLLocation(latitude: 200, longitude: 100)
let locationArray:Array<CLLocation> = [location]
let camera:GMSCameraPosition = GMSCameraPosition.camera(withLatitude: (locationArray.first?.coordinate.latitude)!, longitude: (locationArray.first?.coordinate.longitude)!, zoom: 2)
//You can obtain the Lat and Long for above from the list of arrays of locations you saved
//You can use the .first or .last on the array (I used first)

let mapview:GMSMapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)

let path:GMSMutablePath = GMSMutablePath()
for nextLocation in locationArray {
    if locationArray.index(of: nextLocation) != 0 {
        //You dont want to use the first one as you've already done it
        //so you start with 1
        path.addLatitude(nextLocation.coordinate.latitude, longitude: nextLocation.coordinate.longitude)
    }
}

let polyline:GMSPolyline = GMSPolyline(path: path)
polyline.strokeColor = UIColor.red
polyline.strokeWidth = 2
polyline.map = mapview
self.view = mapview

//I personally prefer view.addSubview(mapview)
}