更改位置坐标,更改图钉并在地图上重新绘制路线
Change the location coordinates, change the pin and redraw the routes on a map
我已经使用故事板创建了一个单视图应用程序。我将 mkMapView 放在 ViewController 上并将其嵌入导航 controller.so 中,该程序显示了 MKMapView 中作为源坐标和目标坐标的两个引脚之间的路线。
import UIKit
import MapKit
class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet weak var mapView: MKMapView!
var s = 0.0
var t = 0.0
var sourceLocation = CLLocationCoordinate2D(latitude: 40.759011, longitude: -73.984472)
var destinationLocation = CLLocationCoordinate2D(latitude: 40.748441, longitude: -73.985564)
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
self.dummy()
}
func dummy()
{
// 1.
// 2.
// 3.
let sourcePlacemark = MKPlacemark(coordinate: sourceLocation, addressDictionary: nil)
let destinationPlacemark = MKPlacemark(coordinate: destinationLocation, addressDictionary: nil)
// 4.
let sourceMapItem = MKMapItem(placemark: sourcePlacemark)
let destinationMapItem = MKMapItem(placemark: destinationPlacemark)
// 5.
let sourceAnnotation = MKPointAnnotation()
sourceAnnotation.title = "Times Square"
if let location = sourcePlacemark.location {
sourceAnnotation.coordinate = location.coordinate
}
let destinationAnnotation = MKPointAnnotation()
destinationAnnotation.title = "Empire State Building"
if let location = destinationPlacemark.location {
destinationAnnotation.coordinate = location.coordinate
}
// 6.
self.mapView.showAnnotations([sourceAnnotation,destinationAnnotation], animated: true )
// 7.
let directionRequest = MKDirectionsRequest()
directionRequest.source = sourceMapItem
directionRequest.destination = destinationMapItem
directionRequest.transportType = .automobile
// Calculate the direction
let directions = MKDirections(request: directionRequest)
// 8.
directions.calculate {
(response, error) -> Void in
guard let response = response else {
if let error = error {
print("Error: \(error)")
}
return
}
let route = response.routes[0]
self.mapView.add((route.polyline), level: MKOverlayLevel.aboveRoads)
let rect = route.polyline.boundingMapRect
self.mapView.setRegion(MKCoordinateRegionForMapRect(rect), animated: true)
}
}
我在 mapView 上放置了一个 changeLatitude 按钮,我正在更改地图
通过将当前源引脚位置分配给目标和源引脚位置纬度 s=s+0.02000 来增加引脚位置。并将源引脚放在新位置。
@IBAction func changeLatitude(_ sender: Any) {
destinationLocation = CLLocationCoordinate2D(latitude: t, longitude: -73.984472)
s=s+0.02000
sourceLocation = CLLocationCoordinate2D(latitude: 40.759011-s, longitude: -73.984472)
t=40.759011-s;
self.refresh()
self.dummy()
//self.refresh(sender: AnyObject)
}
我已经从旧的源位置和旧的目标位置删除了旧的引脚,但旧的路线仍然存在我想删除旧的路线。我如何删除旧的路线?
func refresh() {
mapView.setCenter(mapView.userLocation.coordinate, animated: true)
if self.mapView.overlays.count > 0
{
//let allAnnotations = self.mapView.annotations
//self.mapView.removeAnnotations(allAnnotations)
// self.mapView.removeOverlays(self.mapView.layoutGuides)
self.mapView.removeAnnotations(self.mapView.annotations)
}
}
我在 about refresh() 函数中使用以下语句从 KMmapView 中删除旧图钉
self.mapView.removeAnnotations(self.mapView.annotations)
如何去除路由线?
您可以从这里下载示例项目 link https://www.dropbox.com/s/77m0395nyho9die/IOS9DrawRouteMapKitTutorial1.zip?dl=0
self.mapView.removeOverlays(self.mapView.overlays)
试试这个,它会从您的地图中删除叠加层。
我已经使用故事板创建了一个单视图应用程序。我将 mkMapView 放在 ViewController 上并将其嵌入导航 controller.so 中,该程序显示了 MKMapView 中作为源坐标和目标坐标的两个引脚之间的路线。
import UIKit
import MapKit
class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet weak var mapView: MKMapView!
var s = 0.0
var t = 0.0
var sourceLocation = CLLocationCoordinate2D(latitude: 40.759011, longitude: -73.984472)
var destinationLocation = CLLocationCoordinate2D(latitude: 40.748441, longitude: -73.985564)
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
self.dummy()
}
func dummy()
{
// 1.
// 2.
// 3.
let sourcePlacemark = MKPlacemark(coordinate: sourceLocation, addressDictionary: nil)
let destinationPlacemark = MKPlacemark(coordinate: destinationLocation, addressDictionary: nil)
// 4.
let sourceMapItem = MKMapItem(placemark: sourcePlacemark)
let destinationMapItem = MKMapItem(placemark: destinationPlacemark)
// 5.
let sourceAnnotation = MKPointAnnotation()
sourceAnnotation.title = "Times Square"
if let location = sourcePlacemark.location {
sourceAnnotation.coordinate = location.coordinate
}
let destinationAnnotation = MKPointAnnotation()
destinationAnnotation.title = "Empire State Building"
if let location = destinationPlacemark.location {
destinationAnnotation.coordinate = location.coordinate
}
// 6.
self.mapView.showAnnotations([sourceAnnotation,destinationAnnotation], animated: true )
// 7.
let directionRequest = MKDirectionsRequest()
directionRequest.source = sourceMapItem
directionRequest.destination = destinationMapItem
directionRequest.transportType = .automobile
// Calculate the direction
let directions = MKDirections(request: directionRequest)
// 8.
directions.calculate {
(response, error) -> Void in
guard let response = response else {
if let error = error {
print("Error: \(error)")
}
return
}
let route = response.routes[0]
self.mapView.add((route.polyline), level: MKOverlayLevel.aboveRoads)
let rect = route.polyline.boundingMapRect
self.mapView.setRegion(MKCoordinateRegionForMapRect(rect), animated: true)
}
}
我在 mapView 上放置了一个 changeLatitude 按钮,我正在更改地图 通过将当前源引脚位置分配给目标和源引脚位置纬度 s=s+0.02000 来增加引脚位置。并将源引脚放在新位置。
@IBAction func changeLatitude(_ sender: Any) {
destinationLocation = CLLocationCoordinate2D(latitude: t, longitude: -73.984472)
s=s+0.02000
sourceLocation = CLLocationCoordinate2D(latitude: 40.759011-s, longitude: -73.984472)
t=40.759011-s;
self.refresh()
self.dummy()
//self.refresh(sender: AnyObject)
}
我已经从旧的源位置和旧的目标位置删除了旧的引脚,但旧的路线仍然存在我想删除旧的路线。我如何删除旧的路线?
func refresh() {
mapView.setCenter(mapView.userLocation.coordinate, animated: true)
if self.mapView.overlays.count > 0
{
//let allAnnotations = self.mapView.annotations
//self.mapView.removeAnnotations(allAnnotations)
// self.mapView.removeOverlays(self.mapView.layoutGuides)
self.mapView.removeAnnotations(self.mapView.annotations)
}
}
我在 about refresh() 函数中使用以下语句从 KMmapView 中删除旧图钉
self.mapView.removeAnnotations(self.mapView.annotations)
如何去除路由线?
您可以从这里下载示例项目 link https://www.dropbox.com/s/77m0395nyho9die/IOS9DrawRouteMapKitTutorial1.zip?dl=0
self.mapView.removeOverlays(self.mapView.overlays)
试试这个,它会从您的地图中删除叠加层。