Google Maps API V3 fitBounds()无法正常工作

爱默生

我想fitBounds()在Google地图上致电,以便用户可以看到他们相对于选定标记/位置的位置。

这是我的函数来处理此问题:

var fitMapToShowResult = function(index){
    var hereLatLng = new google.maps.LatLng (lat,lng); //location of the user defined outside this func
    var firstResultLatLng = new google.maps.LatLng(
                                $scope.searchResults[index].geometry.location.k,
                                $scope.searchResults[0].geometry.location.B
    );
    var latlngList = new Array( hereLatLng, firstResultLatLng ); // latlng: an array of instances of GLatLng
    var latlngbounds = new google.maps.LatLngBounds();
    for (var i=0;i<latlngList.length;i++){
       latlngbounds.extend(latlngList[i]);
       console.log('latlngbounds',latlngbounds);
    }
    //$scope.map.setCenter(latlngbounds.getCenter());
    $scope.map.fitBounds(latlngbounds); 

};

大约80%的时间它都能正常工作。但是大约五分之一的标记完全看不见,并且缩放比例太高,无法同时显示两个点。我究竟做错了什么?

我的地图使用自定义标记可能是相关的。

为了协助调试,我添加了以下代码以在地图上绘制边界...

    rect = new google.maps.Rectangle( {bounds: latlngbounds, map: $scope.map} );

对于前几个结果,它总是看起来很完美: 在此处输入图片说明

但是通常这是不正确的:在此处输入图片说明 在此处输入图片说明请注意,在每种情况下,如果不正确,则矩形的一个尺寸(高度/宽度)似乎是正确的,而另一个尺寸是不正确的。我的直觉告诉我这很重要。

相似问题综述

我知道这是一个很普遍的问题,但是我已经复习了所有其他问题,而我的问题似乎并没有重复。希望此库存对将来的疑难解答人员很有用,但是这些都无法解决我的问题。

谷歌地图V3 fitBounds不准确无用的问题,没有代码,没有答案

Google Maps API v3-fitBounds仅将一个标记居中。用户正在重新定义循环内部而没有意识到。

Using setZoom() after using fitBounds() with Google Maps API V3 & Google Maps API V3 fitbounds() zooms out but never in & Google Maps with fitBounds don't zoom fitBounds() happens asyncronously so downstream actions need to be wrapped in eventListener.

google maps → fitBounds manually User was passing incorrect type parameters to LatLngBounds (should be two google.maps.LatLngs)

google maps fitBounds() is broken or..? & Google maps API V3 method fitBounds() Was constructing the google.maps.LatLngBounds with buggy coordinates (instead of leaving it empty and using .extend())

Trying to get Google Maps fitbounds to work Super noob did not realize javascript methods were case sensitive

Google Maps V3自定义标记图像和fitBounds() fitBounds()可以正常工作,但用户需要为错误自定义标记留出更多空间

可见标记上的Google Maps V3 fitBounds关于如何使用fitBounds()... rtfm的简单问题

Google Maps API V3-fitBounds随机起作用,但偶尔会忽略海洋中的边界和载荷,在撰写本文时没有答案。听起来像无效的经/纬对。

爱默生

乌普斯当先生是对的。我的边界框是错误的,因为我....geometry.location.k直接引用而不是LatLng对象。重构代码相应地解决了该问题。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章