0

我遇到了一个非常令人沮丧的错误,它更接近解决方案,但并不完全存在。我正在尝试根据存储在 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});
    }
4

1 回答 1

0

我不知道 story 和 stories 中存储了什么,但是在制作s 时var pointLatLng需要使用两个参数构造 s,例如 (number1, number2) 或 (string1, string2),其中字符串表示数字。

如果 story[s] 是一个字符串,比如“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,不应该是经度吗?

于 2012-06-01T20:17:44.150 回答