Constant error return on google maps maxZoomService.getMaxZoomAtLatLng()

戏子无情 提交于 2020-02-04 14:31:33

问题


I'm experiencing a big problem with googles maxZoomService.getMaxZoomAtLatLng, since it absolutely random decides to return error state, sometimes 10 times in a row, sometimes it works, but I can't figure out why the zoom sometimes just fails.

I tried to solve it by retrying the request multiple times, but it seems like that if I get an error on the first request I'll get the error for all retries too. At the same time I'm facing an issue that got listed in another topic but this isn't my main problem, it just occurs too.

Following my example code:

setMap : function() {
    wirkonf.frontend.triggerLoadAnim();
    wirkonf.storage.mapOptions = {
        zoom: 11,
        center: new google.maps.LatLng(wirkonf.storage.geoCoords.lat, wirkonf.storage.geoCoords.lng),
        mapTypeId: google.maps.MapTypeId.SATELLITE,
        disableDefaultUI: true
    };
    wirkonf.storage.map = new google.maps.Map(document.getElementById('gmc'),
    wirkonf.storage.mapOptions);
    wirkonf.storage.map.setTilt(0);
    var maxZoomService = new google.maps.MaxZoomService();
    maxZoomService.getMaxZoomAtLatLng(wirkonf.storage.mapOptions.center,
        function(response){
            wirkonf.storage.mapZoomMax = response.zoom;
            var ret;
            if (response.zoom > 20) {
                ret = 20;
            } else {
                ret = response.zoom;
            }
            wirkonf.storage.mapZoomCurr = ret;
            wirkonf.storage.map.setZoom(ret);
        }
    );
},

So my question is, has anyone else experienced this problem or maybe even knows a good workaround to solve it?


回答1:


I had the same issue, but noticed that the actual GET request actually return with a valid json response "{zoom:19}" but for some reason got converted into an error inside the google maps js code.

I then tried to simply bypass the google javascript and hit the service directly:

var myMap = {
    maxZoomServiceCallback: function(resp) {
        gmap.setZoom(resp.zoom);
    },
    isAerialAvailable: function() {
        var ll = gmap.getCenter();
        var proj = gmap.getProjection();
        if(!proj) {
            setTimeout(function(){myMap.isAerialAvailable();},100);
            return;
        }   
        var p = proj.fromLatLngToPoint(new google.maps.LatLng(ll.lat, ll.lon));
        p.x=Math.floor(p.x / 256 * 8388608);
        p.y=Math.floor(p.y / 256 * 8388608);
        jQuery.getScript("https://khm.googleapis.com/mz?v=199&x="+p.x+"&y="+p.y+"&z=23&token=0&callback=myMap.maxZoomServiceCallback");
        //maxZoomService.getMaxZoomAtLatLng({lng: ll.lon, lat: ll.lat}, this.maxZoomServiceCallback)
    }
}

After I got this code with the undefined/null check for gmap.getProjection() to work, I assumed that the projection being null could be the same reason the maxZoomService kept failing (probably caused by some underlying timing issue), I then tried the maxZoomService again, but keeping the undefined check. This time it worked. Please try it out for yourselves but it works me for now.

Edit 14/4: I have since removed maxZoomService.getMaxZoomAtLatLng again and gone back to my custom jQuery.getScript call as this worked consistently.

Update 5/5/2016: Google seems to have made some changes and release a new API version. I am getting better results with:

jQuery.getScript("https://khm.googleapis.com/mz?v=203&x="+p.x+"&y="+p.y+"&z=23&token=0&callback=myMap.maxZoomServiceCallback"



来源:https://stackoverflow.com/questions/28339217/constant-error-return-on-google-maps-maxzoomservice-getmaxzoomatlatlng

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!