How to draw a radius around a map marker using the Google Maps SDK (Swift / iOS)?

There are two ways that a user can place a marker on a map inside the application through the UISearchBar above the map (not yet completed) and long press on the map where they would like the marker to be displayed. I use a global variable for a marker because the user is not allowed to set only one marker on the map. Whenever a marker is created, I would like to draw a radius (circle) around the marker. Here is my code:

var mapMarker = GMSMarker() ... //This function dectects a long press on the map and places a marker at the coordinates of the long press. func mapView(mapView: GMSMapView!, didLongPressAtCoordinate coordinate: CLLocationCoordinate2D) { //Set variable to latitude of didLongPressAtCoordinate var latitude = coordinate.latitude //Set variable to longitude of didLongPressAtCoordinate var longitude = coordinate.longitude //Feed position to mapMarker mapMarker.position = CLLocationCoordinate2DMake(latitude, longitude) //Define attributes of the mapMarker. mapMarker.icon = UIImage(named: "mapmarkericon") //Set icon anchor point mapMarker.groundAnchor = CGPoint(x: 0.5, y: 0.5) //Enable animation on mapMarker mapMarker.appearAnimation = kGMSMarkerAnimationPop //Display the mapMarker on the mapView. mapMarker.map = mapView drawCircle(coordinate) } func drawCircle(position: CLLocationCoordinate2D) { //var latitude = position.latitude //var longitude = position.longitude //var circleCenter = CLLocationCoordinate2DMake(latitude, longitude) var circle = GMSCircle(position: position, radius: 3000) circle.strokeColor = UIColor.blueColor() circle.fillColor = UIColor(red: 0, green: 0, blue: 0.35, alpha: 0.05) circle.map = mapView } 

Admittedly, this is my first iOS / Swift app. I wondered if I passed the coordinates from didLongPressAtCoordinate to the drawCircle () function, but apparently I'm doing something wrong. I searched all day for help, but just found Android and Google Maps API v3. Thanks!

EDIT My code works, but the radius does not scale, and an icon appears above the icon. When I zoomed in on the map, I was able to see the radius. There are three problems:

  • The icon does not scale depending on the zoom level of the map.
  • The radius does not scale based on the map zoom level.
  • If the marker position is updated, the radius does not move with it. Instead, a new radius is drawn ... so there are two radii on the map.
+6
source share
2 answers

These are three separate questions, but in any case:

1.

GMSMarker always drawn with the same size (in pixels) regardless of the scale of the map. For an image that scales with a map, view GMSGroundOverlay .

2.

You defined the radius as 3000 meters, so it should always be that size in meters and thus scale with the map scale. Or do you want it to be a fixed size in pixels, so increase it in meters while zooming out?

3.

You will need to save the circle as a member (for example, with a marker) and update its position every time you move, and not always create a new circle.

+2
source

Here is a quick code to draw a circle on a specified radius in a mile

 let circleCenter : CLLocationCoordinate2D = CLLocationCoordinate2DMake(centerLattitude, centerLongitude); let circ = GMSCircle(position: circleCenter, radius: distanceInMile * 1609.34) circ.fillColor = UIColor(red: 0.0, green: 0.7, blue: 0, alpha: 0.1) circ.strokeColor = UIColor(red: 255/255, green: 153/255, blue: 51/255, alpha: 0.5) circ.strokeWidth = 2.5; circ.map = self.googleMapView; 
+5
source

Source: https://habr.com/ru/post/983684/


All Articles