[英]JavaScript for loop error placing markers on Google Map
即将解决三天的问题。 我正在尝试使用长期存储在Django模型中的经纬度在Google Map上放置标记。 我以前从未使用过AJAX,但正在尝试这样做。 使用Firebug,这表示我在JavaScript中有一个for循环错误,尽管我确定它是什么(JavaScript的新功能)。 尽管我确定格式是完美的(以逗号结尾),但是源显示的是纬度,拉长了。
如果有人发现错误,代码将在下面。 深入了解:
<script>
function mainGeo()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition( mainMap, error, {maximumAge: 30000, timeout: 10000, enableHighAccuracy: true} );
}
else
{
alert("Sorry, but it looks like your browser does not support geolocation.");
}
}
var map;
function mainMap(position)
{
// Define the coordinates as a Google Maps LatLng Object
var coords = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
// Prepare the map options
var mapOptions =
{
zoom: 15,
center: coords,
mapTypeControl: false,
navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
mapTypeId: google.maps.MapTypeId.ROADMAP
};
// Create the map, and place it in the map_canvas div
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
// Place the initial marker
var marker = new google.maps.Marker({
position: coords,
map: map,
title: "Your current location!"
});
}
function error() {
alert("You have refused to display your location. You will not be able to submit stories.");
}
mainGeo();
var stories = [{% for story in stories %}
{latitude:{{story.latitude}},longitude:{{story.longitude}}}, {% endfor %}];
loadMarkers(stories);
function loadMarkers(stories)
for (var s in stories) {
var story = story[s];
var point = new google.maps.LatLng(story.latitude, story.longitude);
var marker = new google.maps.Marker({position: point, map: map});
}
</script>
也许这句话说var story = story[s];
应该是var story = stories[s];
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.