Ben Walker
Posts: 0
Joined: Mon Jan 19, 2015 2:13 pm

placing markers on google map changes center of map

I am initializing a g-map using geolocation to set the initial center. Dragging the map changes the center.

However, whenever I run a function to place markers from a DB onto the map, the map's center always changes back to the initialized value, rather than remaining wherever it has been dragged to.

Can someone tell me why this is happening please? I want the map to remain at the position it was dragged to, even when markers are placed...

Thanks

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

placing markers on google map changes center of map

Hello Ben,

You can use method setCenter:

map.setCenter({lat: -34, lng: 151});

More examples here:

https://developers.google.com/maps/do...
https://devcenter.appery.io/tutorials... (item 6)

Ben Walker
Posts: 0
Joined: Mon Jan 19, 2015 2:13 pm

placing markers on google map changes center of map

Thanks Sergiy - I understand the setCenter method - but that doesn't explain why the center reverts back to the initialized value after adding markers.

The code I'm using to list the markers is as follows:

function arrayOpenMarkers() {

Code: Select all

 var map = Apperyio('map').gmap; 

 for(var i = 0; i < data.length; i++) { 

     var latitude = data[i].openSpotGeopoint[0]; 
     var longitude = data[i].openSpotGeopoint[1]; 

     var createdAt = data[i]._createdAt; 

     var arr = createdAt.split(/[- :]/), 
         date = new Date(arr[0], arr[1]-1, arr[2], arr[3], arr[4], arr[5]); 

     var ms1 = Date.parse(date); 
     var ms2 = new Date().getTime(); 
     var minutesOpen = Math.round((ms2 - ms1)/1000/60 + 420); 

     if (minutesOpen < 10) {            

         var position = new google.maps.LatLng(latitude, longitude); 

         var marker = new google.maps.Marker({ 
             position: position, 
             map: map, 
             icon: '[url=http://maps.gstatic.com/mapfiles/markers2/icon_green.png]http://maps.gstatic.com/mapfiles/mark...[/url]', 
             markerContent: "Spot has been open for " + minutesOpen + " minutes!" 
         }); 

         openMarkerArray.push(marker); 
     } 
 } 

 for (var j = 0; j < openMarkerArray.length; j++) { 
     var marker = openMarkerArray[j]; 
     google.maps.event.addListener(marker, 'click', function() { 
         infowindow.setContent(this.markerContent); 
         infowindow.open(map, this); 
     }); 
 } 

}

arrayOpenMarkers();

Is there something in there that will cause the map center to change?

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

placing markers on google map changes center of map

Add setCenter after marker has been added.

Return to “Issues”