簡體   English   中英

如何從谷歌地圖獲取當前縮放級別的ios?

[英]How to get current zoom level from google map for ios?

我使用具有數字值的方法相機,如下所示:

camera = [GMSCameraPosition cameraWithLatitude:37.35 longitude:-122.0 zoom:6];

我需要通過計時器自動重繪當前位置的地圖:

-(void)gmapRedraw{
    NSLog(@"gmapRedraw");
//    self.MapView.camera
    [locationManager startUpdatingLocation];
    NSLog(@"lat %f, lon %f", locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude);
    camera = [GMSCameraPosition cameraWithLatitude:locationManager.location.coordinate.latitude//37.36//-37.81319//-33.86
                                         longitude:locationManager.location.coordinate.longitude//-122.0//144.96298//151.20
                                              zoom:6];
    self.MapView.camera = camera;
}

-(void)timer{
    [NSTimer scheduledTimerWithTimeInterval:0.5
                                     target:self
                                   selector:@selector(gmapRedraw)
                                   userInfo:nil
                                    repeats:YES];
}

但如果我通過觸摸改變它,我怎么能得到當前的變焦?

好的,我找到解決方案,獲得當前縮放:

CGFloat currentZoom = self.MapView.camera.zoom;

然后在相機中使用它:

camera = [GMSCameraPosition cameraWithLatitude:locationManager.location.coordinate.latitude
                                     longitude:locationManager.location.coordinate.longitude
                                          zoom:currentZoom];
self.MapView.camera = camera;

如果要在用戶與地圖交互時檢測縮放級別。 我們可以使用Google Map委托方法

func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
    let zoom = mapView.camera.zoom
    print("map zoom is ",String(zoom))
}

別忘了添加GMSMapViewDelegate

你也可以使用animateToZoom。

self.mapView.animateToZoom(10)

swift 4.x的兩分錢:

let currZoom = self.googleMapView.camera.zoom

但是在第一個objC樣本中是浮點數,而不是CGFloat。 在Swift中,Float不是CGFloat。

暫無
暫無

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

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