2

I'm trying to figure out the best way of going about passing a string value to apple maps so that I can search it. I'm working on an application in class that pulls a random string from an array and I want to be able to call apple maps that is already on the device and search whatever string is selected. I've looked up MKLocalSearchRequest and I think that may be the easiest option I'm just not sure how to integrate it with Apple Maps instead of using it with a mapkitview inside of the app. This is a method I found that seems like it could work I'm just not sure how to declare it.

 class func openMaps(with mapItems: [MKMapItem], 
 launchOptions: [String : Any]? = nil) -> Bool
  • Possible duplicate of [Open Apple Maps programmatically in iOS8, Xcode 7, Swift 2](https://stackoverflow.com/questions/33787653/open-apple-maps-programmatically-in-ios8-xcode-7-swift-2) – rbaldwin May 02 '18 at 19:17
  • @rbaldwin I looked at that one and it didn't seem to help me at all – Caleb Bartholomew May 02 '18 at 19:19
  • @CalebBartholomew if it's a string value you can use approach rbaldwin suggested, check more about Map Link in [documentation](https://developer.apple.com/library/content/featuredarticles/iPhoneURLScheme_Reference/MapLinks/MapLinks.html#//apple_ref/doc/uid/TP40007899-CH5-SW1) although it's old it's still working. – Najdan Tomić May 02 '18 at 19:33
  • @NajdanTomić the documentation link you gave me worked perfectly with what rbaldwin gave me. I was able to piece it together with that. Thank you so much – Caleb Bartholomew May 04 '18 at 16:24

3 Answers3

0
import CoreLocation

Then in viewDidLoad:

    let geocoder = CLGeocoder()

    let locationString = "London"

    geocoder.geocodeAddressString(locationString) { (placemarks, error) in
        if let error = error {
            print(error.localizedDescription)
        } else {
            if let location = placemarks?.first?.location {
                let query = "?ll=\(location.coordinate.latitude),\(location.coordinate.longitude)"
                let urlString = "http://maps.apple.com/".appending(query)
                if let url = URL(string: urlString) {
                    UIApplication.shared.open(url, options: [:], completionHandler: nil)
                }
            }
        }
    }
rbaldwin
  • 4,581
  • 27
  • 38
0

You have many option to do that:

import MapKit

First

UIApplication.shared.openURL(NSURL(string:
    "comgooglemaps://?saddr=&daddr=\(place.latitude),\(place.longitude)&directionsmode=driving")! as URL)

Or

@IBAction func action(_ sender: Any) {
    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)
    }
Jogendar Choudhary
  • 3,476
  • 1
  • 12
  • 26
  • Lets say I'm using `mapItem.name = "Taco Bell"` whenever I perform the search it takes me out to a random place in the ocean. Is there no way to have it just automatically search around you? – Caleb Bartholomew May 04 '18 at 15:25
0

Swift 4 and higher

Use this version to get the coordinates for the Address

import CoreLocation

let myAddress = "One,Apple+Park+Way,Cupertino,CA,95014,USA"
let geoCoder = CLGeocoder()
geoCoder.geocodeAddressString(myAddress) { (placemarks, error) in
    guard let placemarks = placemarks?.first else { return }
    let location = placemarks.location?.coordinate ?? CLLocationCoordinate2D()
    guard let url = URL(string:"http://maps.apple.com/?daddr=\(location.latitude),\(location.longitude)") else { return }
    UIApplication.shared.open(url)
}
Jonas Deichelmann
  • 3,513
  • 1
  • 30
  • 45