Sonic Master
Sonic Master

Reputation: 1246

Add resizable circle radius on GoogleMaps API for iOS

I am trying to make a circle-like-radius area that user can resize and I can get the number of radius of my current location.

================

tl;dr Edited

I want to make this as an iOS app with swift.

=================

Right now, I already created map that listening to current location. Using CLLocationManager, I am observing its update location and the map will show my location.

import UIKit
import GoogleMaps

class ViewController: UIViewController, CLLocationManagerDelegate {
    @IBOutlet var mapView: GMSMapView!

    // Current Location Components
    var locationManager = CLLocationManager()
    var didFindMyLocation = false

    override func viewDidLoad() {
        super.viewDidLoad()

        // Current Location Delegate
        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()

        // Observer for update location (Moving)
        mapView.addObserver(self, forKeyPath: "myLocation", options: NSKeyValueObservingOptions.New, context: nil)
    }

    // Core Location Location Manager Delegate
    func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        if status == CLAuthorizationStatus.AuthorizedWhenInUse {
            mapView.myLocationEnabled = true
        }
    }

    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        if !didFindMyLocation {
            let myLocation: CLLocation = change![NSKeyValueChangeNewKey] as! CLLocation
            mapView.camera = GMSCameraPosition.cameraWithTarget(myLocation.coordinate, zoom: 18.0)
            mapView.settings.myLocationButton = true

            didFindMyLocation = true
        }
    }

Right now I want to make a circle that resizable. Someone already created that in android here. The circle/radius could be shrink or expand by gesture, and I can get the radius of that circle from its center like this question. This is what I want for my app.

So, what should I use for this radius-circle-resizable? I am really appreciate every answers and ideas. Thank you!

Upvotes: 0

Views: 3109

Answers (1)

derdida
derdida

Reputation: 14904

I would create a polygon/circle as an Overlay.

https://developers.google.com/maps/documentation/ios-sdk/reference/interface_g_m_s_circle

For example:

let circle = GMSCircle()
circle.radius = 130 // Meters
circle.fillColor = UIColor.redColor()
circle.position = CLLOCATION.coordinate // Your CLLocationCoordinate2D  position
circle.strokeWidth = 5;
circle.strokeColor = UIColor.blackColor()
circle.map = mapView; // Add it to the map

Do resize it, i would overlay the whole screen with an UIView, and enable an UIPanGestureRecognizer

Check out the example here: Resizable Centered Circular UIView

Upvotes: 1

Related Questions