1
votes

I am facing a problem from last 2 days. I am working on a Google map App using Google Map API. I get user's current location and drop a pin also. but my requirement is when user tap on any position of map i need that touch location latitude & longitude value.

How can i get it. Please don't say below code.

 CGPoint touchPoint = [gestureRecognizer locationInView:mapView_];
 coordinate = [mapView_ convertPoint:touchPoint toCoordinateFromView:mapView_];

This code is working for MKMapView not for Google map API.

And another issue is i am unable to touch the mapview also. I share my code below. Please someone help me.

This is my .h File

#import <UIKit/UIKit.h>
#import <GoogleMaps/GoogleMaps.h>
#import <MapKit/MapKit.h>

@interface ViewController : UIViewController <MKMapViewDelegate>
{
    CLLocationCoordinate2D coordinate;
}
@property (nonatomic, retain) CLLocationManager *locationManager;

@end

This is my .m File

@implementation ViewController
{
   GMSMapView *mapView_;
}

- (void)viewDidLoad
{
  [super viewDidLoad];

  self.locationManager = [[CLLocationManager alloc] init];
  self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
  self.locationManager.distanceFilter = kCLDistanceFilterNone;
  float latitude = self.locationManager.location.coordinate.latitude;
  float longitude = self.locationManager.location.coordinate.longitude;

  NSLog(@"*dLatitude : %f", latitude);
  NSLog(@"*dLongitude : %f",longitude);

  GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:latitude longitude:longitude zoom:8];
  mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
  mapView_.myLocationEnabled = YES;
  mapView_.settings.myLocationButton = YES;
  self.view = mapView_;

  // Creates a marker in the center of the map.
  GMSMarker *pinview = [[GMSMarker alloc] init];
  pinview.position = CLLocationCoordinate2DMake(latitude, longitude);
  //pinview.icon = [UIImage imageNamed:@"pin"];
  pinview.icon = [GMSMarker markerImageWithColor:[UIColor blueColor]];
  pinview.title = @"Bangalore";
  pinview.snippet = @"IntegraMicro";
  pinview.map = mapView_;

  UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc]initWithTarget:self
                                                                  action:@selector(handleGesture:)];
  tgr.numberOfTapsRequired = 1;
  [mapView_ addGestureRecognizer:tgr];

}

How to get touch location latitude and longitude value and how to add tap gesture on mapview.

Please help me.

Thanks :)

1
What do you mean with that you are unable to touch the mapview? Do you need to recognize gestures on the map or just to get touch location?Ruben R Aparicio
I am adding a tap gesture on that mapview. But i am unable to touchSoumya Ranjan

1 Answers

8
votes

If you want just to get the coordinate where the user touchs, you don't need the TapGesture. You could use the delegate method - (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate; of GMSMapView. Implement that function inside your code:

 -(void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate{
     double latitude = coordinate.latitude;
     double longitude = coordinate.longitude;

     //DO SOMETHING HERE WITH THAT INFO
 }
  • Swift 4
 func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
     let lat = coordinate.latitude
     let long = coordinate.longitude

     print("Latitude: \(lat), Longitude: \(long)")
 }

And add this in viewDidLoad after the Map is created:

mapView_.delegate = self;

The didTapAtCoordinate should be called every time the map is touched after this.

Hope that helps!