2017-08-16 94 views
1

我在我的應用程序中顯示一個針的谷歌地圖視圖。谷歌地圖SDK不會打開針

func setUpGoogleMapView() { 

    let camera = GMSCameraPosition.camera(withLatitude: self.site.latitude, longitude: self.site.longitude, zoom: 15.0) 
    let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera) 
    mapView.frame = googleMapView.frame 
    self.view.addSubview(mapView) 

    let marker = GMSMarker() 
    marker.position = CLLocationCoordinate2D(latitude: self.site.latitude, longitude: self.site.longitude) 
    marker.title = self.site.name 
    marker.map = mapView 
} 

如果我手動調用UIApplication.shared.openURL傳遞一個URL與查詢,它在不同的模式下打開(註釋不顯示地名,顯示緯度經度&代替)或打不開如果用戶沒有安裝谷歌地圖。

@IBAction func directionsButtonPressed(_ sender: UIButton) { 
    if (UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!)) { 

     UIApplication.shared.openURL(URL(string: 
      "comgooglemaps://?center=\(self.site.latitude),\(self.site.longitude)&zoom=14&views=traffic&q=\(self.site.latitude),\(self.site.longitude)")!) 
    } else { 
     print("Can't use comgooglemaps://"); 
    } 
} 

如何使用UIApplication.shared.openURL在safari中打開谷歌地圖?

如果我打電話給UIApplication.shared.openURL,我應該如何使用谷歌地圖顯示地點名稱的別針?

我想使用UIApplication.shared.openURL後有相同的方向模式。

screenshot

+0

你可以發佈你正在尋找所需的輸出的屏幕截圖? – Hooda

+0

謝謝。它不會讓我添加圖像,因此我添加了一個鏈接 – NatukaKh

+0

您顯示的屏幕截圖是您正在查找的輸出內容? – Hooda

回答

1

斯威夫特3.X:

let strPlaceName = "batumi+botanical+garden" as String! 
let url = URL(string: "https://www.google.com/maps?q="+strPlaceName!) 

if UIApplication.shared.canOpenURL(url!) { 
    if #available(iOS 10.0, *) { 
     UIApplication.shared.open(url!, options: [:], completionHandler: nil) 
    } else { 
     // for iOS 10, below is deprecated 
     UIApplication.shared.openURL(url!) 
    } 
} 

的Objective-C:

NSString *strPlaceName = @"Batumi+Botanical+Garden"; 
NSString *str = [NSString stringWithFormat:@"https://www.google.com/maps/place/%@",strPlaceName]; 
NSURL *url = [NSURL URLWithString:str]; 

if([[UIApplication sharedApplication] canOpenURL:url]) {  
    if([[UIDevice currentDevice].systemVersion floatValue] >= 10.0){ 
     [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil]; 
    } else { 
     // for iOS 10, below is deprecated 
     [[UIApplication sharedApplication] openURL:url]; 
    } 
} 
+0

非常感謝!這真的很好!我不能upvote它,它說我沒有足夠的聲譽:) @Hooda – NatukaKh

+0

@NatukaKh很高興知道它的工作!您需要15點聲望才能點贊。如果你願意,當你有足夠的聲望時,你可以晚點起來。 – Hooda

+0

請問:)再次感謝! – NatukaKh