简体   繁体   English

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

[英]JavaScript for loop error placing markers on Google Map

Getting close to solving a three-day problem. 即将解决三天的问题。 I'm attempting to place markers on a Google Map using the lat, long stored in a Django model. 我正在尝试使用长期存储在Django模型中的经纬度在Google Map上放置标记。 I've never used AJAX before but am trying to do so to achieve this. 我以前从未使用过AJAX,但正在尝试这样做。 Using Firebug, it's stating I have a for loop error in the JavaScript although I'm usure what it is (new at JavaScript). 使用Firebug,这表示我在JavaScript中有一个for循环错误,尽管我确定它是什么(JavaScript的新功能)。 The source is displaying the lat, longs pulling up, although I'm usure the format is perfect (ends in a comma). 尽管我确定格式是完美的(以逗号结尾),但是源显示的是纬度,拉长了。

The code is below if anyone spots the error. 如果有人发现错误,代码将在下面。 Insight much appreciated: 深入了解:

<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>

Maybe the line saying var story = story[s]; 也许这句话说var story = story[s]; should be var story = stories[s]; 应该是var story = stories[s]; .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM