user836026
user836026

Reputation: 11350

How to get lat/lang/radius of center of circle that user draw

I'm using this exmaple from https://developers.google.com/maps/documentation/javascript/examples/drawing-tools to enable user to choose a point and enable user to draw a circle around it.

However, I need also to:

Upvotes: 8

Views: 11998

Answers (2)

anand krish
anand krish

Reputation: 4415

GoogleMap map;

 // Add a circle in Sydney
 Circle circle = map.addCircle(new CircleOptions()
     .center(new LatLng(-33.87365, 151.20689))
     .radius(10000)
     .strokeColor(Color.RED)
     .fillColor(Color.BLUE));
<-- to get radius of any location in map, use this code. It shows the radius value from center of map to x and y coordinates.-->
 Toast.makeText(app.getBaseContext(),***[circle.getRadius()][1]***, 
                Toast.LENGTH_SHORT).show();

Upvotes: 0

Steve Jansen
Steve Jansen

Reputation: 9494

See http://jsfiddle.net/stevejansen/2HpA6

(function () {
    var circle;

    function initialize() {
        var mapOptions = {
            center: new google.maps.LatLng(-34.397, 150.644),
            zoom: 8
        };


        var map = new google.maps.Map(document.getElementById('map-canvas'),
        mapOptions);

        var drawingManager = new google.maps.drawing.DrawingManager({
            drawingMode: google.maps.drawing.OverlayType.MARKER,
            drawingControl: true,
            drawingControlOptions: {
                position: google.maps.ControlPosition.TOP_CENTER,
                drawingModes: [
                google.maps.drawing.OverlayType.CIRCLE]
            },
            circleOptions: {
                fillColor: '#ffff00',
                fillOpacity: 1,
                strokeWeight: 5,
                clickable: false,
                editable: true,
                zIndex: 1
            }
        });
        drawingManager.setMap(map);
        google.maps.event.addListener(drawingManager, 'circlecomplete', onCircleComplete);
    }

    function onCircleComplete(shape) {
        if (shape == null || (!(shape instanceof google.maps.Circle))) return;

        if (circle != null) {
            circle.setMap(null);
            circle = null;
        }

        circle = shape;
        console.log('radius', circle.getRadius());
        console.log('lat', circle.getCenter().lat());
        console.log('lng', circle.getCenter().lng());
    }

    google.maps.event.addDomListener(window, 'load', initialize);
})();

Upvotes: 16

Related Questions