简体   繁体   English

谷歌地图API V3 fitBounds()不起作用

[英]Google maps API V3 fitBounds() not working

I'm want to call fitBounds() on a google map so that a user can see their own location relative to a selected marker/location. 我想在谷歌地图上调用fitBounds() ,以便用户可以看到自己相对于所选标记/位置的位置。

Here is my function which handles this: 这是我的函数处理这个:

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); 

};

It works perfectly about 80% of the time. 它大约80%的时间都能很好地工作。 But roughly 1 in 5 times the marker is completely out of view and the zoom is way too high for the two points to ever be visible at the same time. 但是,标记的大约五分之一完全不在视野范围内,并且变焦太高,以至于两个点无法同时显示。 What am I doing wrong? 我究竟做错了什么?

It may be relevant that my map uses custom markers. 我的地图使用自定义标记可能是相关的。

To assist with debugging, I added this code to draw the bounds on the map... 为了帮助调试,我添加了这段代码来绘制地图上的边界...

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

It always looks perfect for the first couple results: 对于前几个结果,它总是看起来很完美: 在此输入图像描述

But often it's incorrect: 但往往是不正确的: 在此输入图像描述在此输入图像描述 Notice that in each of the cases where it's incorrect, it appears that one of the dimensions of the rectangle (height/width) is correct, but the other is not. 请注意,在每个不正确的情况下,看起来矩形的一个尺寸(高度/宽度)是正确的,而另一个则不正确。 My gut tells me this is relevant. 我的直觉告诉我这是相关的。

Roundup of similar questions 类似问题综述

I know this is a popular question, but I've reviewed all of the others and my issue does not seem to be a duplicate of any of them. 我知道这是一个很受欢迎的问题,但我已经审查了所有其他问题,而且我的问题似乎与其中任何一个都没有重复。 Hopefully this stockpile will be useful to future troubleshooters, but none of these solved my issue. 希望这个库存对未来的故障排除者有用,但这些都没有解决我的问题。

google maps V3 fitBounds not accurate Useless question with no code and no answers 谷歌地图V3 fitBounds不准确无用的问题,没有代码,没有答案

Google Maps API v3 - fitBounds centers only one marker User was re-defining the inside a loop without realizing it. 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. 使用fitBounds()后使用setZoom()与Google Maps API V3Google Maps API V3 fitbounds()缩小但从未 使用fitBoundsGoogle Maps不会缩放 fitBounds()异步发生,因此下游操作需要包含在eventListener中。

google maps → fitBounds manually User was passing incorrect type parameters to LatLngBounds (should be two google.maps.LatLng s) google maps→fitBounds手动用户将错误的类型参数传递给LatLngBounds (应该是两个google.maps.LatLng s)

google maps fitBounds() is broken or..? google maps fitBounds()坏了还是..? & Google maps API V3 method fitBounds() Was constructing the google.maps.LatLngBounds with buggy coordinates (instead of leaving it empty and using .extend() ) Google maps API V3方法fitBounds()使用错误坐标构建google.maps.LatLngBounds (而不是将其留空并使用.extend()

Trying to get Google Maps fitbounds to work Super noob did not realize javascript methods were case sensitive 试图让谷歌地图适合工作超级noob并没有意识到javascript方法区分大小写

Google maps V3 custom marker images and fitBounds() fitBounds() works as expected but user needed to allow more space for bug custom markers 谷歌地图V3自定义标记图像和fitBounds() fitBounds()按预期工作,但用户需要为bug自定义标记提供更多空间

Google Maps V3 fitBounds on visible markers Simple question about how to use fitBounds() ... rtfm 关于可见标记的Google Maps V3 fitBounds关于如何使用fitBounds() ... rtfm的简单问题

Google Maps API V3 - fitBounds randomly works but occasionally ignores bounds and loads in the middle of the ocean No answers at time of writing. 谷歌地图API V3 - fitBounds随机工作但偶尔会忽略海洋中的边界和载荷在撰写本文时没有答案。 Sounds like invalid lat/lng pairs. 听起来像无效的lat / lng对。

不要将值用作geometry.location.**k** ,它取决于最小化的文件(使用MinifyJS等工具),然后当谷歌发布新的版本时它会发生变化。

MrUpsidown was right. Mr.Upsidown是对的。 My bounding box was wrong because I was referencing ....geometry.location.k directly instead of the LatLng object. 我的边界框是错误的,因为我直接引用....geometry.location.k而不是LatLng对象。 Refactoring the code accordingly corrected the problem. 相应地重构代码纠正了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM