ios - 如何在 iOS 中使用 Google map API 在 mapView 上获取触摸位置的纬度和经度

标签 ios google-maps-api-3

我在过去 2 天遇到了一个问题。我正在使用谷歌地图 API 开发谷歌地图应用程序。我得到用户的当前位置并放下一个别针。但我的要求是当用户点击 map 的任何位置时,我需要触摸位置的纬度和经度值。

我怎么才能得到它。请不要说下面的代码。

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

此代码适用于 MKMapView 而不是 Google map API。

另一个问题是我也无法触摸 map View 。我在下面分享我的代码。请有人帮助我。

这是我的 .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];

}

如何获取触摸位置的纬度和经度值以及如何在 map View 上添加点击手势。

请帮我。

谢谢 :)

最佳答案

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

 -(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)")
     }
    

    并将其添加到 viewDidLoad创建 map 后:
    mapView_.delegate = self;
    
    didTapAtCoordinate在此之后每次触摸 map 时都应该调用它。

    希望有帮助!

    关于ios - 如何在 iOS 中使用 Google map API 在 mapView 上获取触摸位置的纬度和经度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25685918/

    相关文章:

    ios - XIB 中的 UITabelViewCell 将被实例化更多次

    javascript - Google Maps API v3 延迟从 XML 中删除标记?

    javascript - 使用 Google Maps Javascript Api 检测 map 加载时的查询限制消息

    javascript - 在移动 map 中心之前无法在 Google map v3 中看到绘图多边形

    ios - 从 Google Analytics for iOS v2 获取特定的 iOS 设备名称

    iOS/Swift : Imported . 框架不完整或显示错误类

    ios - 排序数组导致错误的数据显示到单元格中

    ios - 控制显示的 y 轴标签?

    javascript - 我需要获取标记的纬度和经度

    javascript - Google Maps API - 一次仅打开一个信息窗口