2015-02-19 11 views
56

Ho la latitudine e la longitudine che voglio aprire nella mia applicazione mappa. Ho provato questo codice da HERE.Come aprire l'app delle mappe a livello di programmazione con coordinate in rapido?

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) 

} 

Questa funzione apre correttamente le mappe ma non mostra alcun pin. Mostra anche la posizione dell'utente che non voglio. Voglio solo un segnaposto sulla mappa per la latitudine e la longitudine fornite.

+2

Questo codice è destinato a mostrare le indicazioni stradali dalla posizione dell'utente alla destinazione. Per mostrare solo un singolo bersaglio, usa MKLaunchOptionsMapCenterKey e un singolo elemento della mappa. Vedi http://stackoverflow.com/questions/28427557/openinmapswithlaunchoptions-not-working. – Anna

+0

Grazie per il suggerimento Anna. –

risposta

108

Questo codice funziona correttamente per 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) 

} 

per SWIFT 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) 
    } 
} 
+0

Puoi confermare che l'opzione MKLaunchOptionsMapSpanKey ha l'effetto desiderato? Indipendentemente dal valore che utilizzo per CLLocationDistance, la mappa viene ingrandita abbastanza da vicino. – stone

+2

Sembra che il 'MKLaunchOptionsMapSpanKey' venga ignorato quando uno o più' MKMapItem' vengono aggiunti alla mappa: http://stackoverflow.com/a/32484331/422288 –

+4

Non dimenticare: importa MapKit – jobima

28

Se si desidera solo per dare all'utente indicazioni stradali, ecco l'ultima sintassi Swift nella sua forma più semplice:

let coordinate = CLLocationCoordinate2DMake(theLatitude,theLongitude) 
let mapItem = MKMapItem(placemark: MKPlacemark(coordinate: coordinate, addressDictionary:nil)) 
mapItem.name = "Target location" 
mapItem.openInMaps(launchOptions: [MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving]) 
+0

Questo ha funzionato bene per me, tranne che la chiave e il valore sono invertiti nelle opzioni di lancio. Dovrebbe essere 'mapItem.openInMapsWithLaunchOptions ([MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving])' – Keith

+1

Assicurati di aggiungere MapKit di importazione all'inizio del file per risolvere gli errori. –

+2

Anche in swift 3 l'ultima riga è ora ... mapItem.openInMaps (launchOptions: [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]) –

6

Questo funziona come un charm for 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) 
14

L'approccio MKMapItem funziona in modo ottimale se si desidera un controllo granulare sulle informazioni visualizzate in Maps.

In caso contrario, il codice qui sotto le grandi opere come bene,:

// 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)!) 

Tuttavia, il codice di cui sopra non consentono di inviare in un nome personalizzato del luogo. Invece, mostrerà l'indirizzo.

Il codice sopra consente anche di navigare da qualsiasi coordinata di origine, che non so se si può fare con l'approccio MKMapItem.

+1

Se lasci vuoto "saddr =", l'app imposterà "la tua posizione" come predefinita. Qualcosa come "http: // maps.apple.com/maps?saddr=&daddr=\(to.latitude),\(to.longitude)" –

Problemi correlati