Google maps - keep center when zooming

In Google Maps, I would like to be able to keep the center of the map on a marker on my location when I'm zooming in or out. It's something that Ingress does, it doesn't matter where you double tap (or double click) or where you pinch, the map remains centered on your marker. So it's possible...

The best I came up with for now is :

google.maps.event.addListener(mymap, 'zoom_changed', function() {
    mymap.setCenter({lat: myLoc.lat, lng: myLoc.lon});
})

But it's far from perfect, it just re-center the map after the user already zoomed...

Thanks a lot !

[EDIT] absolutely nothing helps in the API ref... Or elsewhere I'm blind and missed it !


ANSWERS:


Purely using Javascript

  • First you disable scroll zoom function by default of google map by scrollwheel=false,
  • Next create custom scroll zoom function by capture mouse event and set zoom level for google map

Check my sample code: Fiddle URL:

var map, i = 12;
function initialize() {
		var myLatlng = new google.maps.LatLng(46.769603, 23.589957);
		var mapOptions = {
			center: myLatlng,
			zoom: i,
			scrollwheel: false,
  		disableDoubleClickZoom: true,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		map = new google.maps.Map(document.getElementById("map_canvas"),
			mapOptions);

		var marker = new google.maps.Marker({
			position: myLatlng,
			map: map,
			title: 'Any Title'
		});


	}

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

$( document ).ready(function() {
    $('#map_canvas').on("wheel", function(evt){
   // console.log(evt.originalEvent.deltaY);
    		if(evt.originalEvent.deltaY > 0){
        	map.setZoom(++i);
        }else {
        	map.setZoom(--i);
        }
    		
    });
});
#map_canvas{
    height:400px;
    width:100%;
}
<script src=""></script>
<script src=""></script>
<div id="map_canvas"></div>

I have no idea why you tagged this question as both Javascript and Android. On Android you need to wrap your MapView inside another view and detect Double Tap motion inside onInterceptTouchEvent which you can override if your class can override FrameLayout for example. Then return true to prevent the map from handling this event and you handle it instead.

For full code please see my answer here:


Unfortunately Google Maps Api does not provide this behaviour.

The following example doesn't require jQuery and supports double-click and mouse scrolling, but you can add more events if you want.

View in Codepen

Disable:

  • Double-clickin
  • Scrolling
  • Panning

Add dblclick and zoom_changed events on the map and a mousewheel event on the canvas.

I have commented the code, hopefully this works for your case. Keep in mind that you need to normalize mousewheel speed for all browsers and devices. Hammer.js is a good library for that.

var map, zoom;

function initialize() {

  // The white house!
  var myLatLng = new google.maps.LatLng(38.8977, -77.0365);

  // Default zoom level
  zoom = 17;

  // Keep a reference for evens
  var $map = document.getElementById("map");

  // Disable scrolling + double-click + dragging
  map = new google.maps.Map($map, {
    zoom: zoom,
    center: myLatLng,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    scrollwheel: false,
    disableDoubleClickZoom: true,
    //draggable: false
  });

  var marker = new google.maps.Marker({
    position: myLatLng,
    map: map
  });


  // EVENTS

  // Double Click: event zoom by 1
  map.addListener('dblclick', function(e) {
    zoomHandler(e.latLng, 1);
  });

  // Zoom changed: update current zoom level
  map.addListener('zoom_changed', function(e) {
    // Using a timeout because `getZoom()` does not return a promise.
    setTimeout(function() {
      zoom = map.getZoom();
    }, 50);

  });

  // Dom event: On mousewheel
  $map.addEventListener('mousewheel', wheelEvent, true);
}

// Mouse Scrolling event
function wheelEvent(event) {
  if (event.deltaY > 1)
    zoomHandler(event, -1);
  else
    zoomHandler(event, 1);
}

// Zoom in/out
function zoomHandler(event, zoomin) {
  zoom += zoomin;
  map.setZoom(zoom);
}

google.maps.event.addDomListener(window, 'load', initialize);
#map {
  width: 100%;
  height: 240px;
}
<div id="map"></div>
<script src=""></script>

You should try the center_changed event with panTo function.

google.maps.event.addListener(mymap, 'center_changed', function() {
    mymap.panTo({lat: myLoc.lat, lng: myLoc.lon});
})

You can see an example about it here and documentation about panTo function here. Becareful, it should mess up all the draggable thing.



 MORE:


 ? Type 'Promise<void>' is not assignable to type 'Marker'. Property '_objectInstance' is missing in type 'Promise<void>'. Ionic 2
 ? Google Map is not showing on live site using Avada theme
 ? Adding google tiles with R
 ? Chrome navigator.geolocation.getCurrentPosition() error 403
 ? How to move inside google maps only with two fingers?
 ? React Native: Alert message doesn't work when called frequently
 ? React native ToolbarAndroid action icon not working
 ? Android 6.0 Permission Error
 ? Android 6.0 Permission Error
 ? Android 6.0 Permission Error