2013-05-10 44 views
0

我能夠在Firefox瀏覽器中顯示json的標記,但不顯示crome和safari。 perarps它取決於我的jQuery腳本的形式retrives的JSON?Google api標記顯示在Firefox上,不顯示在Chrome或Safari上

我jQuery代碼是:

(function() { 
    window.onload = function() { 

     // Creating a new map 
     var map = new google.maps.Map(document.getElementById("map"), { 
      center: new google.maps.LatLng(41.65, -0.88), 
      zoom: 12, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }); 


     // Get the JSON data from PHP script 

var json ; 
$.getJSON("http://myserver/down.php").done(function(data) { 
    console.log(data); 
    json = data; 
}); 

alert(" Benvenuto "); 


     // Creating a global infoWindow object that will be reused by all markers 
     var infoWindow = new google.maps.InfoWindow(); 

     // Looping through the JSON data 
     for (var i = 0, length = json.locations.length; i < length; i++) { 

       var data = json.locations[i], 
       latLng = new google.maps.LatLng(data.lat, data.long); 

      var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/'; 
      var marker = new google.maps.Marker({ 
       position: latLng, 
       map: map, 
       title: data.nombre, 
       icon: iconBase + 'schools_maps.png' 
       }); 


      (function(marker, data) { 

       // Attaching a click event to the current marker 
       google.maps.event.addListener(marker, "click", function(e) { 
        infoWindow.setContent(data.nombre); 
        infoWindow.open(map, marker); 
       }); 


      })(marker, data); 

     } 

    } 

})(); 

和調用腳本的HTML代碼:

<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
    <title>MAPPA</title> 
    <link type="text/css" href="css/style.css" rel="stylesheet" media="all" /> 

    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&v=3.8"></script> 

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 

    <script type="text/javascript" src="map.js"></script> 
    </head> 
    <body> 
    <h1>MAPPA PROVE</h1> 
    <div id="map"></div> 

    </body> 
</html> 

我沒有使用資源管理器嘗試。

在此先感謝

+0

有效的鏈接或一些代碼可能幫助我們幫助你 – 2013-05-10 16:04:50

+0

Tx。我剛剛編輯我的問題! – doxsi 2013-05-10 16:09:07

回答

1

這裏的第一件事:$ .getJSON是異步過程。因此,當獲得「for」行時,數據可能尚未加載。

試試這個:

(function() { 
    window.onload = function() { 

     // Creating a new map 
     var map = new google.maps.Map(document.getElementById("map"), { 
      center: new google.maps.LatLng(41.65, -0.88), 
      zoom: 12, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }); 


     // Creating a global infoWindow object that will be reused by all markers 
     function createPoints(json){ 
     var infoWindow = new google.maps.InfoWindow(); 

     // Looping through the JSON data 
     for (var i = 0, length = json.locations.length; i < length; i++) { 

       var data = json.locations[i], 
       latLng = new google.maps.LatLng(data.lat, data.long); 

      var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/'; 
      var marker = new google.maps.Marker({ 
       position: latLng, 
       map: map, 
       title: data.nombre, 
       icon: iconBase + 'schools_maps.png' 
       }); 


      (function(marker, data) { 

       // Attaching a click event to the current marker 
       google.maps.event.addListener(marker, "click", function(e) { 
        infoWindow.setContent(data.nombre); 
        infoWindow.open(map, marker); 
       }); 


      })(marker, data); 
     } 
     } 


     // Get the JSON data from PHP script 

var json ; 
$.getJSON("http://myserver/down.php").done(function(data) { 
    console.log(data); 
    json = data; 
    createPoints(json); 
}); 

    } 

})(); 

我沒有搜索其他錯誤回報,因此,如果不行的話,我們就可以再次嘗試。

+0

奇怪的是,在Firefox上工作...但你的提示使它可以在Chrome和Safari上工作。發送很多!你認爲這取決於同步嗎? – doxsi 2013-05-10 17:59:30

+0

我猜webkit比ff引擎快。也許這就是爲什麼ff。 – Scoup 2013-05-10 18:12:45

相關問題