LoginSignup
8
8

More than 5 years have passed since last update.

swiftでMapを外部アプリとして起動する

Posted at

実装例

import CoreLocation

// 参考サイト
// - Map Links: https://developer.apple.com/library/ios/featuredarticles/iPhoneURLScheme_Reference/MapLinks/MapLinks.html
//

class MapLinkUtils {  
  // 目的地周辺の地図を表示する
  class func showLocation(location: CLLocation, address: String) {
    let daddr = NSString(format: "%f,%f", location.coordinate.latitude,  location.coordinate.longitude)
    let urlString = "http://maps.apple.com/?ll=\(daddr)&address=\(address)"
    print(urlString)
    let encodedUrl = urlString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!
    let url = NSURL(string: encodedUrl)!
    UIApplication.sharedApplication().openURL(url)
  }

  // 経路情報を表示する
  class func showNavigation(srcLocation: CLLocation, destLocation: CLLocation, address: String) {
    let saddr = NSString(format: "%f,%f", srcLocation.coordinate.latitude,  srcLocation.coordinate.longitude)
    let daddr = NSString(format: "%f,%f", destLocation.coordinate.latitude, destLocation.coordinate.longitude)
    let urlString = "http://maps.apple.com/?saddr=\(saddr)&daddr=\(daddr)&dirflg=d&q=\(address)"
    print(urlString)
    let encodedUrl = urlString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!
    let url = NSURL(string: encodedUrl)!
    UIApplication.sharedApplication().openURL(url)
  }
}

参考

8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8