如何在ios中使用谷歌地图实现位置搜索?

时间:2014-11-11 15:03:50

标签: ios objective-c google-maps

我正在构建一个iOS应用程序。我正在使用故事板来构建屏幕,我已经在我的项目中集成了谷歌地图。

我想获得点击地图的位置,并希望在Airbnb app中的标签中显示该位置。我无法做到这一点,有人可以提供帮助。

这是我的代码:

- (void)viewDidLoad {
   [super viewDidLoad];
   GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
                                                            longitude:151.20
                                                                 zoom:6];
   mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];

   mapView_.myLocationEnabled = YES;
   self.view = mapView_;
   GMSMarker *marker = [[GMSMarker alloc] init];
   marker.position = CLLocationCoordinate2DMake(-33.86, 151.20);
   marker.title = @"Sydney";
   marker.snippet = @"Australia";
   marker.map = mapView_;
}

非常感谢帮助!

1 个答案:

答案 0 :(得分:1)

您必须使用GMSMapViewDelegate协议。准确地说,方法mapView:didTapAtCoordinate:将执行您所需的操作,并在地图上检测点击并为您提供点击坐标。

要使用此方法,请在viewDidLoad中添加以下行:

mapView_.delegate = self;

然后在同一个类中实现方法:

- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate{
   // coordinate contains your coordinate :)
   NSLog(@"did tap at coordinate: (%f, %f)", coordinate.latitude, coordinate.longitude);
} 

更新:要添加标记,您可以执行以下操作:

- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate{
   // coordinate contains your coordinate :)
   NSLog(@"did tap at coordinate: (%f, %f)", coordinate.latitude, coordinate.longitude);

   // Create the marker and add it to the map
   CLLocationCoordinate2D position = CLLocationCoordinate2DMake(coordinate.latitude, coordinate.longitude);
   GMSMarker *marker = [GMSMarker markerWithPosition:position];
   marker.appearAnimation = kGMSMarkerAnimationPop;
   marker.map = mapView_;

   // Zoom into the current location
   GMSCameraPosition *cameraPosition = [GMSCameraPosition cameraWithTarget:position zoom:15.0];
   [mapView_ animateToCameraPosition:cameraPosition];
}