Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zooming to center on a map in Google Maps API V3

I'm new to using google maps and am trying to find out how to restrict the zoom function so that it does not zoom anywhere except of the center of the map and not zoom to the mouse point. Basically it zooms to the center of the returned map no matter where your mouse pointer is on the map as opposed to zooming where the mouse pointer is located on the map. I don't even know if this is possible currently, some help would be appreciated.

<script type="text/javascript">
        function initialize() {
        var latlng = new google.maps.LatLng(51.285583, 1.091045);
        var myOptions = {
        zoom: 15,
        center: latlng,
        scrollwheel: true,
        navigationControl: false,
        mapTypeControl: false,
        scaleControl: false,
        draggable: false,
        mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);

        var marker = new google.maps.Marker({
            position: new google.maps.LatLng(51.285826, 1.089973),
            map: map,
            title: 'Run of The Mill',
            clickable: true
            });
        }
    </script>
like image 930
W0lf7 Avatar asked Dec 01 '11 01:12

W0lf7


2 Answers

There are several zoom types:

  • using zoom control - that zooms to the center of the map
  • using scroll wheel - that zooms to the mouse pointer as you complain
  • using double click - that first centers the place under the mouse pointer and then zooms

So if you want only the first zoom type, you can disable the other two by setting disableDoubleClickZoom and scrollwheel map options to false.

Moreover, you might handle dblclick map event (scroll wheel event is not so straightforward, but maybe you'll find a way how to handle the scroll wheel in javascript too) and in this handler just change the map scale using map.setZoom(). The map center will stay the same. Fairly easy.

like image 94
Tomas Avatar answered Sep 26 '22 20:09

Tomas


Or handle this by re-centering the map on zoom change. This preserves the expected functionality on double click and scroll wheel.

google.maps.event.addListener(map,'zoom_changed',function () {
  map.setCenter(new google.maps.LatLng(47.61388802057299,-122.31870898147581));
})
like image 24
here Avatar answered Sep 24 '22 20:09

here