简体   繁体   中英

Set location on Google map on iOS

I'm basically implementing a functionality where after a user moves the map a button appears, and when the button is selected a new search happens in visible area. The problem is the visible area is not accurate, so when I try to zoom into it again it zooms out.

every time I click the button the function below is called and the map zooms out. I understand that not calling animate and removing the code will avoid the zoom, but I want to know why the zoom is happening, because this may cause problems elsewhere

@IBAction func updateSearchSelected(sender: UIButton) {
    let northEast = CLLocationCoordinate2D(latitude: mapView!.projection.visibleRegion().farRight.latitude, longitude: mapView!.projection.visibleRegion().farRight.longitude)
    let southWest = CLLocationCoordinate2D(latitude: mapView!.projection.visibleRegion().nearLeft.latitude, longitude: mapView!.projection.visibleRegion().nearLeft.longitude)
    print("northEast \(northEast)")
    print("southWest \(southWest)")
    let coordinateBounds = GMSCoordinateBounds(coordinate: northEast, coordinate: southWest)
    let cameraUpdate = GMSCameraUpdate.fit(coordinateBounds)
    mapView?.animate(with: cameraUpdate)
}

Here are the logs after clicking the button 3 times

northEast CLLocationCoordinate2D(latitude: 37.84227633765466, longitude: -122.38477706909181)
southWest CLLocationCoordinate2D(latitude: 37.674750313908689, longitude: -122.50391006469727)

northEast CLLocationCoordinate2D(latitude: 37.859492217765393, longitude: -122.37251903861761)
southWest CLLocationCoordinate2D(latitude: 37.657491381296779, longitude: -122.51616809517145)

northEast CLLocationCoordinate2D(latitude: 37.880245733530728, longitude: -122.35773839056492)
southWest CLLocationCoordinate2D(latitude: 37.636675535472314, longitude: -122.53094874322414)

You have to set the zoom level to what you want it to be when you call GMSCameraPosition.camera , for example:

let camera = GMSCameraPosition.camera(withLatitude: -33.8683,
                                      longitude: 151.2086,
                                      zoom: 16)

See: https://developers.google.com/maps/documentation/ios-sdk/views#using_gmscameraupdate

See the section titled Camera Position :

"The map view is modeled as a camera looking down on a flat plane. The position of the camera (and hence the rendering of the map) is specified by the following properties: latitude/longitude location, zoom, bearing and viewing angle."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM