我目前正在使用location开发iOS应用程序。 但是我希望有一个自定义地图来显示用户的位置,而不是MapKit给出的默认地图。
这样的地图例如:
是否有可能在Swift 4.0中实现类似的功能?
非常感谢
答案 0 :(得分:1)
您需要使用MKTileOverlay
- 文档为here ...
正如它所说:
您使用图块叠加对象来表示您自己的基于图块的内容 并在地图视图中协调该内容的显示。您的 磁贴可以补充底层地图内容或替换它 完全强>
(我的重点)
答案 1 :(得分:-1)
这是您可以使地图显示当前实时位置的方式。
完整代码:
import UIKit
import MapKit
//import CoreLocation
class ViewController: UIViewController, UIScrollViewDelegate ,CLLocationManagerDelegate, MKMapViewDelegate {
@IBOutlet var map: MKMapView!
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation: CLLocation = locations[0]
let latitude = userLocation.coordinate.latitude
let longitude = userLocation.coordinate.longitude
let latDelta: CLLocationDegrees = 0.05
let lonDelta: CLLocationDegrees = 0.05
let span = MKCoordinateSpan(latitudeDelta: latDelta, longitudeDelta: lonDelta)
let location = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
let region = MKCoordinateRegion(center: location, span: span)
self.map.setRegion(region, animated: false)
let annotation = MKPointAnnotation()
annotation.coordinate = location
annotation.title = "My current Location"
map.addAnnotation(annotation)
}
}
请注意,您还必须将权限添加到info.plist文件中 权限是:
隐私 - 使用时的位置用法说明
隐私 - 位置始终使用说明
答案 2 :(得分:-1)
以下是如何在地图视图中提供固定位置和图钉
import UIKit
import MapKit
class ViewController: UIViewController {
@IBOutlet weak var myMap: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
let latitude : CLLocationDegrees = 86.44999
let longitude : CLLocationDegrees = 54.39
let latitudeDelta : CLLocationAccuracy = 0.05
let longituteDelta : CLLocationAccuracy = 0.05
let myLocation = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
let mySpan = MKCoordinateSpanMake(latitudeDelta, longituteDelta)
let myRegion = MKCoordinateRegion(center: myLocation, span: mySpan)
myMap.setRegion(myRegion, animated: true)
let annotation = MKPointAnnotation()
annotation.coordinate = myLocation
myMap.addAnnotation(annotation)
}
}