12 April 2010

Google maps: getMaxZoomAtLatLng and error 500

When I had to catch zoom level when 'no images available for this zoom level', I thought I had pretty quickly found the solution. But I was completely disappointed when this one didn't work. After some research I've managed to make it work :) First of all I needed this in GWT application. We were using GWT Maps (1.0.4 by that time) which lack this function as such. So here is how we can bridge this function using JSNI:
class GoogleMapsJsBridgeSample {
    /**
     * Wrapper function to native call
     */
    public void getMaxZoomAtLatLng(MapType mapType, double lat, double lon) {
        getMaxZoomAtLatLng(mapType.getPeer(), lat, lon);
    }
    
    private native void getMaxZoomAtLatLng(JavaScriptObject mapTypePeer, double lat, double lon) /*-{
        var latLng = new $wnd.GLatLng(lat, lon);
        mapTypePeer.getMaxZoomAtLatLng(latLng, function(response) {
            if (response && response['status'] == $wnd.G_GEO_SUCCESS) {
                this@com.company.my.GoogleMapsJsBridgeSample::doSomethingWithMaxZoomAtLatLng(I)(response['zoom'])-*/;
            }
        });
    }
    
    private void doSomethingWithMaxZoomAtLatLng(int maxZoom) {
        getMap().setZoom(maxZoom);
    }
}

The point here is is NOT using MapWidget.getCurrentMapType() due a bug in gmaps-api (not GWT specific). According to the comments for the issue, problem appears only for hybrid map type, but for satellite and normal it is ok! So you can use to code above in the following manner in GWT:

GoogleMapsJsBridgeSample jsBridge = new GoogleMapsJsBridgeSample();
// ...
jsBridge.getMaxZoomAtLatLng(MapType.getSatelliteMap(), lat, lon);
// or use MapType.getNormalMap() for street map view

Such issues are sometimes specific to the version of gmaps-api you are using. You can always make sure what particular version is used in your application by outputting G_API_VERSION JavaScript variable.

Changelogs for each gmaps-api version are available at http://mapki.com/

Post a Comment