내 기본 응용 프로그램 내에서 Google Maps iPhone 애플리케이션을 시작하려면 어떻게해야합니까?
-
09-06-2019 - |
문제
그만큼 Apple 개발자 문서 (링크는 지금 죽었습니다) 웹 페이지에 링크를 배치 한 다음 iPhone에서 Mobile Safari를 사용하는 동안 링크를 클릭하면 iPhone과 함께 표준으로 제공되는 Google지도 응용 프로그램이 시작될 것이라고 설명합니다.
연락처에서 주소를 탭핑하는 것과 같은 방식으로 내 기본 iPhone 응용 프로그램 (예 : 모바일 사파리를 통한 웹 페이지가 아님) 내에서 특정 주소로 동일한 Google Maps 애플리케이션을 시작하려면 어떻게해야합니까?
참고 : 이것은 장치 자체에서만 작동합니다. 시뮬레이터에 없습니다.
해결책
iOS 5.1.1 이하의 경우 사용하십시오 openURL
의 방법 UIApplication
. 일반적인 iPhone Magical URL 재 해석을 수행합니다. 그래서
[someUIApplication openURL:[NSURL URLWithString:@"http://maps.google.com/maps?q=London"]]
Google지도 앱을 호출해야합니다.
iOS 6에서 Apple의 자체지도 앱을 호출하게됩니다. 이를 위해 구성하십시오 MKMapItem
표시하려는 위치가있는 개체를 한 다음 openInMapsWithLaunchOptions
메시지. 현재 위치에서 시작하려면 다음을 시도하십시오.
[[MKMapItem mapItemForCurrentLocation] openInMapsWithLaunchOptions:nil];
이것에 대해 Mapkit과 연결되어야합니다 (그리고 위치 액세스 권한이 있습니다).
다른 팁
정확히. 달성하는 데 필요한 코드는 다음과 같습니다.
UIApplication *app = [UIApplication sharedApplication];
[app openURL:[NSURL URLWithString: @"http://maps.google.com/maps?q=London"]];
~부터 문서에 따라, UIAPplication은 SharedApplication에 전화하지 않는 한 응용 프로그램 대의원에서만 사용할 수 있습니다.
특정 코디네이트에서 Google지도를 열려면이 코드를 사용해보십시오.
NSString *latlong = @"-56.568545,1.256281";
NSString *url = [NSString stringWithFormat: @"http://maps.google.com/maps?ll=%@",
[latlong stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
Latlong 문자열을 Corelocation의 현재 위치로 바꿀 수 있습니다.
( "z") 플래그를 사용하여 줌 레벨을 지정할 수도 있습니다. 값은 1-19입니다. 예는 다음과 같습니다.
[UIAPPLICATION SHAREDAPPLICATION] OPENURL : [NSURL URLWITHSTRING :@"http://maps.google.com/maps?z=8"]];
이제 문서화 된 App Store Google지도 앱도 있습니다. https://developers.google.com/maps/documentation/ios/urlscheme
따라서 먼저 설치되어 있는지 확인합니다.
[[UIApplication sharedApplication] canOpenURL:
[NSURL URLWithString:@"comgooglemaps://"]];
그런 다음 조건부 교체 할 수 있습니다 http://maps.google.com/maps?q=
~와 함께 comgooglemaps://?q=
.
지도 링크에 대한 Apple URL 구성표 참조는 다음과 같습니다. https://developer.apple.com/library/archive/featuredarticles/iphoneurlscheme_reference/maplinks/maplinks.html
유효한 맵 링크를 만드는 규칙은 다음과 같습니다.
- 도메인은 Google.com이어야하며 하위 도메인은지도 또는 DITU 여야합니다.
- 쿼리에 사이트가 키로 및 로컬로 포함 된 경우 경로는 /, /맵, /local 또는 /m이어야합니다.
- 경로는 /지도 /*가 될 수 없습니다.
- 모든 매개 변수는 지원되어야합니다. 지원되는 매개 변수 목록 **는 표 1을 참조하십시오.
- 값이 URL 인 경우 매개 변수는 q =*가 될 수 없습니다 (kml을 선택하지 않음).
- 매개 변수는보기 = 텍스트 또는 dirflg = r을 포함 할 수 없습니다.
** 지원되는 매개 변수 목록은 위의 링크를 참조하십시오.
iOS 10을 사용하는 경우 info.plist에 쿼리 체계를 추가하는 것을 잊지 마십시오.
<key>LSApplicationQueriesSchemes</key>
<array>
<string>comgooglemaps</string>
</array>
Objective-C를 사용하는 경우
if ([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"comgooglemaps:"]]) {
NSString *urlString = [NSString stringWithFormat:@"comgooglemaps://?ll=%@,%@",destinationLatitude,destinationLongitude];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
} else {
NSString *string = [NSString stringWithFormat:@"http://maps.google.com/maps?ll=%@,%@",destinationLatitude,destinationLongitude];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:string]];
}
Swift 2.2를 사용하는 경우
if UIApplication.sharedApplication().canOpenURL(NSURL(string: "comgooglemaps:")!) {
var urlString = "comgooglemaps://?ll=\(destinationLatitude),\(destinationLongitude)"
UIApplication.sharedApplication().openURL(NSURL(string: urlString)!)
}
else {
var string = "http://maps.google.com/maps?ll=\(destinationLatitude),\(destinationLongitude)"
UIApplication.sharedApplication().openURL(NSURL(string: string)!)
}
Swift 3.0을 사용하는 경우
if UIApplication.shared.canOpenURL(URL(string: "comgooglemaps:")!) {
var urlString = "comgooglemaps://?ll=\(destinationLatitude),\(destinationLongitude)"
UIApplication.shared.openURL(URL(string: urlString)!)
}
else {
var string = "http://maps.google.com/maps?ll=\(destinationLatitude),\(destinationLongitude)"
UIApplication.shared.openURL(URL(string: string)!)
}
전화 질문에 대해 시뮬레이터에서 테스트하고 있습니까? 이것은 장치 자체에서만 작동합니다.
또한 OpenUrl은 BOOL을 반환합니다. BOOL은 실행중인 장치가 기능을 지원하는지 확인하는 데 사용할 수 있습니다. 예를 들어, 당신은 iPod touch에서 호출 할 수 없습니다 :-)
이 메소드를 호출하고 Google Maps URL 구성표를 다음과 같은 .plist 파일에 추가하십시오. 답변.
Swift-4 :-
func openMapApp(latitude:String, longitude:String, address:String) {
var myAddress:String = address
//For Apple Maps
let testURL2 = URL.init(string: "http://maps.apple.com/")
//For Google Maps
let testURL = URL.init(string: "comgooglemaps-x-callback://")
//For Google Maps
if UIApplication.shared.canOpenURL(testURL!) {
var direction:String = ""
myAddress = myAddress.replacingOccurrences(of: " ", with: "+")
direction = String(format: "comgooglemaps-x-callback://?daddr=%@,%@&x-success=sourceapp://?resume=true&x-source=AirApp", latitude, longitude)
let directionsURL = URL.init(string: direction)
if #available(iOS 10, *) {
UIApplication.shared.open(directionsURL!)
} else {
UIApplication.shared.openURL(directionsURL!)
}
}
//For Apple Maps
else if UIApplication.shared.canOpenURL(testURL2!) {
var direction:String = ""
myAddress = myAddress.replacingOccurrences(of: " ", with: "+")
var CurrentLocationLatitude:String = ""
var CurrentLocationLongitude:String = ""
if let latitude = USERDEFAULT.value(forKey: "CurrentLocationLatitude") as? Double {
CurrentLocationLatitude = "\(latitude)"
//print(myLatitude)
}
if let longitude = USERDEFAULT.value(forKey: "CurrentLocationLongitude") as? Double {
CurrentLocationLongitude = "\(longitude)"
//print(myLongitude)
}
direction = String(format: "http://maps.apple.com/?saddr=%@,%@&daddr=%@,%@", CurrentLocationLatitude, CurrentLocationLongitude, latitude, longitude)
let directionsURL = URL.init(string: direction)
if #available(iOS 10, *) {
UIApplication.shared.open(directionsURL!)
} else {
UIApplication.shared.openURL(directionsURL!)
}
}
//For SAFARI Browser
else {
var direction:String = ""
direction = String(format: "http://maps.google.com/maps?q=%@,%@", latitude, longitude)
direction = direction.replacingOccurrences(of: " ", with: "+")
let directionsURL = URL.init(string: direction)
if #available(iOS 10, *) {
UIApplication.shared.open(directionsURL!)
} else {
UIApplication.shared.openURL(directionsURL!)
}
}
}
희망, 이것이 당신이 찾고있는 것입니다. 어떤 걱정이라도 나에게 돌아갑니다. :)
"g" "Q"로 변경
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"http://maps.google.com/maps?q=London"]]
여전히 문제가있는 경우이 비디오는 Google에서 "내지도"를 얻는 방법을 보여줍니다. 그런 다음 iPhone에 표시됩니다. 그런 다음 링크를 가져 와서 누구에게나 보낼 수 있습니다.
Google 맵으로 이동하려면이 API를 사용하고 대상 위도 및 경도를 보내십시오.
NSString* addr = nil;
addr = [NSString stringWithFormat:@"http://maps.google.com/maps?daddr=%1.6f,%1.6f&saddr=Posizione attuale", destinationLat,destinationLong];
NSURL* url = [[NSURL alloc] initWithString:[addr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:url];
Google URL 형식보다 더 많은 Flexabilty가 필요한 경우, 맵 앱을 시작하는 대신 애플리케이션에 맵을 포함 시키려고합니다. https://sourceforge.net/projects/quickconnect.
Google URL 형식보다 유연성이 더 필요한 경우 맵 앱을 시작하는 대신 응용 프로그램에 맵을 포함 시키려고합니다. 여기 예입니다.
그것은 심지어 모든 임베딩을 수행 할 수있는 소스 코드를 제공합니다.
iPhone4 iOS 6.0.1 (10A523)
Safari & Chrome을 위해. 두 최신 버전 모두 지금까지 (2013-Jun-10th).
아래 URL 체계도 작동합니다. 그러나 Chrome의 경우 페이지 내부에서만 작동하는 경우 주소 표시 줄에서 작동하지 않습니다.
지도 : Q = giventitle@latitude, longtitude
**Getting Directions between 2 locations**
NSString *googleMapUrlString = [NSString stringWithFormat:@"http://maps.google.com/?saddr=%@,%@&daddr=%@,%@", @"30.7046", @"76.7179", @"30.4414", @"76.1617"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:googleMapUrlString]];
Swift 4에서와 같이 작동 코드 :
1 단계 - info.plist에 팔로우를 추가하십시오
<key>LSApplicationQueriesSchemes</key>
<array>
<string>googlechromes</string>
<string>comgooglemaps</string>
</array>
2 단계 - 다음 코드를 사용하여 Google지도를 표시합니다.
let destinationLatitude = "40.7128"
let destinationLongitude = "74.0060"
if UIApplication.shared.canOpenURL(URL(string: "comgooglemaps:")!) {
if let url = URL(string: "comgooglemaps://?ll=\(destinationLatitude),\(destinationLongitude)"), !url.absoluteString.isEmpty {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}else{
if let url = URL(string: "http://maps.google.com/maps?ll=\(destinationLatitude),\(destinationLongitude)"), !url.absoluteString.isEmpty {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}