用于在Google地图上放置标记的循环错误的JavaScript

时间:2012-06-01 23:08:39

标签: javascript ajax google-maps-markers

接近解决为期三天的问题。我正在尝试使用长度存储在Django模型中的lat将标记放置在Google Map上。我之前从未使用过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>

1 个答案:

答案 0 :(得分:0)

也许说var story = story[s];的行应该是var story = stories[s];