The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Web Design
> JavaScript Development
|
Javascript shows only last markers
Discuss Javascript shows only last markers in the JavaScript Development forum on Dev Shed. Javascript shows only last markers JavaScript Development forum discussing JavaScript and DHTML, AJAX, and issues such as coding cross-browser JavaScript.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 29th, 2012, 12:38 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 1
Time spent in forums: 23 m
Reputation Power: 0
|
|
|
Javascript shows only last markers
The following code grabs some JSON data from /home.json URL which contain 5 microposts by 6 users. However, only the last 4 markers are shown (not even the 5th !!). I have spend 2 days to find the bug but unfortunately I can't really get why this dosen't work. If anyone could help me I would really appreciate it!
javascript Code:
Original
- javascript Code |
|
|
|
var GMAPS = window.GMAPS || {}; GMAPS.mainMap = function() { var map; var infowindow = new google.maps.InfoWindow(); var jsonObject = {}; function addMarkers() { var xhr = new XMLHttpRequest(); xhr.open( "GET", "/home.json", true ); xhr.onreadystatechange = function () { if ( xhr.readyState == 4 && xhr.status == 200 ) { jsonObject = JSON.parse( xhr.responseText ); for (var i=0; i<jsonObject.length; i++) { for (var j=0; j<jsonObject[i].microposts.length; j++) { (function(Name, Title, Content, Latitude, Longitude) { return function() { //alert(Name + "\n" + Title + "\n" + Content + "\n" + Latitude + "\n" + Longitude + "\n"); //<-- this works! var position = new google.maps.LatLng(Latitude, Longitude); var contentString = "<h4>" + Name.bold() + "</h4>" + "<br />" + Title.bold() + "<br />" + Content; var marker = new google.maps.Marker({ position: position, title: Title, map: map }); google.maps.event.addListener(marker, 'click', (function(marker, contentString) { return function() { infowindow.setContent(contentString); infowindow.open(map, marker); } })(marker, contentString)); }; })(jsonObject[i].name, jsonObject[i].microposts[j].title, jsonObject[i].microposts[j].content, jsonObject[i].microposts[j].lat, jsonObject[i].microposts[j].lon)(); } } } }; xhr.send(null); } function createMap() { map = new google.maps.Map(document.getElementById('main_map'), { zoom: 10, panControl: true, zoomControl: true, zoomControlOptions: { style: google.maps.ZoomControlStyle.SMALL }, mapTypeControl: true, scaleControl: true, streetViewControl: true, overviewMapControl: true, scrollwheel: false, center: new google.maps.LatLng(37.975327,23.728701), mapTypeId: google.maps.MapTypeId.ROADMAP }); } function initAddress() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition); } else { var position = new google.maps.LatLng(0, 0); map.setCenter(position); } } function showPosition(position) { var position = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); map.setCenter(position); } return { initMap : function() { createMap(); initAddress(); addMarkers(); } }; }(); document.addEventListener("DOMContentLoaded", GMAPS.mainMap.initMap, false);
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|