I have this function that adds the new marker to the google maps.
But center does not work. Any advise?
function addMarker(lat, lng, name){
new google.maps.Marker({
map: map,
icon: image,
position: new google.maps.LatLng(lat, lng),
title: name,
center: new google.maps.LatLng(lat, lng),
zoom: 6
});
}
But center does not work.
When you create a new marker object via the Google Maps API, you can call the map object’s
setCenterfunction, passing in the marker’s position:That should center the map on the marker.
If you want to animate the process, use
panToinstead:It should also be noted that the
centerandzoomproperties you’re attempting to set in the constructor for the Marker object do not exist. Those are properties of the map object itself and need to be set there.I imagine something like this:
I’d also consider adding
mapandimageto the parameter list ofaddMarkerinstead of using “global” scoped objects, or I’d forego using a function at all. That is kind of nit-picky and depends on the need of your code.More Info Here