JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 29th, 2012, 12:38 PM
F__i__L F__i__L is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 1 F__i__L User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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
  1. var GMAPS = window.GMAPS || {};
  2. GMAPS.mainMap = function() {
  3.   var map;
  4.   var infowindow = new google.maps.InfoWindow();
  5.   var jsonObject = {};
  6.  
  7.   function addMarkers() {
  8.     var xhr = new XMLHttpRequest();
  9.     xhr.open( "GET", "/home.json", true );
  10.     xhr.onreadystatechange = function () {
  11.       if ( xhr.readyState == 4 && xhr.status == 200 ) {
  12.         jsonObject = JSON.parse( xhr.responseText );
  13.         for (var i=0; i<jsonObject.length; i++) {
  14.           for (var j=0; j<jsonObject[i].microposts.length; j++) {
  15.             (function(Name, Title, Content, Latitude, Longitude) {
  16.               return function() {
  17.                 //alert(Name + "\n" + Title + "\n" + Content + "\n" + Latitude + "\n" + Longitude + "\n");  //<-- this works!
  18.                 var position = new google.maps.LatLng(Latitude, Longitude);
  19.                 var contentString = "<h4>" + Name.bold() + "</h4>" + "<br />" + Title.bold()
  20.                                 + "<br />" + Content;
  21.  
  22.                 var marker = new google.maps.Marker({
  23.                   position: position,
  24.                   title: Title,
  25.                   map: map
  26.                 });
  27.                 google.maps.event.addListener(marker, 'click', (function(marker, contentString) {
  28.                     return function() {
  29.                       infowindow.setContent(contentString);
  30.                       infowindow.open(map, marker);
  31.                     }
  32.                 })(marker, contentString));
  33.               };
  34.             })(jsonObject[i].name, jsonObject[i].microposts[j].title,
  35.                jsonObject[i].microposts[j].content,
  36.                jsonObject[i].microposts[j].lat,
  37.                jsonObject[i].microposts[j].lon)();
  38.           }
  39.         }
  40.       }
  41.     };
  42.     xhr.send(null);
  43.   }
  44.  
  45.   function createMap() {
  46.     map = new google.maps.Map(document.getElementById('main_map'), {
  47.       zoom: 10,
  48.       panControl: true,
  49.       zoomControl: true,
  50.       zoomControlOptions: {
  51.         style: google.maps.ZoomControlStyle.SMALL
  52.       },
  53.       mapTypeControl: true,
  54.       scaleControl: true,
  55.       streetViewControl: true,
  56.       overviewMapControl: true,
  57.       scrollwheel: false,
  58.       center: new google.maps.LatLng(37.975327,23.728701),
  59.       mapTypeId: google.maps.MapTypeId.ROADMAP
  60.     });
  61.   }
  62.  
  63.  
  64.   function initAddress() {
  65.     if (navigator.geolocation) {
  66.       navigator.geolocation.getCurrentPosition(showPosition);
  67.     }
  68.     else {
  69.       var position = new google.maps.LatLng(0, 0);
  70.       map.setCenter(position);
  71.     }
  72.   }
  73.  
  74.   function showPosition(position) {
  75.     var position = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
  76.     map.setCenter(position);
  77.   }
  78.  
  79.   return {
  80.  
  81.     initMap : function() {
  82.       createMap();
  83.       initAddress();
  84.       addMarkers();
  85.     }
  86.   };
  87. }();
  88.  
  89. document.addEventListener("DOMContentLoaded", GMAPS.mainMap.initMap, false);

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Javascript shows only last markers

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap