How to fit bounds for coordinate array with google maps sdk for iOS?

为君一笑 提交于 2019-12-20 08:57:33

问题


How to fit bounds for coordinate array with google maps sdk for iOS? I need to zoom map for 4 visible markers.


回答1:


Here's my solution for this problem. Building a GMSCoordinateBounds object by multiple coordinates.

- (void)focusMapToShowAllMarkers
{       
    CLLocationCoordinate2D myLocation = ((GMSMarker *)_markers.firstObject).position;
    GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithCoordinate:myLocation coordinate:myLocation];

    for (GMSMarker *marker in _markers)
        bounds = [bounds includingCoordinate:marker.position];

    [_mapView animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:15.0f]];
}

Updated answer: Since GMSMapView markers property is deprecated, you should save all markers in your own array.

updated swift 3 answer:

    func focusMapToShowAllMarkers() {
        let firstLocation = (markers.first as GMSMarker).position
        var bounds = GMSCoordinateBoundsWithCoordinate(firstLocation, coordinate: firstLocation)

        for marker in markers {
            bounds = bounds.includingCoordinate(marker.position)
        }
        let update = GMSCameraUpdate.fitBounds(bounds, withPadding: CGFloat(15))
        self.mapView.animate(cameraUpdate: update)
  }



回答2:


Swift 3.0 version of Lirik's answer:

func focusMapToShowAllMarkers() {
    let myLocation: CLLocationCoordinate2D = self.markers.first!.position
    var bounds: GMSCoordinateBounds = GMSCoordinateBounds(coordinate: myLocation, coordinate: myLocation)

    for marker in self.markers {
        bounds = bounds.includingCoordinate(marker.position)
        self.mapView.animate(with: GMSCameraUpdate.fit(bounds, withPadding: 15.0))
    }
}

And here's my own way:

func focusMapToShowMarkers(markers: [GMSMarker]) {

    guard let currentUserLocation = self.locationManager.location?.coordinate else {
        return
    }

    var bounds: GMSCoordinateBounds = GMSCoordinateBounds(coordinate: currentUserLocation,
                                                          coordinate: currentUserLocation)

    _ = markers.map {
        bounds = bounds.includingCoordinate($0.position)
        self.mapView.animate(with: GMSCameraUpdate.fit(bounds, withPadding: 15.0))
    }
}

And you can call my function above like so:

self.focusMapToShowMarkers(markers: [self.myLocationMarker, currentPokemonMarker])




回答3:


For time being, Google has finally implemented the GMSCoordinateBounds, you can make use of it with GMSCameraUpdate.

For details, please check the official reference.




回答4:


Swift 5 version of Lirik's answer:

    func focusMapToShowAllMarkers() {

        if arrMarkers.count > 0 {
            let firstLocation = (arrMarkers.first!).position
            var bounds = GMSCoordinateBounds(coordinate: firstLocation, coordinate: firstLocation)

            for marker in arrMarkers {
              bounds = bounds.includingCoordinate(marker.position)
            }

           let update = GMSCameraUpdate.fit(bounds, withPadding: CGFloat(15))
           self.mapView.animate(with: update)
        }
    }


来源:https://stackoverflow.com/questions/15134511/how-to-fit-bounds-for-coordinate-array-with-google-maps-sdk-for-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!