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

    评论

报告相同问题?

悬赏问题

  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝