【问题标题】:How to get the latitude & longitude of a touch location on mapView using Google map API in iOS如何在 iOS 中使用 Google 地图 API 在 mapView 上获取触摸位置的纬度和经度
【发布时间】:2014-09-05 12:28:00
【问题描述】:

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

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

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

此代码适用于 MKMapView,不适用于 Google 地图 API。

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

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

}

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

请帮帮我。

谢谢:)

【问题讨论】:

  • 无法触摸地图视图是什么意思?您需要识别地图上的手势还是仅获取触摸位置?
  • 我在该地图视图上添加了一个点击手势。但我无法触摸

标签: ios google-maps-api-3


【解决方案1】:

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

 -(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

mapView_.delegate = self;

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

希望有帮助!

【讨论】:

  • 请我再次需要你的帮助。如何根据该纬度和经度获取该位置的名称
  • @SRNayak 我没用过,但你可以用 GMSGeocoder。看看这个答案:stackoverflow.com/a/21819559/3874888
猜你喜欢
  • 2015-07-16
  • 2015-09-07
  • 1970-01-01
  • 1970-01-01
  • 2012-09-26
  • 2011-01-30
  • 1970-01-01
  • 2012-01-22
  • 2012-07-24
相关资源
最近更新 更多