Search code examples
objective-cgoogle-mapsios7google-maps-markersurl-scheme

ios actionsheet make call to external google map app and pin marker


what i need is, send lat and lng information from my own app by using actionsheet delegation to google map app, and with provided lat lng parameters, google map can draw a pin (marker)

here is my code

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    //coordinates for the place we want to display
    CLLocationCoordinate2D mosqueLocation = CLLocationCoordinate2DMake(lat,lng);
    if (buttonIndex==0) {
        //Apple Maps, using the MKMapItem class
        MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:mosqueLocation addressDictionary:nil];
        MKMapItem *item = [[MKMapItem alloc] initWithPlacemark:placemark];
        item.name = navBarTitle.title;
        [item openInMapsWithLaunchOptions:nil];
    } else if (buttonIndex==1) {
        //Google Maps
        //construct a URL using the comgooglemaps schema
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"comgooglemaps://?center=%f,%f",mosqueLocation.latitude,mosqueLocation.longitude]];
        if (![[UIApplication sharedApplication] canOpenURL:url]) {
            NSLog(@"Google Maps app is not installed");
              } else {
            [[UIApplication sharedApplication] openURL:url];
        }
    }
}

as you can see, i have apple map with red pin drawed successfully but i dont know how to draw the same pin on google map by using the comgooglemap urlscheme.

lat and lng provided, just need to send to google map app to make the draw, any suggestions would be very helpful, code example even better, thanks


Solution

  • Add a search for the coordinates to show a marker at that location:

    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]) {
      NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"comgooglemaps://?center=%f,%f&q=%f,%f",mosqueLocation.latitude,mosqueLocation.longitude, mosqueLocation.latitude,mosqueLocation.longitude]];
      [[UIApplication sharedApplication] openURL:url];
    } else {
      NSLog(@"Can't use comgooglemaps://");
    }