首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

iOS Google Maps SDK 獲取地圖中心、定位當前位置、監聽地圖移動

     編輯:程式設計綜合問答

需求描述

使用 Google Maps SDK for iOS 實現:獲取地圖中心坐標、一鍵定位當前位置、監聽地圖移動(類似 MKMapView 的 regionDidChange)。

解決方案

1. 獲取地圖中心坐標


GMSMapView *mapView = ...;
CLLocationCoordinate2D center = mapView.camera.target;
NSLog(@"中心: %f, %f", center.latitude, center.longitude);

2. 恢復到當前位置(動畫)


if (mapView.myLocation) {
    CLLocationCoordinate2D current = mapView.myLocation.coordinate;
    float zoom = mapView.camera.zoom;
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithTarget:current zoom:zoom];
    [mapView animateToCameraPosition:camera];
}

3. 監聽地圖移動(委託方法)


// 設置委託
mapView.delegate = self;

// 實現委託方法
- (void)mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition *)position {
    NSLog(@"地圖移動,新中心: %f, %f", position.target.latitude, position.target.longitude);
    // 類似 MKMapView 的 regionDidChangeAnimated:
}

// 可選:移動結束時
- (void)mapView:(GMSMapView *)mapView idleAtCameraPosition:(GMSCameraPosition *)position {
    NSLog(@"地圖停穩");
}

關鍵類

  • GMSMapView:地圖視圖
  • GMSCameraPosition:相機位置(目標坐標、縮放、方位、傾斜)
  • GMSMapViewDelegate:委託協議

總結

Google Maps SDK 通過 camera.target 獲取中心,animateToCameraPosition: 定位,委託方法 didChangeCameraPosition: 監聽移動,API 設計清晰易用。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved