捕捉到最近的标记方法总是返回false

Snapping to closest Marker Method always returning false

本文关键字:方法 返回 false 最近      更新时间:2024-05-12

希望您能帮助我解决这个问题:)

这是我的情况:
最近我一直在使用谷歌地图API。然后我创建了一张地图,上面有标记,表示对旅行路线的建议。每个标记的数据都存储在我的数据库中,这样每次我启动Web应用程序时,我都可以在地图上表示它们。然而,用户可以将标记移动到地图上的另一个地方,从而存储新的旅行路线
除了推荐的旅行,我还在数据库中存储同一国家的其他地点。这是由于用户能够移动其他标记来个性化他/她的旅行

这是我的问题:
使用Javascript,我会听取用户的Drop事件,然后我会得到该标记的新位置(它被丢弃的位置),并将其传递给另一个方法:

//point1: contains marker which was dropped with its new location
//location: String containing the name of the place where the marker was dropped
arePointsNear: function (point1, location) {
    //result: Obj I pass on to method: trip.getHotels in order to get a List containing
    //all Hotels in the region 'location'
    var result = { 'markerLat': point1.position.lat(), 'markerLng': point1.position.lng(), 'loc': location };
    Trip.getHotels(result, function (hotels) {
        var found = false;
        //i: used as index of the hotels Array(res)
        var i = 0
        while (!found) {
            //sw: SouthWest, ne: NorthEast
            var sw = new google.maps.LatLng(hotels[i].lat - 1, hotels[i].lng - 1);
            var ne = new google.maps.LatLng(hotels[i].lat + 1, hotels[i].lng + 1);
                var bounds = new google.maps.LatLngBounds(sw, ne);
                found = $.contains(bounds, point1);
                if (found) { break; }
                i++;
        }
    });
}


我现在基本上是想把掉下来的标记弹到我阵列中最近的酒店。上面的代码没有给出任何错误,但找到的var总是'false',尽管我很担心被丢弃的标记的位置在其中一家酒店的边界内

链接到相关问题捕捉到最近的标记

下面是我的问题
为什么发现总是假的?可能是我遗漏了什么吗?
问题出在jquery contains方法上,还是出在下面的行上:

var sw = new google.maps.LatLng(hotels[i].lat - 1, hotels[i].lng - 1);


我很高兴听到你的回答,我提前感谢你

编辑
在我的问题得到回答后,我尝试了用".contains"。由于某种原因,这仍然不起作用,所以我自己写了一个非常简单的方法来检测地图上与另一个点或标记最近的点或标记。我想与所有可能正在寻找的人分享这一点:

//marker: Point to which you want to find the closest Point to
arePointsClose: function (marker) {
    //Trip: This is my Object and getHotels a method within my Object trip
    //With getHotels I get a List of Hotels from the Controller using AJAX
    //I pass a function to getHotels (For more information research: Callback)
    Trip.getHotels(function (hotels) {
        var closestPoint = undefined;
        var closestHotel = 0;
        //Here I start looping through my array of hotels
        for (var i = 0; i < hotels.length ; i++) {
            // Here I maka an if in one line and if the number resulting should be negative I multiply by -1 to evit that
            var x = ((marker.position.lat() - hotels[i].lat) < 0) ? (marker.position.lat() - hotels[i].lat) * (-1) : (marker.position.lat() - hotels[i].lat);
            var y = ((marker.position.lng() - hotels[i].lng) < 0) ? (marker.position.lng() - hotels[i].lng) * (-1) : (marker.position.lng() - hotels[i].lng);
            var point = x + y;
            //The point var is overwritten for the purpose of checking later in the if, if it is actually smaller than the actual closestPoint
            if (closestPoint == undefined || point <= closestPoint) {
                closestPoint = point;
                closestHotel = hotels[i];
            }
        }
        //Now closestPoint contains just what you wanted. Pass it on to another function now to maybe redraw the markers (and) their path
        //Trip.yourFunction
    });
}

jQuery contains函数是用来处理DOM元素的,例如这个div包含那个span:吗

$.contains('#thisDiv', '#thatSpan')`;

如果Google Maps LatLngBounds对象包含特定点,则不会。

为此,您需要使用LatLngBounds自己的contains函数,例如:

var bounds = new google.maps.LatLngBounds(sw, ne);
found = bounds.contains(point1);