简体   繁体   English

谷歌地图api v3在一定的缩放级别上显示标记

[英]Google maps api v3 show markers on a certain level of zoom

I'm kind of a newbie in JS and google map but I'm trying to create a Styled map with my own tiles, and I want to add some markers depending on zoom level because when my map is at minZoom (0), the markers are repeating horizontally. 我是JS和谷歌地图的新手,但我正在尝试使用我自己的瓷砖创建一个Styled地图,我想根据缩放级别添加一些标记,因为当我的地图位于minZoom(0)时,标记是水平重复的。 So I'm looking for a way to disable the horizontal repeat of the markers or a way to toggle the markers depending on the level of zoom. 所以我正在寻找一种方法来禁用标记的水平重复或根据缩放级别切换标记的方法。

  var ItalieTypeOptions = {
    getTileUrl: function (coord, zoom) {
        var normalizedCoord = getNormalizedCoord(coord, zoom);
        if (!normalizedCoord) {
            return null;
        }
        var bound = Math.pow(2, zoom);
        return 'http://mysite.fr/folder' + '/' + zoom + '/' + normalizedCoord.x + '/' + (bound - normalizedCoord.y - 1) + '.png';
    },
    tileSize: new google.maps.Size(256, 256),
    maxZoom: 4,
    minZoom: 0,
    name: 'Italie'
};
var ItalieMapType = new google.maps.ImageMapType(ItalieTypeOptions);

function initialize() {
    var myLatlng = new google.maps.LatLng(0, 0);
    var mapOptions = {
        center: myLatlng,
        zoom: 0,
        draggable: true,
        zoomControl: true,
        scaleControl: false,
        mapTypeControl: false,
        streetViewControl: false,
        backgroundColor: '#6AAECB',
        mapTypeControlOptions: {
            mapTypeIds: ['Italie']


        }


    };




    var map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
    map.mapTypes.set('Italie', ItalieMapType);
    map.setMapTypeId('Italie');
    var locations = [
        ['Lorem', 20, -30],
        ['Ipsum', 20, 75],
        ['Dolor', 53, 45]
    ];
    var infowindow = new google.maps.InfoWindow();
    var image = {
        url: 'img/marker.png',
        size: new google.maps.Size(33, 41),
        origin: new google.maps.Point(0, 0),

    };




    for (i = 0; i < locations.length; i++) {

        marker = new google.maps.Marker({
            position: new google.maps.LatLng(locations[i][1], locations[i][2]),
            map: map,
            draggable: false,
            icon: image,

            animation: google.maps.Animation.DROP
        });
        google.maps.event.addListener(marker, 'click', (function (marker, i) {
            return function () {
                infowindow.setContent(locations[i][0]);
                infowindow.open(map, marker);
            }
        })(marker, i));
    }
}



function getNormalizedCoord(coord, zoom) {
    var y = coord.y;
    var x = coord.x;

    var tileRange = 25 << zoom;  // I wrote 25 to display only one image but i'm not ure if it's the right way

    if (y < 0 || y >= tileRange) {
        return null;
    }

    if (x < 0 || x >= tileRange) {
        x = (x % tileRange + tileRange) % tileRange;
    }
    return {
        x: x,
        y: y
    };

}
google.maps.event.addDomListener(window, 'load', initialize);

Thanks 谢谢

EDIT 编辑

So, I managed to display one marker depending on the level of zoom with this : 所以,我设法根据缩放级别显示一个标记:

   google.maps.event.addListener(map, 'zoom_changed', function () {

   var currentZoom = map.getZoom();

   if (currentZoom >= 3 && currentZoom <= 4) {
       marker.setMap(map);
       alert('lol');
   } else if (currentZoom == 0) {
       marker.setMap(null);
   } else if (currentZoom == 1) {
       marker.setMap(null);
   } else if (currentZoom == 2) {
       marker.setMap(null);
   } else if (currentZoom == 4) {
       marker.setMap(null);
   } else {
       marker.setMap(null);
   }
  });

I know it's not clean at all, but If someone can help me with that. 我知道它根本不干净,但如果有人可以帮助我。 The problem with this it's that it only diplay one marker (the last -> Location ['Dolor', 53, 45]), and i want to find a way to say "display all the markers in the location var) 这个问题是它只显示一个标记(最后 - >位置['Dolor',53,45]),我想找到一种方法来说“显示位置var中的所有标记”

Thanks 谢谢

Create an array for your markers, say: 为标记创建一个数组,例如:

var allMarkers = [];

Then when you create your markers you can push them into the array and simply run through your array to setMap(map) like so: 然后,当您创建标记时,可以将它们推入数组,然后只需将数组运行到setMap(map),如下所示:

allMarkers.push(marker);
//when you are ready to display them do the loop
for(var i = 0; i <= allMarkers.length-1; i++){
    allMarkers[i].setMap(map);
}

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

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