javascript - how to access variables in google map api to use in every function -
i having problem utilizing autocomplete of google map search. here trying
function initialize() { directionsdisplay = new google.maps.directionsrenderer(); //add map, type of map var map = new google.maps.map(document.getelementbyid('map-canvas'), { zoom: 6, center: haight, maptypeid: google.maps.maptypeid.roadmap }); autocomplete = new google.maps.places.autocomplete(document.getelementbyid('address')); google.maps.event.addlistener(autocomplete, 'place_changed', function () { var place = autocomplete.getplace(); var lat = place.geometry.location.lat(); var lng = place.geometry.location.lng(); }); alert(lat); }
if try generate alert out of addlistner function not working, working inside addlistner. there way can access lat,lng out of addlistner?
and can use variable lat,lng outside of initialize function in runtime?
edit
var markers = new google.maps.marker({ map: map, position: new google.maps.latlng(lat, lng), title: 'point of origin' }); // add circle overlay , bind marker var circle = new google.maps.circle({ map: map, radius: 1000*10, // metres fillcolor: '#aa0000' }); circle.bindto('center', markers, 'position');
i want use lat,lng variable create marker generating circle particular latitude , longitude.
try that:
var map; function initialize() { directionsdisplay = new google.maps.directionsrenderer(); //add map, type of map map = new google.maps.map(document.getelementbyid('map-canvas'), { zoom: 6, center: haight, maptypeid: google.maps.maptypeid.roadmap }); autocomplete = new google.maps.places.autocomplete(document.getelementbyid('address')); google.maps.event.addlistener(autocomplete, 'place_changed', function() { var place = autocomplete.getplace(); var lat = place.geometry.location.lat(); var lng = place.geometry.location.lng(); markerandcircle(lat, lng); }); } function markerandcircle(lat, lng) { var marker = new google.maps.marker({ map: map, position: new google.maps.latlng(lat, lng), title: 'point of origin' }); // add circle overlay , bind marker var circle = new google.maps.circle({ map: map, center: new google.maps.latlng(lat, lng), radius: 1000 * 10, // metres fillcolor: '#aa0000' }); }
this untested. if doesn't work, watch javascript console errors , report here.
note: map
variable declared outside of initialize
function need in markerandcircle
function.
Comments
Post a Comment