A simple geocoding example that loads an arbitrary address

All "simple examples" geocoding loads a hard-coded long / lat pair and then, after displaying the map, they load an arbitrary custom address. Unfortunately for me this means that the hardcoded location always appears first and then the map goes to the address provided by the user. I would like to know how to first geocode an arbitrary address and then initialize the map to the location in one step so that when the map is displayed on the screen, it already shows the correct location.

However, I was unable to figure out how this can be done from the API docs. All examples show "new google.maps.Map ()" which is called before geocoding and it looks like you already have room to create the map, so it is not possible to geocode before displaying the map. On the other hand, it seems to me that this would be the most common way to use google map, so of course, is there a way to do this?

Is this possible and does anyone have an example they could point to? Thank!


source to share

1 answer

If I understood you correctly, I created a simple page here


  • The HTML code ("Sardegna") has hard code for human readability.
  • Geocoder detects LatLng from Sardegna
  • The map is created and centered there.

To change the default map center, change the address in the Source HTML panel instead of the text box, then click the blue Run button.



All Articles