如何跟踪用户在MKMapview上的位置并在用户path上画线

我想跟踪用户在mapView上的用户path,当用户点击地图上的button,并沿着path绘制蓝色或红色线,如下图所示。 另外我想测量用户行进的距离。 目前我正在使用MKMapView。 这个任务是否可以使用iOS地图套件,或者我应该继续使用Google地图SDK。 我刚开始学习iOS开发,如果你发现这个问题,请耐心等待。 在这里输入图像说明 提前致谢…;)

goggle 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 

可能有点太晚了,但为了相关性,这里是@ 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) }