Google Maps API - how to set center and zoom for current marker?

30,973

The map object has setZoom() and setCenter() functions for the same. Refer to:

https://developers.google.com/maps/documentation/javascript/events#MarkerEvents

Share:
30,973
user984621
Author by

user984621

Updated on June 30, 2020

Comments

  • user984621
    user984621 almost 4 years

    When I initialize google maps in my app, I do something like this:

    var myOptions = {
      center: new google.maps.LatLng(39.729001, -94.902342),
      zoom: 3,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map"), myOptions);
    setUpMarker(map);
    

    Which works, however, I would need to set up the center of the map and zoom for the specific marker that is created by the function setUpMarker.

    I've tried inside the setUpMarker function to do this:

    fromMarker = new google.maps.Marker({
      center: fromLatlng,
      zoom: 5,
      map: map,
      title: "Here"
    });
    

    But it didn't help. Thus, how do achieve of this effect?

    Thank you guys for help.

  • user984621
    user984621 almost 10 years
    Thank you. The event in the addListener function is set up on the click value. When the marker is clicked, then the map is zoomed and centered. But I would need it automatically, when the map is loaded, I would need to apply on the marker setZoom() and setCenter(), so after the map would loaded, the marker would be automatically zoomed and centered. Is there any way to do that? Thank you
  • Quin
    Quin almost 10 years
    Don't keep it in the listener. Just declare the map and your marker and write the setZoom() setCenter() statements after that. You don't need to literally copy the example. Just see how it works.