简体   繁体   中英

How can I get co-ordinates with drawing?

I am not using listener in my code then How can I get each co-ordinates points from drawing?

     function initMap() {
        var map = new google.maps.Map(document.getElementById('map'), {
        center: {lat: -34.397, lng: 150.644},
        zoom: 8
      });

        var drawingManager = new google.maps.drawing.DrawingManager({
        drawingMode: google.maps.drawing.OverlayType.MARKER,
        drawingControl: true,
        drawingControlOptions: {
          position: google.maps.ControlPosition.TOP_CENTER,
          drawingModes: ['polygon', 'polyline']
        },

        circleOptions: {
          fillColor: '#ffff00',
          fillOpacity: 1,
          strokeWeight: 5,
          clickable: false,
          editable: true,
          zIndex: 1
        }
      });
      drawingManager.setMap(map);
}

fiddle: https://jsfiddle.net/31aey9mk/

I've updated your fiddle here .

Basically, you need to pass an array of google.map.drawing.OverlayType s of the type you wish you use. In this case, I've added a marker and polygon.

Then, you need to add an event handler in the initMap() method for capturing when the drawing has been completed:

google.maps.event.addListener(drawingManager, 'overlaycomplete', function (event)
{
    if (event.type === 'marker') console.log('Lat: ' + event.overlay.position.lat() + ', Long: ' + event.overlay.position.lng())
    else console.log(event.overlay.getPath().b);
});

Which grabs the event and the shape/marker which was drawn.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM