How to open App maps programmatically with coordinates in swift?

I have the latitude and longitude that I want to open in 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 opens cards, but it does not show any output. It also shows the location of the user that I do not want. I want the latitude and longitude to be indicated on the map.

+90
ios swift apple-maps
Feb 19 '15 at 10:51
source share
6 answers

This code works fine for me.

 func openMapForPlace() { let lat1 : NSString = self.venueLat let lng1 : NSString = self.venueLng let latitude:CLLocationDegrees = lat1.doubleValue let longitude:CLLocationDegrees = lng1.doubleValue let regionDistance:CLLocationDistance = 10000 let coordinates = CLLocationCoordinate2DMake(latitude, longitude) let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, regionDistance, regionDistance) let options = [ MKLaunchOptionsMapCenterKey: NSValue(MKCoordinate: regionSpan.center), MKLaunchOptionsMapSpanKey: NSValue(MKCoordinateSpan: regionSpan.span) ] let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil) let mapItem = MKMapItem(placemark: placemark) mapItem.name = "\(self.venueName)" mapItem.openInMapsWithLaunchOptions(options) } 

For quick 3.0:

 import UIKit import MapKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() openMapForPlace() } func openMapForPlace() { let latitude: CLLocationDegrees = 37.2 let longitude: CLLocationDegrees = 22.9 let regionDistance:CLLocationDistance = 10000 let coordinates = CLLocationCoordinate2DMake(latitude, longitude) let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, regionDistance, regionDistance) let options = [ MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center), MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span) ] let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil) let mapItem = MKMapItem(placemark: placemark) mapItem.name = "Place Name" mapItem.openInMaps(launchOptions: options) } } 
+140
Feb 20 '15 at 5:33
source share

If you just want to give the user a direction of movement, here is the latest Swift syntax in its simplest form:

 let coordinate = CLLocationCoordinate2DMake(theLatitude,theLongitude) let mapItem = MKMapItem(placemark: MKPlacemark(coordinate: coordinate, addressDictionary:nil)) mapItem.name = "Target location" mapItem.openInMaps(launchOptions: [MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving]) 
+52
May 7, '16 at 16:50
source share

The MKMapItem approach above works great if you want to get detailed control over the information displayed on Maps.

Otherwise, the code below works fine:

 // Open and show coordinate let url = "http://maps.apple.com/maps?saddr=\(coord.latitude),\(coord.longitude)" UIApplication.shared.openURL(URL(string:url)!) // Navigate from one coordinate to another let url = "http://maps.apple.com/maps?saddr=\(from.latitude),\(from.longitude)&daddr=\(to.latitude),\(to.longitude)" UIApplication.shared.openURL(URL(string:url)!) 

However, the code above does not allow you to send your own place name. Instead, it will display the address.

In the above code, you can also go from any source coordinate that I don't know about, if you can do this using the MKMapItem approach.

+29
Dec 20 '16 at 21:32
source share

You can call a function of the MKMapItem class passing the elements there, it uses only the first and last for the source / destination, respectively, if you want to pass more than two elements.

Swift 5, 4

 let source = MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: lat, longitude: lng))) source.name = "Source" let destination = MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: lat, longitude: lng))) destination.name = "Destination" MKMapItem.openMaps(with: [source, destination], launchOptions: [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]) 

or using the extension:

 extension MKMapItem { convenience init(coordinate: CLLocationCoordinate2D, name: String) { self.init(placemark: .init(coordinate: coordinate)) self.name = name } } let source = MKMapItem(coordinate: .init(latitude: lat, longitude: lng), name: "Source") let destination = MKMapItem(coordinate: .init(latitude: lat, longitude: lng), name: "Destination") MKMapItem.openMaps( with: [source, destination], launchOptions: [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]) 
+25
Apr 14 '18 at 23:34
source share

It works like a charm to me

 let coordinate = CLLocationCoordinate2DMake(theLatitude, theLongitude) let region = MKCoordinateRegionMake(coordinate, MKCoordinateSpanMake(0.01, 0.02)) let placemark = MKPlacemark(coordinate: coordinate, addressDictionary: nil) let mapItem = MKMapItem(placemark: placemark) let options = [ MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: region.center), MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: region.span)] mapItem.name = theLocationName mapItem.openInMaps(launchOptions: options) 
+10
Oct 07 '16 at 15:56
source share

You can use the code below to show the PIN code in lat, long on the Apple card.

 let coordinates = CLLocationCoordinate2DMake(-37.848854,144.990295) let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, 1000, 1000) let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil) let mapItem = MKMapItem(placemark: placemark) mapItem.name = "Desired place" mapItem.openInMaps(launchOptions:[ MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center) ] as [String : Any]) 
+2
May 23 '18 at 9:37
source share



All Articles