Google 地图:如何在 google 地图 ios 上绘制虚线以连接方向路径的起点
GoogleMaps : How to draw dash line to connect with starting point of direction path on google map ios
我在我的项目中使用 google 地图作为方向和显示路线。我已经完成了所有的事情,但我无法绘制虚线来连接我所在位置或建筑物的路线起点。
查看我的应用截图截图1
现在我想像 google-maps screenshot 2
那样画虚线
任何意见和建议将不胜感激
像这样创建一个MKPolyLine
var pointsToUse: [CLLocationCoordinate2D] = []
pointsToUse += [CLLocationCoordinate2DMake(55.4, -3.2)] // current location
pointsToUse += [CLLocationCoordinate2DMake(55.6, -3.4)] // lat lon of starting point
let polyline = MKPolyline(coordinates: &pointsToUse, count: pointsToUse.count)
mapView.add(polyline, level: MKOverlayLevel.aboveRoads)
然后像这样更新 MapView 渲染器
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer
{
if (overlay is MKPolyline)
{
let pr = MKPolylineRenderer(overlay: overlay)
pr.strokeColor = UIColor.blue.withAlphaComponent(0.5)
pr.lineWidth = 2
pr.lineDashPattern = [10, 10]
return pr
}
}
要添加虚线以连接起点和终点,您必须在方向路径中做更多的事情 json 响应,我在某处绘制了直线虚线折线,如果您有与代码相关的曲线然后更新还有你的回答,
Russell 建议link对你帮助不大,
Drawing Route Between Two Places on GMSMapView in iOS
你必须做以下事情,
func drawRoute(coordinate: CLLocationCoordinate2D) {
// let directionURL = "https://maps.googleapis.com/maps/api/directions/json?" +
// "origin=\(19.0760),\(72.8777)&destination=\(18.520),\(72.9781)&" +
// "key=AIzaSyBdzgSm8g6w3daxTQvtlG9pqBxrj3lkaN0"
//
var directionURL = "https://maps.googleapis.com/maps/api/directions/json?origin=\(coordinate.latitude),\(coordinate.longitude)&destination=\(18.5767),\(73.6881)&key=AIzaSyARoB09HGFjDy3IKfLpZq-ZQd3YwUT-3_E"
//AIzaSyDxSgGQX6jrn4iq6dyIWAKEOTneZ3Z8PtU
directionURL += "&mode=" + "walking"
print("drawRoute")
Alamofire.request(directionURL).responseJSON
{ response in
if let JSON = response.result.value {
let mapResponse: [String: AnyObject] = JSON as! [String : AnyObject]
let routesArray = (mapResponse["routes"] as? Array) ?? []
let routes = (routesArray.first as? Dictionary<String, AnyObject>) ?? [:]
//print("routes : \(routes)")
//--------Dash line lat-long for starting point ----------\
let dictArray = (routes["legs"] as? Array) ?? []
let dict = (dictArray.first as? Dictionary<String, AnyObject>) ?? [:]
let steps = (dict["steps"] as? Array) ?? []
let stepsDict = (steps.first as? Dictionary<String, AnyObject>) ?? [:]
let startLocation = stepsDict["start_location"]
let lat = startLocation!["lat"] as! NSNumber
let lng = startLocation!["lng"] as! NSNumber
print("lat : \(lat) lng : \(lng)")
let dotCoordinate = CLLocationCoordinate2D(latitude: CLLocationDegrees(lat), longitude: CLLocationDegrees(lng))
//--------Route polypoints----------\
let overviewPolyline = (routes["overview_polyline"] as? Dictionary<String,AnyObject>) ?? [:]
let polypoints = (overviewPolyline["points"] as? String) ?? ""
let line = polypoints
self.addPolyLine(encodedString: line, coordinate:coordinate , dotCoordinate:dotCoordinate)
}
}
}
现在按照下面的方式绘制路线折线和虚线,
func addPolyLine(encodedString: String, coordinate: CLLocationCoordinate2D ,dotCoordinate : CLLocationCoordinate2D) {
//--------Dash line to connect starting point---------\
let dotPath :GMSMutablePath = GMSMutablePath()
// add coordinate to your path
dotPath.add(CLLocationCoordinate2DMake(coordinate.latitude, coordinate.longitude))
dotPath.add(CLLocationCoordinate2DMake(dotCoordinate.latitude, dotCoordinate.longitude))
let dottedPolyline = GMSPolyline(path: dotPath)
dottedPolyline?.map = self.viewMap
dottedPolyline?.strokeWidth = 3.0
let styles: [Any] = [GMSStrokeStyle.solidColor(UIColor.green), GMSStrokeStyle.solidColor(UIColor.clear)]
let lengths: [Any] = [10, 5]
dottedPolyline?.spans = GMSStyleSpans(dottedPolyline?.path!, styles as! [GMSStrokeStyle], lengths as! [NSNumber], kGMSLengthRhumb)
//---------Route Polyline---------\
let path = GMSMutablePath(fromEncodedPath: encodedString)
let polyline = GMSPolyline(path: path)
polyline?.strokeWidth = 5
polyline?.strokeColor = .blue
polyline?.map = self.viewMap
}
查看输出:
我在我的项目中使用 google 地图作为方向和显示路线。我已经完成了所有的事情,但我无法绘制虚线来连接我所在位置或建筑物的路线起点。
查看我的应用截图截图1
现在我想像 google-maps screenshot 2
那样画虚线任何意见和建议将不胜感激
像这样创建一个MKPolyLine
var pointsToUse: [CLLocationCoordinate2D] = []
pointsToUse += [CLLocationCoordinate2DMake(55.4, -3.2)] // current location
pointsToUse += [CLLocationCoordinate2DMake(55.6, -3.4)] // lat lon of starting point
let polyline = MKPolyline(coordinates: &pointsToUse, count: pointsToUse.count)
mapView.add(polyline, level: MKOverlayLevel.aboveRoads)
然后像这样更新 MapView 渲染器
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer
{
if (overlay is MKPolyline)
{
let pr = MKPolylineRenderer(overlay: overlay)
pr.strokeColor = UIColor.blue.withAlphaComponent(0.5)
pr.lineWidth = 2
pr.lineDashPattern = [10, 10]
return pr
}
}
要添加虚线以连接起点和终点,您必须在方向路径中做更多的事情 json 响应,我在某处绘制了直线虚线折线,如果您有与代码相关的曲线然后更新还有你的回答,
Russell 建议link对你帮助不大,
Drawing Route Between Two Places on GMSMapView in iOS
你必须做以下事情,
func drawRoute(coordinate: CLLocationCoordinate2D) {
// let directionURL = "https://maps.googleapis.com/maps/api/directions/json?" +
// "origin=\(19.0760),\(72.8777)&destination=\(18.520),\(72.9781)&" +
// "key=AIzaSyBdzgSm8g6w3daxTQvtlG9pqBxrj3lkaN0"
//
var directionURL = "https://maps.googleapis.com/maps/api/directions/json?origin=\(coordinate.latitude),\(coordinate.longitude)&destination=\(18.5767),\(73.6881)&key=AIzaSyARoB09HGFjDy3IKfLpZq-ZQd3YwUT-3_E"
//AIzaSyDxSgGQX6jrn4iq6dyIWAKEOTneZ3Z8PtU
directionURL += "&mode=" + "walking"
print("drawRoute")
Alamofire.request(directionURL).responseJSON
{ response in
if let JSON = response.result.value {
let mapResponse: [String: AnyObject] = JSON as! [String : AnyObject]
let routesArray = (mapResponse["routes"] as? Array) ?? []
let routes = (routesArray.first as? Dictionary<String, AnyObject>) ?? [:]
//print("routes : \(routes)")
//--------Dash line lat-long for starting point ----------\
let dictArray = (routes["legs"] as? Array) ?? []
let dict = (dictArray.first as? Dictionary<String, AnyObject>) ?? [:]
let steps = (dict["steps"] as? Array) ?? []
let stepsDict = (steps.first as? Dictionary<String, AnyObject>) ?? [:]
let startLocation = stepsDict["start_location"]
let lat = startLocation!["lat"] as! NSNumber
let lng = startLocation!["lng"] as! NSNumber
print("lat : \(lat) lng : \(lng)")
let dotCoordinate = CLLocationCoordinate2D(latitude: CLLocationDegrees(lat), longitude: CLLocationDegrees(lng))
//--------Route polypoints----------\
let overviewPolyline = (routes["overview_polyline"] as? Dictionary<String,AnyObject>) ?? [:]
let polypoints = (overviewPolyline["points"] as? String) ?? ""
let line = polypoints
self.addPolyLine(encodedString: line, coordinate:coordinate , dotCoordinate:dotCoordinate)
}
}
}
现在按照下面的方式绘制路线折线和虚线,
func addPolyLine(encodedString: String, coordinate: CLLocationCoordinate2D ,dotCoordinate : CLLocationCoordinate2D) {
//--------Dash line to connect starting point---------\
let dotPath :GMSMutablePath = GMSMutablePath()
// add coordinate to your path
dotPath.add(CLLocationCoordinate2DMake(coordinate.latitude, coordinate.longitude))
dotPath.add(CLLocationCoordinate2DMake(dotCoordinate.latitude, dotCoordinate.longitude))
let dottedPolyline = GMSPolyline(path: dotPath)
dottedPolyline?.map = self.viewMap
dottedPolyline?.strokeWidth = 3.0
let styles: [Any] = [GMSStrokeStyle.solidColor(UIColor.green), GMSStrokeStyle.solidColor(UIColor.clear)]
let lengths: [Any] = [10, 5]
dottedPolyline?.spans = GMSStyleSpans(dottedPolyline?.path!, styles as! [GMSStrokeStyle], lengths as! [NSNumber], kGMSLengthRhumb)
//---------Route Polyline---------\
let path = GMSMutablePath(fromEncodedPath: encodedString)
let polyline = GMSPolyline(path: path)
polyline?.strokeWidth = 5
polyline?.strokeColor = .blue
polyline?.map = self.viewMap
}
查看输出: