Enlarge Google Maps APIs - Different for City and Country

so .. I ran into a possibly very common problem.

just started to implement google maps api, and the following my code resolves the city in lat / lang and centers the map there:

function SetMapAddress(address) { // "London, UK" for example var geocoder = new google.maps.Geocoder(); if (geocoder) { geocoder.geocode({ 'address': address }, function (results, status) { if (status == google.maps.GeocoderStatus.OK) { var loc = results[0].geometry.location; document.map.setCenter(new google.maps.LatLng(loc.lat(),loc.lng(), 13)); } 

the problem is that I am missing static zoom (13).

If someone calls the name of the country, I would like to increase the scale. if it is a city, I would like to zoom in, etc.

The only thing I can think of is to determine the appropriate scale for each city and country, save them in some hash and try to figure out what is used to go through the appropriate scaling.

Did Google think of a smarter approach?

+4
source share
3 answers

Geocoder returns "recommended" viewport

What you can use in your SetMapAddress function as follows:

  function SetMapAddress(address) { // "London, UK" for example var geocoder = new google.maps.Geocoder(); if (geocoder) { geocoder.geocode({ 'address': address }, function (results, status) { if (status == google.maps.GeocoderStatus.OK) { document.map.fitBounds(results[0].geometry.viewport); } }); } } 
+6
source

The geocoding result provides an array of address_components containing the types of address addresses for the request.

From my very limited testing, the more information is added in the request, the longer this address_components array becomes. When entering France, this is only the following:

 > Object long_name: "France" short_name: "FR" types: Array[2] > 0: "country" > 1: "political" 

When a city is added, there is a type called "locality". So you can go through this array, checking if there is a match between long_names and what the user entered, if only a city or country is typed, it’s easy, but there are many options like Rome / Rome Italy (spelling differences), and if the user entered the city and country, you must give priority to the city.

In the end, it sounds like a very fuzzy search and match, even if you created your own hash to match the user input of possible place images.

Here is my lazy approach:

Create var mapZoom = 13; (suppose this is a city)

Check if all user input is really the name of the country: if it matches long_name and the record type is "country", reduce mapZoom to 5.

Apply setCenter using this mapZoom variable.

+1
source

Another (sometimes problematic) solution is to count the length of the address_components array.

As Tina CG Hoehr mentioned in another answer, the places object has an address_components array. The array contains different parts of the address.

You can check the length of the address_components array and set the scaling level accordingly.

Based on your sample code,

 function SetMapAddress(address) { // "London, UK" for example var geocoder = new google.maps.Geocoder(); if (geocoder) { geocoder.geocode({ 'address': address }, function (results, status) { if (status == google.maps.GeocoderStatus.OK) { var loc = results[0].geometry.location; // Test address_components var ac_length = results[0].address_components.length; if (ac_length == 1) { // Assume country document.map.setCenter(new google.maps.LatLng(loc.lat(),loc.lng(), 3)); } else if (ac_length == 2) { // Assume city document.map.setCenter(new google.maps.LatLng(loc.lat(),loc.lng(), 7)); } else { // Everything else can have a standard zoom level document.map.setCenter(new google.maps.LatLng(loc.lat(),loc.lng(), 13)); } } } } 

This method seems to work fine. Checking this at Australian addresses, I found that some suburbs have a zip code, and some do not - change the length of the array. Those who did not have zip codes seemed to be in less populated areas, however, for this it was necessary to scale down for those that suited my purposes.

0
source

All Articles