I have latitude and longitude that I want to open into my map application. I tried this code from HERE.
func goToMap(){
var lat1 : NSString = self.venueLat
var lng1 : NSString = self.venueLng
var latitude:CLLocationDegrees = lat1.doubleValue
var longitude:CLLocationDegrees = lng1.doubleValue
var coordinate = CLLocationCoordinate2DMake(latitude, longitude)
var placemark : MKPlacemark = MKPlacemark(coordinate: coordinate, addressDictionary:nil)
var mapItem:MKMapItem = MKMapItem(placemark: placemark)
mapItem.name = "Target location"
let launchOptions:NSDictionary = NSDictionary(object: MKLaunchOptionsDirectionsModeDriving, forKey: MKLaunchOptionsDirectionsModeKey)
var currentLocationMapItem:MKMapItem = MKMapItem.mapItemForCurrentLocation()
MKMapItem.openMapsWithItems([currentLocationMapItem, mapItem], launchOptions: launchOptions)
}
This function successfully open maps but it doesn't show any pin. Also it shows user location which I don't want. I only want a pin on the map for the provided latitude and longitude.
You could call class function of
MKMapItem
passing items there, it uses only first and last for source / destination appropriately, if you want pass more than two items.Swift 4
This code is working fine for me.
For swift 3.0:
This works as a charm for me
If you just want to give the user driving directions, here's the latest Swift syntax in its simplest form:
The
MKMapItem
approach above works great if you want granular control over the information that is displayed in Maps.Otherwise, the code below works great as well, :
However, the code above does not let you to send in a custom name of the place. Instead, it will show the address.
The code above also lets you navigate from any source coordinate, which I don't know if you can do with the MKMapItem approach.
You can use below code to show PIN on lat, long in to Apple map.