NatukaKh NatukaKh - 1 year ago 54
iOS Question

Google Maps SDK won't open pin

I have a google maps view inside my app showing a pin.

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
}


If I manually call
UIApplication.shared.openURL
passing in a URL with the query, it opens in a different mode (the annotation doesn't show the name of the place, shows latitude & longitude instead) or doesn't open if the user hasn't installed google maps.

@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://");
}
}


How can I open google maps in safari using
UIApplication.shared.openURL
?

How should I have google maps to display the pin with name of the place if I call
UIApplication.shared.openURL
?

I would like to have the same directions mode after using
UIApplication.shared.openURL
.

screenshot

Answer Source

Swift 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];
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download