Reputation: 3
I tried to show the place that I want in mapView by insering latitude and longitude, but I failed to do that and the map show me always my place where I am and not the place that I want to get
these is the code that I used
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
}
}
Upvotes: 0
Views: 56
Reputation: 11
To have a more reliable and efficient result,
I recommend using .plist (property list) file to store the latitude and longitude dynamically it would be much easier and would cost you less time. plist contains the pins annotation directions which are basically an XML text file that holds the essential configuration information for bundle execution. here is how to attach it to your project: In your main view controller write the function:
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)
}
}
}
Upvotes: 0
Reputation: 100503
Comment this
map.showsUserLocation = true
And put the code of didUpdateLocations
inside 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]) {
///
}
}
Upvotes: 0