
[英]Determining if Latitude/Longitude Point is in a MKPolygon within Mapview?
[英]Put latitude and longitude on a mapView
我试图通过插入纬度和经度来显示我在mapView中想要的地方,但是我没有做到这一点,地图总是向我显示我所在的地方,而不是我想要获得的地方
这些是我使用的代码
class MapViewController: UIViewController, CLLocationManagerDelegate{
@IBOutlet weak var map: MKMapView!
let manager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
map.showsPointsOfInterest = true
map.showsUserLocation = true
manager.requestAlwaysAuthorization()
manager.requestWhenInUseAuthorization()
//user location stuff
if CLLocationManager.locationServicesEnabled() {
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.startUpdatingLocation()
}
}
func locationManager(_manager: CLLocationManager, didUpdateLocations locations:[CLLocation]) {
let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)
let location = CLLocationCoordinate2D(latitude: 36.1070, longitude: -112.1130)
let region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)
map.setRegion(region, animated: true)
self.map.showsUserLocation = true
}
}
评论这个
map.showsUserLocation = true
并将didUpdateLocations
的代码放入viewDidLoad
class MapViewController: UIViewController, CLLocationManagerDelegate{
@IBOutlet weak var map: MKMapView!
let manager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
map.showsPointsOfInterest = true
//map.showsUserLocation = true
manager.requestAlwaysAuthorization()
manager.requestWhenInUseAuthorization()
//user location stuff
if CLLocationManager.locationServicesEnabled() {
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.startUpdatingLocation()
}
let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)
let location = CLLocationCoordinate2D(latitude: 36.1070, longitude: -112.1130)
let region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)
map.setRegion(region, animated: true)
}
func locationManager(_manager: CLLocationManager, didUpdateLocations locations:[CLLocation]) {
///
}
}
为了获得更可靠,更有效的结果,
我建议使用.plist(属性列表)文件动态存储纬度和经度,这样会更容易,并且会花费更少的时间。 plist包含管脚注释方向,它们基本上是一个XML文本文件,其中包含用于捆绑软件执行的基本配置信息。 这是将其附加到项目的方法:在主视图控制器中,编写函数:
func fetchAllData(){
if let path = Bundle.main.path(forResource: "NAME OF YOUR PLIST FILE", ofType: "plist") {
////If your plist contain root as Dictionary
if let dic = NSDictionary(contentsOfFile: path) as? [String: Any] {
let keys=dic.keys
for dataOfKey in keys {
if let object=dic[dataOfKey] as? [[String:Any]]{
locationsArray.append(contentsOf: object)
}
}
}
for location in self.locationsArray{
let newPin = MKPointAnnotation()
newPin.coordinate = CLLocationCoordinate2D.init(latitude: Double(location["latitude"] as! String)!, longitude: Double(location["longitude"] as! String)!)
self.mapView.addAnnotation(newPin)
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.