Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically Finding Appropriate Zoom for Geocoding Result

I'm using Google Maps and Google Geocoding service for my location service application. I use Google Geocoding service for translating address to lat/lng position. My problem is how to automatically find an appropriate zoom for a certain address like the maps.google.com does.

For example, when I search a street in maps.google.com (e.g. Cisitu Baru, Bandung), it will show the street in smaller zoom. When I search a region (e.g. Bandung), it will show larger zoom. And a larger zoom for province (e.g. Jawa Barat / West Java), and so on.

I have tried both

var geocoder = new google.maps.Geocoder();
geocoder.geocode( {
    'address': someAddress
}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        console.dir(results);
        //cut
        map.panToBounds(results[0].geometry.bounds); //setting bound
        //cut
    }
});

and

//cut
map.panToBounds(results[0].geometry.viewports); //setting bound
//cut

(Honestly, I still don't know what's the difference between bounds and viewport and what are their uses from code.google.com/apis/maps/documentation/javascript/geocoding.html)

but both still don't display the map in appropriate zoom.

Right now, I use a small hack like this

var tabZoom =  {
    street_address: 15,
    route: 15,
    sublocality: 14,
    locality: 13,
    country: 10
};
//cut
map.setCenter(results[0].geometry.location);
if (tabZoom[results[0].types[0]] != undefined){
    map.setZoom(tabZoom[results[0].types[0]]);
} else {
    map.zetZoom(10);
}
//cut

Is there other solution? (Or anything from Google Map API that I don't know yet?)

Thanks!

like image 842
Petra Barus Avatar asked Jan 09 '12 08:01

Petra Barus


People also ask

How accurate is Google Geocoding?

Results. Respectively 81.4% and 84.4% of addresses were geocoded to the exact address (65.1% and 61.4%) or to the street segment (16.3% and 23.0%) with methods A and B.

How do you test geocoding?

Geocoding request and response (latitude/longitude lookup) You can test this by entering the URL into your web browser (be sure to replace YOUR_API_KEY with your actual API key). The response includes the latitude and longitude of the address.

What is the maximum limit for the number of geocode requests?

While there is no maximum number of requests per day, the following usage limit is still in place for the Geocoding API: 50 requests per second, calculated as the sum of client-side and server-side queries.


2 Answers

use GLatLngBounds class

an example:

// map: an instance of GMap2

// latlng: an array of instances of GLatLng

var latlngbounds = new GLatLngBounds( );

for ( var i = 0; i < latlng.length; i++ )
{
    latlngbounds.extend( latlng[ i ] );
}

map.setCenter( latlngbounds.getCenter( ), map.getBoundsZoomLevel( latlngbounds ) );

^

The trick is to add the list of all points that need to be visible on the map simultaneously into a GLatLngBounds object. The Google Maps API can do the rest of the maths.

or in v3 you can use LatLngBounds class (similar to GLatLngBounds in v2), link: http://code.google.com/apis/maps/documentation/javascript/reference.html#LatLngBounds

for an example better check this out: http://unicornless.com/code/google-maps-v3-auto-zoom-and-auto-center

like image 160
Arief Latu Suseno Avatar answered Sep 29 '22 18:09

Arief Latu Suseno


use viewport of the result geometry. if your search result does not have specific bounds, you will get an error with geometry.bounds

viewport gives you best view for the result.

map.fitBounds(results[0].geometry.viewport);

like image 33
Yevgen Avatar answered Sep 29 '22 18:09

Yevgen