Link to home
Start Free TrialLog in
Avatar of Refael
RefaelFlag for United States of America

asked on

Google Maps API - Keep marker position on zoom

I am using google maps api including MarkerWithLabel.
For some reason when changing the map zoom (using the map controls) markers seem to lose their exact location.
Code below. Please if someone can help to keep the markers in place when zooming in-out. Thank you!

var map = new google.maps.Map(document.getElementById("map"), {
  center: new google.maps.LatLng(52.554201,13.425703),
  zoom: 16,
  streetViewControl : false,
  overviewMapControl: false,
  mapTypeControl: false,
  zoomControl : true,
    zoomControlOptions: {
      position: google.maps.ControlPosition.RIGHT_BOTTOM
    },            
  panControl : false,
  scrollwheel: false,
  fullscreenControl: true,
  mapTypeId: google.maps.MapTypeId.ROADMAP
});

google.maps.event.addDomListener(window, "load", function () {

function createMarker(options, html) {
    var marker = new google.maps.Marker(options);
    return marker;
  };

var marker = createMarker({
    position: new google.maps.LatLng(56.554201,19.425703),
    map: map,
    cursor: 'hand',
    icon: "images/location.png",
  });

var marker2 = new MarkerWithLabel({
        position: new google.maps.LatLng(53.549140,14.428730),
        icon:'images/pointers.png',
        map: map,
        labelContent: "Agni <span>Shop</span>",
        labelAnchor: new google.maps.Point(44,86),
        labelClass: "labels",
        labelStyle: {opacity: 1}
     });

});

window.onresize = function() {
  var currCenter = new google.maps.LatLng(52.555564,13.426775);
  google.maps.event.trigger(map, 'resize');
  map.setCenter(currCenter);
};

Open in new window

Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Where are you adding the markers to your map?
You don't have problem with your marker and zoom.
Initial page:
User generated imageDe-zoom:
User generated imageDrag the picture center and zoom again on a marker...:
User generated imageThe marker is still there
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.