Change Camera Zoom based on Radius Google Maps iOS SDK

前端 未结 6 830
既然无缘
既然无缘 2021-02-01 09:53

I am working on an app that displays certain markers based on a radius around your current location. The radius is between 100 - 5000 meters. I change the radius with an U

6条回答
  •  囚心锁ツ
    2021-02-01 10:17

    With the help of Saxon Druce,finally I did it.

    class MapUtil {
    
    class func translateCoordinate(coordinate: CLLocationCoordinate2D, metersLat: Double,metersLong: Double) -> (CLLocationCoordinate2D) {
        var tempCoord = coordinate
    
        let tempRegion = MKCoordinateRegionMakeWithDistance(coordinate, metersLat, metersLong)
        let tempSpan = tempRegion.span
    
        tempCoord.latitude = coordinate.latitude + tempSpan.latitudeDelta
        tempCoord.longitude = coordinate.longitude + tempSpan.longitudeDelta
    
        return tempCoord
    }
    
    class func setRadius(radius: Double,withCity city: CLLocationCoordinate2D,InMapView mapView: GMSMapView) {
    
        let range = MapUtil.translateCoordinate(city, metersLat: radius * 2, metersLong: radius * 2)
    
        let bounds = GMSCoordinateBounds(coordinate: city, coordinate: range)
    
        let update = GMSCameraUpdate.fitBounds(bounds, withPadding: 5.0)    // padding set to 5.0
    
        mapView.moveCamera(update)
    
        // location
        let marker = GMSMarker(position: city)
        marker.title = "title"
        marker.snippet = "snippet"
        marker.flat = true
        marker.map = mapView
    
        // draw circle
        let circle = GMSCircle(position: city, radius: radius)
        circle.map = mapView
        circle.fillColor = UIColor(red:0.09, green:0.6, blue:0.41, alpha:0.5)
    
        mapView.animateToLocation(city) // animate to center
    }
    

    }enter image description here

提交回复
热议问题