【问题标题】:How to Get Center Location Latitude And Longitude of Google map in iOS?如何在iOS中获取谷歌地图的中心位置经纬度?
【发布时间】:2015-03-24 20:09:26
【问题描述】:

我是 iOS 开发的新手,也是地图工具包的新手可能就像在这里我为它附加图像。

这里我的当前位置是孟买然后它显示我的 mapView 就像

我想知道中心位置的纬度和经度,就像用户滚动 Mapview 时一样,然后我想知道我的图像指示点的纬度和经度。这里的图像显示我想知道这个点的纬度和经度这里我的图像指示在我的 MapView 的中心位置。

【问题讨论】:

  • 你得到答案了吗?你解决了这个问题,那么请把答案放在这里。

标签: ios objective-c google-maps google-maps-sdk-ios


【解决方案1】:

要在地图移动时获取位置,您需要处理 mapView:didChangeCameraPosition:

然后在该方法中,您可以访问mapView.camera.target 以获取地图的当前中心。

- (void) mapView: (GMSMapView *)mapView 
     didChangeCameraPosition: (GMSCameraPosition *)position 
{
    double latitude = mapView.camera.target.latitude;
    double longitude = mapView.camera.target.longitude;

    // now do something with latitude and longitude
}

请注意,要处理委托方法,您需要实现GMSMapViewDelegate protocol。设置地图时,您需要将地图的委托设置为处理协议的类(通常为self),例如:

mapView.delegate = self;

【讨论】:

  • 请问你能给我任何资源吗?
  • 这里我没有更改位置然后它工作?在这里我只移动我的地图视图。那么它成功了吗?
【解决方案2】:

使用以下代码获取地图视图经纬度的中心。 每当您自动向任何方向滚动地图视图时,都会调用 regionWillChangeAnimated: 方法。它是可选委托方法之一。

- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
{
     CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(mkmapView.centerCoordinate.latitude, mkmapView.centerCoordinate.longitude);
     CGFloat txtLatitude = coord.latitude;
     CGFloat txtLongitude = coord.longitude;
     NSLog(@"Latitude is===>>>%f %f",txtLongitude);
     NSLog(@"Longitude is===>>>%f %f",txtLongitude);
 }

如果您点击地图视图,则需要 LatitudeLongitude 使用以下代码。 这里_mkMapView是Mapview的出口。

- (void)viewDidLoad
{
     UITapGestureRecognizer *tapGesture= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backgroundTapped:)];
     [self.view addGestureRecognizer:tapGesture];
}

- (void)backgroundTapped:(UITapGestureRecognizer *)gesture
{
    CGPoint touchPoint = [gesture locationInView:_mkMapView];
    CLLocationCoordinate2D location =[_mkMapView convertPoint:touchPoint toCoordinateFromView:_mkMapView];
}

可能对你有帮助。

【讨论】:

    【解决方案3】:
    - (void)mapView:(GMSMapView *)pMapView didChangeCameraPosition:(GMSCameraPosition *)position {
    mapViewRef.delegate = self;
    double latitude = mapViewRef.camera.target.latitude;
    double longitude = mapViewRef.camera.target.longitude;
    CLLocationCoordinate2D addressCoordinates = CLLocationCoordinate2DMake(latitude,longitude);
    GMSGeocoder* coder = [[GMSGeocoder alloc] init];
    [coder reverseGeocodeCoordinate:addressCoordinates completionHandler:^(GMSReverseGeocodeResponse *results, NSError *error) {
        if (error) {
            // NSLog(@"Error %@", error.description);
        } else {
            GMSAddress* address = [results firstResult];
            NSArray *arr = [address valueForKey:@"lines"];
            NSString *str1 = [NSString stringWithFormat:@"%lu",(unsigned long)[arr count]];
            if ([str1 isEqualToString:@"0"]) {
                self.txtPlaceSearch.text = @"";
            }
            else if ([str1 isEqualToString:@"1"]) {
                NSString *str2 = [arr objectAtIndex:0];
                self.txtPlaceSearch.text = str2;
            }
            else if ([str1 isEqualToString:@"2"]) {
                NSString *str2 = [arr objectAtIndex:0];
                NSString *str3 = [arr objectAtIndex:1];
                self.txtPlaceSearch.text = [NSString stringWithFormat:@"%@,%@",str2,str3];
            }
        }
      }];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-30
      • 1970-01-01
      • 2019-03-29
      • 1970-01-01
      • 2019-09-27
      相关资源
      最近更新 更多