Google Maps: marker icon and popup

The code below opens a pop-up window whenever a click on the map is clicked. He works:

<script type="text/javascript">
    function popup() {
        newwindow = window.open('test.php','Test','width=800,height=500');
        newwindow.focus();
        return false;
    }

    function addMarker(lat, lng, map){
        var latlng = new google.maps.LatLng(lat,lng);
        var marker = new google.maps.Marker({
            position: latlng,
            map: map
        });
        google.maps.event.addListener(marker, 'click', function() {
            popup();
        });
    }

    function initialize() {
        var myOptions = {
            center: new google.maps.LatLng(47.367633, 8.542557),
            zoom: 5,
            scrollwheel: true,
            mapTypeId: google.maps.MapTypeId.HYBRID,
            mapTypeControlOptions:{
                style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
            },
            navigationControlOptions:{
                style: google.maps.NavigationControlStyle.SMALL
            }
        };
        var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

        var jsonData = <?php echo $json; ?>;

        for(var i = 0; i < jsonData.length; i += 1){
            addMarker(jsonData[i].lat, jsonData[i].lng, map);
        }
    }
</script>

If I add a marker icon, the pop-up window still opens, but it immediately disappears in the background, that is, behind the browser window containing the map:

function addMarker(lat, lng, map){
    var latlng = new google.maps.LatLng(lat,lng);
    var marker = new google.maps.Marker({
        position: latlng,
        icon: 'myicon.png',
        map: map
    });
    google.maps.event.addListener(marker, 'click', function() {
        popup();
    });
}

What is the reason for this behavior?

+3
source share
1 answer

This is an interesting problem. This seems to be related to the Google Map window requesting focus after the click event. It is strange how this happens only when you use the custom marker icon.

, ​​1 . Firefox 3.6.3 , , :

function popup() {
   setTimeout(function () {
      var newwindow = window.open('test.php','Test','width=800,height=500');
      newwindow.focus();
   }, 1);
   return false;
}

. , , , .

+1

Source: https://habr.com/ru/post/1746349/


All Articles