How to show my location and pin in map correctly on Swift?

user3745888 picture user3745888 · Jul 16, 2014 · Viewed 12.6k times · Source

I'm trying sshow my location in app Swift but this not show anything, the map is all blue..

My code is this form a tutorial on internet:

 var latitude:CLLocationDegrees = location.location.coordinate.latitude
    var longitude:CLLocationDegrees = location.location.coordinate.longitude
    var homeLati: CLLocationDegrees = 40.01540192
    var homeLong: CLLocationDegrees = 20.87901079
    var latDelta:CLLocationDegrees = 0.01
    var longDelta:CLLocationDegrees = 0.01
    var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)

    var myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
    var region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
    self.mapKit.setRegion(region, animated: true)

    self.mapKit.showsUserLocation = true
    ///Red Pin
    var myHomePin = MKPointAnnotation()
    myHomePin.coordinate = myHome
    myHomePin.title = "Home"
    myHomePin.subtitle = "Bogdan's home"
    self.mapKit.addAnnotation(myHomePin)

I have imported corresponding configuration in .split.. What is bad on this code?

Thanks!

Answer

Annu picture Annu · Sep 6, 2014

To display location on map using latitude and longitude.

 var latDelta:CLLocationDegrees = 0.01

 var longDelta:CLLocationDegrees = 0.01

 var theSpan:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
 var pointLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(your latitude, your longitude)

 var region:MKCoordinateRegion = MKCoordinateRegionMake(pointLocation, theSpan)
  mapView.setRegion(region, animated: true)

  var pinLocation : CLLocationCoordinate2D = CLLocationCoordinate2DMake(your latitude, your longitude)
  var objectAnnotation = MKPointAnnotation()
  objectAnnotation.coordinate = pinLocation
  objectAnnotation.title = your title
  self.mapView.addAnnotation(objectAnnotation)