如何获得纬度在iOS中使用Google Map API在mapView上触摸位置的经度 [英] How to get the latitude & longitude of a touch location on mapView using Google map API in iOS

查看:63
本文介绍了如何获得纬度在iOS中使用Google Map API在mapView上触摸位置的经度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近2天来我都遇到了问题.我正在使用Google Map API开发Google Map App.我得到了用户的当前位置,并且也下了一个图钉.但我的要求是,当用户点击地图上的任意位置时,我需要该触摸位置的纬度&经度值.

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_];

此代码适用于MKMapView,不适用于Google map API.

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.

这是我的.h文件

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

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

@end

这是我的.m文件

@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.

请帮助我.

谢谢:)

推荐答案

如果只想获取用户触摸的坐标,则不需要TapGesture.您可以使用GMSMapView的委托方法- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate;.在代码中实现该功能:

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
 }

  • 雨燕4
  •  func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
         let lat = coordinate.latitude
         let long = coordinate.longitude
    
         print("Latitude: \(lat), Longitude: \(long)")
     }
    

    并在创建地图后将其添加到viewDidLoad中:

    And add this in viewDidLoad after the Map is created:

    mapView_.delegate = self;
    

    此后每次触摸地图都应调用didTapAtCoordinate.

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

    希望有帮助!

    这篇关于如何获得纬度在iOS中使用Google Map API在mapView上触摸位置的经度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆