weixin_33726943 2014-08-14 12:06 采纳率: 0%
浏览 26

一次放下一个标记

I'm using Google Maps Api v3 and I'm trying to drop one marker at time on my Map, just like Google Demo but I could not get it to work, here's my code, all markers are dropped at same time.

var map;
var markers = [];

function initialize() { 
    var latlng = new google.maps.LatLng(52.520816, 13.410186);

    var options = {
        zoom: 5,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map(document.getElementById("map-canvas"), options);
}

initialize();

function loadMarkers() {  

    $.ajax({
       url: 'js/poi.php',
       type: 'GET',
       dataType : "json",
       success: function (data) {

            var latlngbounds = new google.maps.LatLngBounds();      


            $.each(data, function(index, point) {

                var marker = new google.maps.Marker({
                    position: new google.maps.LatLng(point.Lat, point.Lng),
                    animation: google.maps.Animation.DROP,
                    icon: 'img/marker.png'

                });


                markers.push(marker);
                latlngbounds.extend(marker.position);

            });

            var markerCluster = new MarkerClusterer(map, markers);
            map.fitBounds(latlngbounds);

        }

    });
}

loadMarkers();

I tried do use a Timeout on each Marker but i guess that loadMarkers(); will always drop them at once...

    setTimeout(function() {

        var marker = new google.maps.Marker({
            position: new google.maps.LatLng(point.Lat, point.Lng),
            animation: google.maps.Animation.DROP,
            icon: 'img/marker.png'

        });

    }, point.Id * 200);

Any ideas on how could I fix this ?

EDIT: The poi.php file list all points from my Table and Output them like this:

[
{"Id":"1","Lat":"52.511467","Lgn":"13.447179"},
{"Id":"2","Lat":"52.549061","Lgn":"13.422975"},
{"Id":"3","Lat":"52.497622","Lgn":"13.396110"},
{"Id":"4","Lat":"52.517683","Lgn":"13.394393"}
]
  • 写回答

2条回答 默认 最新

  • 胖鸭 2014-08-14 13:42
    关注
    1. add the markers to the clusterer as they are added to the map
    2. adjust the bounds to show the markers as they are added
    3. fixed typo in your JSON (don't know if that is a problem or not)
    function initialize() {
        var latlng = new google.maps.LatLng(52.520816, 13.410186);
    
        var options = {
            zoom: 5,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
    
        map = new google.maps.Map(document.getElementById("map-canvas"), options);
        loadMarkers();
    }
    
    function loadMarkers() {
        $.ajax({
            type: 'POST',
            dataType: 'json',
            url: '/echo/json/',
            data: {
                json: JSON.stringify(jsonData)
            },
            delay: 3,
            success: function (data) {
                var markerCluster = new MarkerClusterer(map, markers);
                var latlngbounds = new google.maps.LatLngBounds();
                $.each(data, function (index, point) {
                        setTimeout(function() {
                          var marker = new google.maps.Marker({
                            position: new google.maps.LatLng(point.Lat, point.Lng),
                            animation: google.maps.Animation.DROP,
                            map: map /* don't have your custom marker
                            icon: 'img/marker.png'*/
                          });
                          markerCluster.addMarker(marker);
                          markers.push(marker);
                          // adjust the bounds to show all the markers
                          latlngbounds.extend(marker.getPosition());
                          map.fitBounds(latlngbounds);
                        }, point.Id * 200);
                });
            }
        });
    }
    

    working fiddle

    评论

报告相同问题?