google maps - newbie javascript scope problem -


i know bit scope is, new javascript , have hit full stop in code. here code use.

function initialize() {       var mapoptions = {         center: new google.maps.latlng(37.775057,-122.419624),         zoom: 8,         maptypeid: google.maps.maptypeid.roadmap       };       var map = new google.maps.map(document.getelementbyid("map-canvas"),           mapoptions);       } google.maps.event.adddomlistener(window, 'load', initialize); function clicked(id) {   $.ajax({     type: "get",     url: 'response.php',     data: {id: id},     success: function (lats) {       var obj = $.parsejson(lats);       var line = [];       (var = obj.length - 1; >= 0; i--) {         line.push(new google.maps.latlng(obj[i].latitude, obj[i].longitude));       };       var polypath = new google.maps.polyline({     path: line,     strokecolor: "#ff0000",     strokeopacity: 1.0,     strokeweight: 2   });       polypath.setmap(map);       console.log(polypath);     }   });   return false; } 

needless google maps. problem due scope, javascript cant access map variable required draw polylines. highly appreciated!

because map declared inside function initialize, clicked can't access need declare outside initialize like

var map = null; function initialize() {       var mapoptions = {         center: new google.maps.latlng(37.775057,-122.419624),         zoom: 8,         maptypeid: google.maps.maptypeid.roadmap       };       map = new google.maps.map(document.getelementbyid("map-canvas"),           mapoptions);     } google.maps.event.adddomlistener(window, 'load', initialize); function clicked(id) {   $.ajax({     type: "get",     url: 'response.php',     data: {id: id},     success: function (lats) {       var obj = $.parsejson(lats);       var line = [];       (var = obj.length - 1; >= 0; i--) {         line.push(new google.maps.latlng(obj[i].latitude, obj[i].longitude));       };       var polypath = new google.maps.polyline({     path: line,     strokecolor: "#ff0000",     strokeopacity: 1.0,     strokeweight: 2   });       polypath.setmap(map);       console.log(polypath);     }   });   return false; } 

now clicked can access because it's outside initialize scope


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -