2

I've been using the site for a long long time and it's been very helpful to me. Today for the first time I've come across a problem I couldn't find a solution for using the search-function.

Here it is: I have a big application around google maps and it is integral for it that I can zoom in the map to the absolute maximum. For most urban locations this maximum zoom level is 21, but when I execute setZoom(21) or setOptions({zoom: 21}) on the google maps object, it only goes to zoom level 19. Only when I execute setZoom(21) again for the same location and after a short wait does google maps actually go to the desired zoom level of 21. This issue only occurs when the zooming is executed from a function triggered by a geocoding event. If for example I do setZoom

Below you can see a stripped down version of the code and a JSFiddle plus a couple of reproduction steps to see the problem.

Please take a look at this JSFiddle: http://jsfiddle.net/mYUuR/4/ Steps to reproduce the strange behaviour:

  1. Run the fiddle and wait for the map to load.
  2. Open your console
  3. Press Search at the top left. (You will see the map zooming in and "Current Zoom Level: 19" logged in your console.
  4. Press Search a second time. (The map zooms in further and "Current Zoom Level: 21" will be logged to your console.

Here is my javascript:

var mapObj = initializeMap()
var geocoder;

function initializeMap() 
{

    geocoder = new google.maps.Geocoder();
    searchInput = document.getElementById("searchField");
    searchInput.focus();
    var searchButton = document.getElementById("searchButton");
    searchButton.addEventListener("click", codeAddress);


    var mapOptions = 
    {
        center: new google.maps.LatLng(53.70854553884514, 10.105501748621464),
        zoom: 3,            
        panControl: true,
        zoomControl: true,
        mapTypeControl: true,
        scaleControl: true,
        streetViewControl: true,
        overviewMapControl: true,
        draggable: true,
        mapTypeId: google.maps.MapTypeId.HYBRID
    };
    gMapObj = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);

    return gMapObj;
}
function codeAddress(e) {
    e.preventDefault();
    var address = searchInput.value;
    geocoder.geocode({
        "address": address
    },

    function (results, status) 
    {
        if (status == google.maps.GeocoderStatus.OK) 
        {
            google.maps.event.addListenerOnce(mapObj, "center_changed", function (e) 
            {
                mapObj.setZoom(21);                
                console.log("Current Zoom Level: " + mapObj.getZoom());
            });
            mapObj.setCenter(results[0].geometry.location);
        } else {
            alert("Search was not successful for the following reason: " + status);
        }
    })

}

Here is my HTML:

<div id="NavigationBar">
    <form>
        <input id="searchField" type="textbox" value="Hamburg" placeholder="Enter your address."></input>
        <input id="searchButton" type="submit" value="Search">
    </form>
</div>
<div id="PageContents">
    <div id="map-canvas"></div>
</div>

And here is my CSS:

#searchButton
{
    background-color: #FFF;
}

html { height: 100% }
body 
{ 
    height: 100%; margin: 0; padding: 0;
    position: relative;
}
#NavigationBar
{
    height: 5%;
    border-bottom-style: solid;
    border-bottom-width: 1px;
    border-bottom-color: #AAA;
}

#map-canvas
{
    height: 100%;
    width: 100%;
}

#PageContents
{
    position: relative;
    height: 95%;
    width: 100%;
}

Things I've already tried:

  • Executing a second zoom directly after the first.
  • Executing a second zoom once the first zoom is done using an eventListener listening for "zoom_changed".

Code Example for that:

google.maps.event.addListenerOnce(mapObj, "zoom_changed", function(e){mapObj.setZoom(21)});

I would very much appreciate any suggestions or thoughts.

Leonard
  • 29
  • 3
  • 1
    The zoom level depends on some factors like location/long lat, the map type etc. – Umesh Aawte Jan 20 '14 at 19:50
  • It's question if this is malfunction. I don't know explanation but it seems that this is already known. See question [google map API zoom range](http://stackoverflow.com/questions/9356724/google-map-api-zoom-range), answer and code example from user bkaid. He is always triggering the 2nd click to get the max zoom. – Anto Jurković Jan 20 '14 at 23:34
  • Yeah, there are quite a few posts about this on stackoverflow and on google groups, but none of them found a fix or a reason for this strange behaviour. The way I've ended up solving it, is to just execute another zoom to maximum on a setTimeout of 500ms. It is a question of delaying it sufficiently, so something in the background has to be loaded or triggered. A delay of 200ms between zooms works sometimes, but sometimes it doesn't. 500ms seems to always give the site enough time to do its business. Once I test this out on a real server and slower connections I'll report back. – Leonard Jan 21 '14 at 15:54

0 Answers0