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
});
}
}
如果我想將** EpidemicName **作爲每個點上的標記,我該怎麼辦。 ** EpidemicName **也是Json中的一個對象。 –
@LutaayaHuzaifahIdris我用這些信息更新了我的答案。 –
非常感謝@Jason –