[英]JavaScript error in placing markers on Google Maps
我遇到了一個非常令人沮喪的錯誤,該錯誤更接近分辨率,但還不止於此。 我正在嘗試根據Django模型中存儲的經緯度長坐標將標記放置在Google Map上。
得益於一些見解,我不太熟悉的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});
}
我不知道故事和故事中存儲了什么,但是在創建var point
,需要使用兩個參數構造LatLng
,例如(number1,number2)或(string1,string2),其中字符串代表數字。
如果故事是單個字符串,請說“ 39.39,20.21”,您只需要split(",")
即可獲得兩個數字字符串。
myString = "40.7, -70.4";
myStringValues = myString.split(",");
var point = new google.maps.LatLng(myStringValues[0], myStringValues[1]);
var stories
也重復緯度,難道不是經度嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.