【问题标题】:How to trace users location on MKMapview and draw line on users path如何在 MKMapview 上跟踪用户位置并在用户路径上画线
【发布时间】:2013-12-06 10:29:51
【问题描述】:

当用户单击地图上的按钮并沿着路径绘制一条蓝色或红色线时,我想在 mapView 上跟踪我的用户路径,如下图所示。另外我想测量用户行进的距离。目前我正在使用 MKMapView。是否可以使用 iOS 地图工具包完成这项任务,或者我应该继续使用 Google 地图 SDK。我刚开始学习iOS开发,如果您发现问题不正常,请多多包涵。 提前谢谢...;)

【问题讨论】:

  • 你对在地图上画线做过哪些研究?
  • 我检查了一些他们建议我使用 MKPolyline 方法的地方......我只是不确定。我也找到了一些旧教程,但我不知道它们是否适用于 iOS7...
  • iOS7 弃用了某些东西,但这取决于您支持的 iOS 版本。检查 MKPolylineView 和 MKPolylineRenderer。
  • 如果您不介意可以帮我举一些例子吗...?

标签: ios mkmapview


【解决方案1】:

google ios sdkhttps://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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-04
      • 1970-01-01
      • 1970-01-01
      • 2013-07-02
      • 2020-08-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多