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
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
}
}