2017-08-29 57 views
2

我在JavaScript中的初學者,我試圖將數據導入地圖,我從我的URL JSON數組列表。但經度和緯度沒有取得,我也試圖在控制檯中打印它們,但沒有顯示。JSON數組是不是在谷歌地圖中取出

這是我的JSON數據:

[{"Id":1,"Country":"Uganda","District":"kampala","SubCounty":"Hima","EpidemicName":"cholera","PatientName":"Michael","PatientResidenceArea":"Muhokya","Latitude":0.3249832,"Longitude":32.5753133,"HealthFacilityName":"Mulago","HealthOfficerName":"Fauziya"},{"Id":2,"Country":"Uganda","District":"Mbarara","SubCounty":"Mile","EpidemicName":"Cholera","PatientName":"Musa","PatientResidenceArea":"Kabalagala","Latitude":0.635364,"Longitude":0.354789,"HealthFacilityName":"Kiruddu","HealthOfficerName":"Mbabaali"},{"Id":3,"Country":"Uganda","District":"Jinja","SubCounty":"kashaali","EpidemicName":"Cholera","PatientName":"Musa","PatientResidenceArea":"Kabalagala","Latitude":0.635364,"Longitude":0.354789,"HealthFacilityName":"Kiruddu","HealthOfficerName":"Mbabaali"},{"Id":4,"Country":"Uganda","District":"Mbarara","SubCounty":"Mile","EpidemicName":"Cholera","PatientName":"Musa","PatientResidenceArea":"Kabalagala","Latitude":0.635364,"Longitude":0.354789,"HealthFacilityName":"Kiruddu","HealthOfficerName":"Mbabaali"},{"Id":5,"Country":"Uganda","District":"Jinja","SubCounty":"kashaali","EpidemicName":"Cholera","PatientName":"Musa","PatientResidenceArea":"Kabalagala","Latitude":0.635364,"Longitude":0.354789,"HealthFacilityName":"Kiruddu","HealthOfficerName":"Mbabaali"},{"Id":6,"Country":"Uganda","District":"Mbarara","SubCounty":"Mile","EpidemicName":"Cholera","PatientName":"Musa","PatientResidenceArea":"Kabalagala","Latitude":0.635364,"Longitude":0.354789,"HealthFacilityName":"Kiruddu","HealthOfficerName":"Mbabaali"},{"Id":7,"Country":"Uganda","District":"Jinja","SubCounty":"kashaali","EpidemicName":"Cholera","PatientName":"Musa","PatientResidenceArea":"Kabalagala","Latitude":0.635364,"Longitude":0.354789,"HealthFacilityName":"Kiruddu","HealthOfficerName":"Mbabaali"},{"Id":8,"Country":"Uganda","District":"Mbarara","SubCounty":"Mile","EpidemicName":"Cholera","PatientName":"Musa","PatientResidenceArea":"Kabalagala","Latitude":0.635364,"Longitude":0.354789,"HealthFacilityName":"Kiruddu","HealthOfficerName":"Mbabaali"},{"Id":9,"Country":"Uganda","District":"Jinja","SubCounty":"kashaali","EpidemicName":"Cholera","PatientName":"Musa","PatientResidenceArea":"Kabalagala","Latitude":0.635364,"Longitude":0.354789,"HealthFacilityName":"Kiruddu","HealthOfficerName":"Mbabaali"},{"Id":10,"Country":"Uganda","District":"Mbarara","SubCounty":"Mile","EpidemicName":"Cholera","PatientName":"Musa","PatientResidenceArea":"Kabalagala","Latitude":0.635364,"Longitude":0.354789,"HealthFacilityName":"Kiruddu","HealthOfficerName":"Mbabaali"},{"Id":11,"Country":"Uganda","District":"Mbarara","SubCounty":"Mile","EpidemicName":"Cholera","PatientName":"Musa","PatientResidenceArea":"Kabalagala","Latitude":0.635364,"Longitude":0.354789,"HealthFacilityName":"Kiruddu","HealthOfficerName":"Mbabaali"}] 

而下面是我的代碼:

<body> 
    <div id="map"></div> 
    <script> 
     var map; 
     function initMap() { 
      map = new google.maps.Map(document.getElementById('map'), { 
       zoom: 2, 
       center: new google.maps.LatLng(2.8, -187.3), 
       mapTypeId: 'terrain' 
      }); 

      // Create a <script> tag and set the USGS URL as the source. 
      var script = document.createElement('script'); 
      // This example uses a local copy of the GeoJSON stored at 
      // http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_week.geojsonp 
      script.src = 'http://localhost:82/Mobile/Fetch.aspx?DataFormat=Details'; 
      document.getElementsByTagName('head')[0].appendChild(script); 
     } 

     // Loop through the results array and place a marker for each 
     // set of coordinates. 
     window.eqfeed_callback = function (results) { 
      for (var i = 0; i < results.length; i++) { 
       var coords = results.features[i].geometry.coordinates; 
       var Ltd = results.Latitude[i]; 
       var Lgd = results.Longitude[i]; 
       console.log("Latitude : "+ Ltd+" Longitude : "+ Lgd); 
       var latLng = new google.maps.LatLng(Ltd, Lgd); 
       var marker = new google.maps.Marker({ 
        position: latLng, 
        map: map 
       }); 
      } 
     } 
    </script> 
    <script async defer 
    src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAYol0DZtLxlMaLW6S7QZIQk48Do18cm6A&callback=initMap"> 
    </script> 
    </body> 

編輯:

這是我更新 但是仍然不知道爲什麼它沒有帶點,即使我試圖打印他們沒有顯示在控制檯中的點爲什麼?

var map; 
     function initMap() { 
      map = new google.maps.Map(document.getElementById('map'), { 
       zoom: 2, 
       center: new google.maps.LatLng(2.8, -187.3), 
       mapTypeId: 'terrain' 
      }); 

      // Create a <script> tag and set the USGS URL as the source. 
      var script = document.createElement('script'); 
      // This example uses a local copy of the GeoJSON stored at 
      // http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_week.geojsonp 
      script.src = 'http://192.168.0.6:72/Mobile/Fetch.aspx?DataFormat=Details'; 
      document.getElementsByTagName('head')[0].appendChild(script); 
     } 

     // Loop through the results array and place a marker for each 
     // set of coordinates. 
     window.eqfeed_callback = function (results) { 
      for (var i = 0; i < results.length; i++) { 

       var Ltd = results[i].Latitude; 
       var Lgd = results[i].Longitude; 
       console.log("Latitude : " + Ltd + " Longitude : " + Lgd); 

       alert(""+ Ltd); 
       var latLng = new google.maps.LatLng(Ltd, Lgd); 
       var marker = new google.maps.Marker({ 
        position: latLng, 
        map: map, 
        label: results[i].EpidemicName 
       }); 
      } 
     } 

回答

1

window.eqfeed_callback函數有一些錯誤。它應該是這樣的:

window.eqfeed_callback = function (results) { 
    for (var i = 0; i < results.length; i++) { 
     var Ltd = results[i].Latitude; 
     var Lgd = results[i].Longitude; 
     console.log("Latitude : "+ Ltd+" Longitude : "+ Lgd); 
     var latLng = new google.maps.LatLng(Ltd, Lgd); 
     var marker = new google.maps.Marker({ 
      position: latLng, 
      map: map 
     }); 
    } 
} 

注意,coords變量已被刪除,因爲它是未使用的。而且,數組索引必須出現在陣列的名稱後,而不是在表達的最後,如在var Ltd = results[i].Latitude;而非var Ltd = results.Latitude[i];

而且,由initMap函數加載腳本必須時,它調用eqfeed_callback功能負載。這可以通過通過jQuery加載JSON來完成,像這樣:

$.getJSON('http://192.168.0.6:72/Mobile/Fetch.aspx?DataForma‌​t=Details', eqfeed_callback); 

編輯:

要將標題添加到每個標記,做這樣的事情:

var marker = new google.maps.Marker({ 
    position: latLng, 
    map: map, 
    label: results[i].EpidemicName 
}); 

請請參閱Google Maps API reference以獲取更多信息

+0

如果我想將** EpidemicName **作爲每個點上的標記,我該怎麼辦。 ** EpidemicName **也是Json中的一個對象。 –

+0

@LutaayaHuzaifahIdris我用這些信息更新了我的答案。 –

+0

非常感謝@Jason –