我遇到了一个非常令人沮丧的错误,它更接近解决方案,但并不完全存在。我正在尝试根据存储在 Django 模型中的经纬度坐标在 Google 地图上放置标记。
多亏了一些见解,看起来我不太熟悉的 AJAX 是从模型中获取这些坐标然后使用它在地图上放置标记的方法。在从结果中查看源代码时,正在检索坐标,但通过 Firebug 导致错误:
syntax error
[Break On This Error]
function loadMarkers(stories):
/report/all/ (line 1172, col 29)
任何有关可能导致此问题的原因以及可能的解决方案的见解都将受到欢迎。仅供参考,这是开发模式,所有坐标都在同一个地方,可能太多了。地图不再使用“loadMarkers”功能显示。
代码如下:
<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 %}
{{story.latitude}},{{story.latitude}}, {% endfor %}];
loadMarkers(stories);
function loadMarkers(stories):
for (var s in stories) {
var story = story[s];
var point = new google.maps.LatLng(story[s]);
var marker = new google.maps.Marker({position: point, map: map});
}
编辑:修改代码但检索到相同的错误
var stories = [{% for story in stories %}
({{story.latitude}},{{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});
}