google maps - Global variable javascript not working appropriately -
i working on website in trying incorporate google maps. page fetches values database using php. passes 1 of column values javascript function plots using google maps. now, want display plots of values in single map.
i attaching javascript function , php call. here code:
<script type="text/javascript"> var myoptions; var map; var geocoder; var first_call = 0; function map_function(addr){ // define addresses want map. var clubs = addr; // create new geocoder if (first_call == 0){ var geocoder = new google.maps.geocoder(); } // locate address using geocoder. geocoder.geocode( { "address": clubs }, function(results, status) { // if geocoding successful if (status == google.maps.geocoderstatus.ok) { if (first_call == 0){ // create google map @ latitude/longitude returned geocoder. // create once alert("initializing map"); var myoptions = { zoom: 16, center: results[0].geometry.location, maptypeid: google.maps.maptypeid.roadmap }; //initialise once var map = new google.maps.map(document.getelementbyid("map"), myoptions); } // add marker @ address. alert("plotting address"); var marker = new google.maps.marker({ map: map, position: results[0].geometry.location }); } else { try { console.error("geocode not successful following reason: " + status); } catch(e) {} } }); first_call = first_call + 1; alert (first_call); } </script> <?php while($row = mysqli_fetch_array($result)){ $addr = $row['address']; echo '<script type="text/javascript">map_function("{$addr}");</script>'; ?>
i know map along options should initialized once. keeping track of function call using first_call , making sure map initialized on first call. besides, declaring map, options , geocoder global variables. unfortunately, not output. new javascript , dont know going wrong.
anxiously awaiting solution.
i don't know php code javascript code has several errors.
first of define mapoptions
, map
, , geocoder
global , redefine each of them locally. so, var
in front of variables deleted in function map_function
.
the next 1 prevents map initialized @ update of first_call
variable. because geocode()
asynchronous function, first_call
set 1 , increased further before code reach check if (first_call == 0){
. map never initialized.
below changed code test code without php:
<script type="text/javascript"> var myoptions; var map; var geocoder; var first_call = 0; function map_function(addr){ // define addresses want map. var clubs = addr; // create new geocoder if (first_call == 0){ geocoder = new google.maps.geocoder(); // var deleted } // locate address using geocoder. geocoder.geocode( { "address": clubs }, function(results, status) { // if geocoding successful if (status == google.maps.geocoderstatus.ok) { if (first_call == 0){ // create google map @ latitude/longitude returned geocoder. // create once console.log("initializing map"); myoptions = { zoom: 16, center: results[0].geometry.location, maptypeid: google.maps.maptypeid.roadmap }; //initialise once map = new google.maps.map(document.getelementbyid("map"), myoptions); } // add marker @ address. console.log("plotting address"); var marker = new google.maps.marker({ map: map, position: results[0].geometry.location }); first_call = first_call + 1; console.log (first_call); } else { try { console.error("geocode not successful following reason: " + status); } catch(e) {} } }); // wrong place because of async call: map never initialized //first_call = first_call + 1; //console.log (first_call); } var addresses = ['paris', 'london', 'berlin']; (var = 0; < addresses.length; i++) { map_function(addresses[i]); } </script>
Comments
Post a Comment