samedi 18 avril 2015

Google Maps Java API - How to assign latitude and longitude to variable outside of Drawing Manager function

I'm attempting to pass the latitude and longitude to the global variables "lat" and "lng" in order to use them in an AJAX get request. How do I us the variables outside of the drawing manager function? As of now, when I attempt to use the variables "lat", "lng", and "radius" they are undefined.



var lat;
var lng;
var radius;
var circle;

function initialize() {

var mapOptions = {
center: new google.maps.LatLng(47.3850000, 39.2430556),
zoom: 6
};

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: '#017ad4',
fillOpacity: .35,
strokeOpacity: .80,
strokeColor: '#02528d',
strokeWeight: 2,
clickable: false,
editable: false,
zIndex: 1
}
});

circles = new Array();


drawingManager.setMap(map);

google.maps.event.addListener(drawingManager, 'circlecomplete', function(circle) {
//exit the circle drawing mode
drawingManager.setDrawingMode(null);

//add the latest circle to the circles array
circles.push(circle);
//pop the previous drawn circle
circles.reverse();

//pop the previous circle and set its map handle to null, so that only last drawn circle is shown
while(circles[1])
{
circles.pop().setMap(null);
}
//get lat, long, and radius
var lat = circle.getCenter().lat();
var lng = circle.getCenter().lng();
var radius = circle.getRadius();

//test
console.log("latitude: " + lat );
console.log("longitude: " + lng);
console.log("radius: " + radius);
});
}

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

Aucun commentaire:

Enregistrer un commentaire