使用mapbox自定义MGLPolyline

使用mapbox自定义MGLPolyline

本文介绍了使用mapbox自定义MGLPolyline的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚开始使用Mapbox,便通过将其添加到locationManaged didUpdateLocations中来绘制了MGLPolyline

Just started using Mapbox, managed to draw a MGLPolyline by adding this in the locationManaged didUpdateLocations

   var shape = MGLPolyline(coordinates: &a, count: UInt(a.count))
   mapView.addAnnotation(shape)
  1. 更改线宽不会在屏幕上更改

func mapView(mapView: MGLMapView, lineWidthForPolylineAnnotation annotation: MGLPolyline) -> CGFloat { return 20.0 }

  1. 如何将笔划默认颜色从黑色更改为其他颜色?

推荐答案

您需要将地图委托设置为self才能使函数正常工作.这是代码:

You need to set the map delegate to self for the functions to work. Here is the code:

使用MGLMapViewDelegate

class yourController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate, MGLMapViewDelegate{

然后在设置地图后,像这样添加self.mapView.delegate = self

Then after you set the map, add self.mapView.delegate = self like so

mapView = MGLMapView(frame: mapViewWrapper.bounds, styleURL: NSURL(string: Mapbox.getTheme()))
mapView = Mapbox.configure(mapView)
mapView.setCenterCoordinate(appleMap.userLocation.coordinate, zoomLevel: 12, animated: true)
mapViewWrapper.addSubview(mapView)
self.mapView.delegate = self

然后您的功能将起作用:

Then your functions will work:

func mapView(mapView: MGLMapView, alphaForShapeAnnotation annotation: MGLShape) -> CGFloat {
   // Set the alpha for all shape annotations to 1 (full opacity)
   return 1
}

func mapView(mapView: MGLMapView, lineWidthForPolylineAnnotation annotation: MGLPolyline) -> CGFloat {
   // Set the line width for polyline annotations
   return 5.0
}

func mapView(mapView: MGLMapView, strokeColorForShapeAnnotation annotation: MGLShape) -> UIColor {
   // Give our polyline a unique color by checking for its `title` property
   return UIColor.redColor()
}

这篇关于使用mapbox自定义MGLPolyline的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 00:04