[英]Google Map Markers does not appear in $.getJSON Javascript
I am reading from a JSON file to plot my latitude and longitude onto Google Maps. 我正在读取JSON文件,以将经纬度绘制到Google Maps上。 I have tried using different approaches but nothing showed up.
我尝试使用不同的方法,但没有任何结果。 For this approach, the map showed but the markers are not there.
对于这种方法,地图显示出来了,但标记不在那里。
//this is my json file: out1copy.json
[{
"GPSLatitude": "1.306279",
"GPSLongitude": "103.904783",
"Title": "Joo Chiat Road"
},
{
"GPSLatitude": "1.343176",
"GPSLongitude": "103.851983",
"Title": "Braddell Road"
},
{
"GPSLatitude": "1.318105",
"GPSLongitude": "103.766597",
"Title": "Clementi Avenue"
},
{
"GPSLatitude": "1.427895",
"GPSLongitude": "103.837806",
"Title": "Yishun Central"
}]
This is my html file: 这是我的html文件:
<!DOCTYPE html>
<html>
<head>
<title>Try (Markers gone)</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
html, body, #map-canvas {
height: 100%;
margin: 0px;
padding: 0px
}
</style>
<body>
<div id="map-canvas"></div>
</body>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
var map;
var myLatlng = new google.maps.LatLng(1.3667, 103.8);
var mapOptions = {
zoom: 11,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
$.getJSON('out1copy.json', function(data){
$.each(data.markers, function(i, item) {
var latlng = new google.maps.LatLng(item.GPSLatitude,item.GPSLongitude);
new google.maps.Marker({
position: latlng,
map: map
});
});
});
</script>
</head>
</html>
Where could have gone wrong? 哪里可能出问题了?
You need to wait for the dom to load before running your map functions: 在运行地图功能之前,您需要等待dom加载:
google.maps.event.addDomListener(window, 'load', function() {
var map;
var myLatlng = new google.maps.LatLng(1.3667, 103.8);
var mapOptions = {
zoom: 11,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
$.getJSON('out1copy.json', function(data){
$.each(data.markers, function(i, item) {
var latlng = new google.maps.LatLng(item.GPSLatitude,item.GPSLongitude);
new google.maps.Marker({
position: latlng,
map: map
});
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.