谷歌地图,为标记传递一系列Lat和Lng?

时间:2012-03-27 00:02:12

标签: jquery arrays google-maps

我有一个回拨网址,我正在拉出Lat和Lng在Google地图上显示为标记。我将它们放入一个数组中,但我认为我必须拆分它们才能让谷歌地图了解它们。

function jsonData(){
        $.ajax({
            url: https://api.foursquare.com/v2/users/self/checkins?oauth_token=FUKXDJRWIB0AQ2MQUKUEUSB3KW2TMYKUMFGYLYUHBBH14CQ0&v=20120126,
            cache: false,
            dataType: 'json',
            success: function(results) {
                var lat;
                var long;
                var paths = [];
                for(var i = 0; i < results.response.checkins.items.length; i++) {
                    var lat = results.response.checkins.items[i].venue.location.lat;
                    var long = results.response.checkins.items[i].venue.location.lng;   
                    var pathDetails = lat +"," + long;
                    paths.push(pathDetails);
                }
                drop(paths);
            } 
        });
   };


        var directionsDisplay;
        var directionsService = new google.maps.DirectionsService();
        var map;

        var neighborhoods = [];

        var markers = [];
        var iterator = 0;

        function initialize() {
              var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
              directionsDisplay = new google.maps.DirectionsRenderer();
              //var newyork = new google.maps.LatLng(40.7051157, -74.0088305);
              var myOptions = {
                zoom:1,
                mapTypeId: google.maps.MapTypeId.ROADMAP
              }

              map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
              directionsDisplay.setMap(map);

              var start = "new york, ny";
              var end = "los angeles, ca";
              var request = {
                origin:start,
                destination:end,
                travelMode: google.maps.TravelMode.DRIVING
              };
              directionsService.route(request, function(result, status) {
                if (status == google.maps.DirectionsStatus.OK) {
                  directionsDisplay.setDirections(result);
                }
              });
          }


          function drop(neighborhoods) {
            console.log("Passing in data: " + neighborhoods);
            for (var i = 0; i < neighborhoods.length; i++) {
              setTimeout(function() {
                addMarker();
              }, i * 200);
            }
          }

          function addMarker() {
            markers.push(new google.maps.Marker({
              position: neighborhoods[iterator],
              map: map,
              draggable: false,
              animation: google.maps.Animation.DROP
            }));
            iterator++;
          }   

$(document).ready(function() {
    jsonData();
    drop();

});

2 个答案:

答案 0 :(得分:0)

你可以存储一个数组OF数组,这样每个数组元素都会将lat和lng分开,然后你可以引用它们:arrayName[i][0]arrayName[i][1]

我认为如果你将点数转换为谷歌LatLng,然后存储一组数据,它也可能有效。

编辑:对于你的代码现在如何,我认为第二个选项会更好......否则,在你的addMarker函数中,你应该取值,把它们变成LatLng,然后用它作为点< / p>

答案 1 :(得分:0)

我做了大约4次更改,重要的是邻域是一个要删除的局部变量(我将其重命名为nx)和全局变量。

请在此处查看更改: http://jsfiddle.net/JVQTK/