接近解决一个为期三天的问题。我正在尝试使用长期存储在 Django 模型中的 lat 在谷歌地图上放置标记。我以前从未使用过 AJAX,但我正在尝试这样做来实现这一点。使用 Firebug,它说明我在 JavaScript 中有一个 for 循环错误,尽管我确定它是什么(JavaScript 的新功能)。源显示 lat,longs 拉起,虽然我确信格式是完美的(以逗号结尾)。
如果有人发现错误,代码如下。洞察力非常感谢:
<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>