开发者

How to calculate zoom level for Android Geocoder results

I'm using Android's Geocoder to allow searching for a location in my map-based app. The geocoding works well, and the app successfully recenters the MapView on the geocoding results. The problem is that I don't know what zoom level to use when moving to the new location. The location_query parameter could be a street or it could be a country -- how do I know how far to zoom in to display the resulting location? That is, what do I replace ??? with?

public void goToPlace(String location_query) {

    List<Address> addresses;
    Geocoder gc = new Geocoder(_ctx);

    addresses = gc.getFromLocationName(location_query, 1);
    if (addresses != null) {
        Address a = addresses.get(0);
        int 开发者_StackOverflowlat = (int) (a.getLatitude() * 1E6);
        int lon = (int) (a.getLongitude() * 1E6);
        mapController.animateTo(new GeoPoint(lat, lon));
        mapController.setZoom(???);
    }
}


My idea is to use the method getAddressLine for selecting the appropriate zoom level. As docs say:

Returns a line of the address numbered by the given index (starting at 0), or null if no such line is present.

So for a country, it returns only 1 line address, for city 2 lines, for street 3 lines. For each case you can use different zoom level.


The Android Geocoding APIs only provide latitude and longitude. On the other hand, the Web Geocoding APIs provide latitude, longitude AND viewport needed to zoom the map. The takeaway is that until Google decides to extend its Android APIs, you need to use the web APIs to get the missing viewport zoom data.

Luckily using the web APIs is not too hard thanks to the GeocoderPlus library, which does all the web API requests and JSON parsing for you and returns only Java objects as results. It has an interface that is similar to the native Geocoder and it is available in JAR format for inclusion into your Android app.

Check out the blog post below for usage: http://bricolsoftconsulting.com/perfect-geocoding-zoom-part-2/


You could do setZoom(18) or something like that as you have only one point to show.

If you have multiple points then what you need is zoomToSpan. What you need to do is find the min and max lat/lng pairs pass the difference between then to the above function to get the optimal zoom level.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜