17
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

iOSアプリで外部マップアプリ(標準・google map)を起動する

Last updated at Posted at 2017-03-25

iOSアプリ作成時に外部マップアプリ(標準マップアプリ・google mapアプリ)を起動する方法のメモ

下記の修正で起動可能とできる

####1)info.plistの修正
iOS9以降では、info.plistにArray型のLSApplicationQueriesSchemesを追加して、起動スキーム名をString型で登録する必要があります

info.plist..png
(こんな感じで登録、写真はgoogle mapのスキームを登録した例)

####2)コード修正

swift
    if UIApplication.sharedApplication().canOpenURL(NSURL(string:"comgooglemaps://")!) {

        let urlStr:String = "comgooglemaps://?daddr=\(latitude),\(longitude)&directionsmode=walking&zoom=14"

        UIApplication.sharedApplication().openURL(NSURL(string:urlStr)!)
        
    } else {
        
        let daddr = NSString(format: "%f,%f", latitude, longitude)
        
        let urlString = "http://maps.apple.com/?daddr=\(daddr)&dirflg=w"
        
        let encodedUrl = urlString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!
        
        let url = NSURL(string: encodedUrl)!
        
        UIApplication.sharedApplication().openURL(url)
    }

まず、google mapアプリが開けるか(インストールされているか)を確認

UIApplication.sharedApplication().canOpenURL(NSURL(string:"comgooglemaps://")!)

google mapアプリが開ける場合(インストールされている場合)は、google mapアプリを開く
(オプション詳細はここを参照:Google ios-sdk

        let urlStr:String = "comgooglemaps://?daddr=\(latitude),\(longitude)&directionsmode=walking&zoom=14"

        UIApplication.sharedApplication().openURL(NSURL(string:urlStr)!)

google mapアプリが開けない場合(インストールされていない場合)は標準のマップアプリを開く
(オプション詳細はここを参照:Apple Reference

            let daddr = NSString(format: "%f,%f", lat, lon)
            
            let urlString = "http://maps.apple.com/?daddr=\(daddr)&dirflg=w"
            
            let encodedUrl = urlString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!
            
            let url = NSURL(string: encodedUrl)!
            
            UIApplication.sharedApplication().openURL(url)
17
14
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
17
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?