简体   繁体   English

Google Maps Multiple Marker with Custom Markers and OnClick Zoom

[英]Google Maps Multiple Marker with Custom Markers and OnClick Zoom

I am trying to place multiple custom markers and what i want is zoom in and focus the location of clicked marker.我正在尝试放置多个自定义标记,我想要的是放大并聚焦单击标记的位置。 So far, i've partly succeed but there is two problem.到目前为止,我已经部分成功,但有两个问题。 First of all when i clicked on a marker and zoom in map is constantly shaking and second problem is i could only manage ot focus on one location.首先,当我点击一个标记并放大地图时,它会不断抖动,第二个问题是我只能管理一个位置。 Even if i click another, map is focusing the first one.即使我点击另一个,地图也会聚焦第一个。

You can see my code live in here: live map你可以在这里看到我的代码:实时地图

And here is my code:这是我的代码:

JS JS

    <script src='https://maps.googleapis.com/maps/api/js?key=AIzaSyBfkvgO7viPOgYJw24X4INQxZ66f6gU1NE&sensor=false&extension=.js'></script>
<script>
    google.maps.event.addDomListener(window, 'load', init);
    var map;
    function init() {
        var mapOptions = {
            center: new google.maps.LatLng(39.905643, 36.148673),
            zoom: 5,
            zoomControl: true,
            zoomControlOptions: {
                style: google.maps.ZoomControlStyle.LARGE,
            },
            disableDoubleClickZoom: true,
            mapTypeControl: true,
            mapTypeControlOptions: {
                style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
            },
            scaleControl: true,
            scrollwheel: false,
            panControl: true,
            streetViewControl: false,
            draggable: true,
            overviewMapControl: true,
            overviewMapControlOptions: {
                opened: true,
            },
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            styles: [{ featureType: "administrative", stylers: [{ visibility: "off" }] }, { featureType: "poi", stylers: [{ visibility: "simplified" }] }, { featureType: "road", stylers: [{ visibility: "simplified" }] }, {
                featureType: "water",
                stylers: [{ visibility: "simplified" }]
            }, { featureType: "transit", stylers: [{ visibility: "simplified" }] }, { featureType: "landscape", stylers: [{ visibility: "simplified" }] }, { featureType: "road.highway", stylers: [{ visibility: "off" }] }, { featureType: "road.local", stylers: [{ visibility: "on" }] }, { featureType: "road.highway", elementType: "geometry", stylers: [{ visibility: "on" }] }, { featureType: "water", stylers: [{ color: "#84afa3" }, { lightness: 52 }] }, { stylers: [{ saturation: -77 }] }, { featureType: "road" }],
        }
        var mapElement = document.getElementById('haritam');
        var map = new google.maps.Map(mapElement, mapOptions);
        var locations = [
['Location One', 'This is my address.', '0 111 222 33 44', 'info@mydomain.com', 'www.mydomain.com', 36.85915525936127, 30.79201858795159, 'http://www.karayeltasarim.com/Resim/Upload/osmark.png'], ['Location Two', 'This is my address.', '0 242 344 10 20', 'info@mydomain.com', 'www.mydomain.com', 37.009614, 27.257248, 'http://www.karayeltasarim.com/Resim/Upload/osmark.png']
        ];
        for (i = 0; i < locations.length; i++) {
            if (locations[i][1] == 'undefined') { description = ''; } else { description = locations[i][1]; }
            if (locations[i][2] == 'undefined') { telephone = ''; } else { telephone = locations[i][2]; }
            if (locations[i][3] == 'undefined') { email = ''; } else { email = locations[i][3]; }
            if (locations[i][4] == 'undefined') { web = ''; } else { web = locations[i][4]; }
            if (locations[i][7] == 'undefined') { markericon = ''; } else { markericon = locations[i][7]; }
            marker = new google.maps.Marker({
                icon: markericon,
                position: new google.maps.LatLng(locations[i][5], locations[i][6]),
                map: map,
                title: locations[i][0],
                desc: description,
                tel: telephone,
                email: email,
                web: web
            });
            if (web.substring(0, 7) != "http://") {
                link = "http://" + web;
            } else {
                link = web;
            }
            bindInfoWindow(marker, map, locations[i][0], description, telephone, email, web, link);
        }
        function bindInfoWindow(marker, map, title, desc, telephone, email, web, link) {
            var infoWindowVisible = (function () {
                var currentlyVisible = false;
                return function (visible) {
                    if (visible !== undefined) {
                        currentlyVisible = visible;
                    }
                    return currentlyVisible;
                };
            }());
            iw = new google.maps.InfoWindow();
            google.maps.event.addListener(marker, 'click', function () {
                if (infoWindowVisible()) {
                    iw.close();
                    infoWindowVisible(false);
                } else {
                    var html = "<div id='sube' style='color:#000;background-color:transparent;padding:5px;width:200px;'><h4>" + title + "</h4><p>" + desc + "<p><p>" + telephone + "<p><a href='mailto:" + email + "' >" + email + "<a><a href='" + link + "'' >" + web + "<a></div>";
                    iw = new google.maps.InfoWindow({ content: html });
                    iw.open(map, marker);
                    infoWindowVisible(true);
                }
            });
            google.maps.event.addListener(iw, 'closeclick', function () {
                infoWindowVisible(false);
            });

            google.maps.event.addListener(map, 'center_changed', function () {
                window.setTimeout(function () {
                    map.panTo(marker.getPosition());
                }, 1000);
            });

            google.maps.event.addListener(marker, 'click', function () {
                map.setZoom(8);
                map.setCenter(marker.getPosition());
            });
        }
    }
</script>

CSS CSS

    <style>
    #haritam {
        height: 400px;
        width: 100%;
    }

    .gm-style-iw * {
        display: block;
        width: 100%;
    }

    .gm-style-iw h4, .gm-style-iw p {
        margin: 0;
        padding: 0;
    }

    .gm-style-iw a {
        color: #4272db;
    }
</style>

HTML HTML

<div id='haritam'></div>

When you have markers inside loop, and want to access the clicked marker always use 'this' instead of marker variable,当您在循环中有标记并且想要访问单击的标记时,请始终使用“this”而不是标记变量,

For eg.例如。

for(var i=0; i<9; i++) {
  new google.maps.event.addListener(markers[i], 'click', function() {
    infowindow.open(map, this)
  });
}

If you don't do the above work around, and use marker variable instead, the loop executes and finishes before you click on marker, there fore the value of I will always be the value of its last iteration.如果您不执行上述工作,而是使用标记变量,则循环会在您单击标记之前执行并完成,因此 I 的值将始终是其最后一次迭代的值。 And thus clicking only shows the latest one因此点击只显示最新的

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

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