簡體   English   中英

如何在iOS中獲取谷歌地圖的中心位置緯度和經度?

[英]How to Get Center Location Latitude And Longitude of Google map in iOS?

我是 iOS 開發的新手和地圖工具包的新手我想知道 MapView 中心的緯度和經度每次就像當用戶從一個位置滾動到另一個位置然后我想顯示我的 GMSMapView 的中心緯度和經度如何它可能像我在這里附上圖片。

這里我的當前位置是孟買然后它顯示我的地圖視圖就像

在此處輸入圖片說明

我想知道中心位置的緯度和經度,就像用戶滾動地圖視圖一樣,然后我想知道我的圖像指示點的緯度和經度。 此處圖像顯示為我想知道該點的緯度和經度此處我指示的圖像位於我的 MapView 的中心位置。

在此處輸入圖片說明

要在移動地圖時獲取位置,您需要處理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協議 設置地圖時,您需要將地圖的委托設置為處理協議的類(通常是self ),例如:

mapView.delegate = self;

使用下面的代碼獲取地圖視圖的中心緯度和經度。 當您在任何方向上自動滾動地圖視圖時 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);
 }

如果您點擊地圖視圖,則需要使用以下代碼的緯度經度 這里_mkMapViewMapview的出口。

- (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];
}

可能對你有幫助。

- (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];
        }
    }
  }];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM