问题
I would like to trace my users path on mapView when the user clicks a button on the map and draw a blue or red line along the path like shown in the image below. Also I would like to measure the distance traveled by the user. Currently I am using MKMapView. Is this task possible with iOS map kit or should i move on to using Google maps SDK. I've just started learning iOS development please bear with me if you find the question out of order.

回答1:
Might be a bit too late, but for the sake of relevance, here's @Rinju's code in Swift (I added a bit more information here too):
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)
}
回答2:
google ios sdk
https://developers.google.com/maps/documentation/ios/ Follow this.
#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
来源:https://stackoverflow.com/questions/20129683/how-to-trace-users-location-on-mkmapview-and-draw-line-on-users-path