javascript - Google maps marker parsed using ajax json -
can tell me why can't add multiple markers parsed via json inside , ajax request? i've build array should receive json array contains information required display markers on google map. strangely not work @ all! @ rate if try print in console length of array can see array looping correctly
var map; function initialize() { var mapoptions = { zoom: 15, center: new google.maps.latlng(41.90832, 12.52407) }; map = new google.maps.map(document.getelementbyid('map-canvas'), mapoptions); } google.maps.event.adddomlistener(window, 'load', initialize); var xmlhttp = new xmlhttprequest(); var url = "json/prova.json"; xmlhttp.onreadystatechange=function() { if (xmlhttp.readystate == 4 && xmlhttp.status == 200) { myfunction(xmlhttp.responsetext); } } xmlhttp.open("get", url, true); xmlhttp.send(); var infowindow = []; var marker = []; function myfunction(response) { var arr = json.parse(response); for(var = 0; < arr.length; i++) { console.log(arr.length); console.log(infowindow.length); (function(i){ //new line infowindow[i] = new google.maps.infowindow({ content: arr[i].title }); marker[i] = new google.maps.marker({ title: arr[i].title, animation: google.maps.animation.drop, //animation: google.maps.animation.bounce, position: new google.maps.latlng(arr[i].latitude,arr[i].longitude) }); google.maps.event.addlistener(marker[i], 'click', function() { infowindow[i].open(map,this); }); })(i); //new line } }
html, body, #map-canvas { height: 100%; margin: 0px; padding: 0px }
<!doctype html> <html> <head> <title>simple map</title> <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> <meta charset="utf-8"> <script src="http://maps.google.com/maps/api/js?signed_in=true&key=aizasyd3xz7-z7u_xzziaolx9khhtfsld8vd0k4"></script> <script src="js/prova_simple.js"></script> </head> <body> <h1>customers</h1> <div id="map-canvas"></div> </body> </html>
you're not placing marker in map.
if go markers documentation can see several ways it.
i recommend case inside for
function:
marker[i].setmap(map)
edit:
for(var = 0; < arr.length; i++) { console.log(arr.length); console.log(infowindow.length); (function(i){ //new line infowindow[i] = new google.maps.infowindow({ content: arr[i].title }); marker[i] = new google.maps.marker({ title: arr[i].title, animation: google.maps.animation.drop, //animation: google.maps.animation.bounce, position: new google.maps.latlng(arr[i].latitude,arr[i].longitude) }); marker[i].setmap(map) //<----- here google.maps.event.addlistener(marker[i], 'click', function() { infowindow[i].open(map,this); }); })(i); //new line }
Comments
Post a Comment