【问题标题】:Snapping to closest Marker Method always returning false捕捉到最近的标记方法总是返回 false
【发布时间】:2016-04-13 15:01:49
【问题描述】:

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

这是我的情况:
最近我一直在使用Google Maps 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 found 始终是 'false',即使我很确定放置标记的位置在 边界的酒店之一。

Link to Related question: Snap to nearest marker

这是我的问题...
为什么发现总是假的?会不会是我遗漏了什么?
问题出在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

    });
}

【问题讨论】:

    标签: javascript google-maps google-maps-api-3


    【解决方案1】:

    jQuery contains 函数用于处理 DOM 元素,例如这个 div 是否包含那个跨度:

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

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

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

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

    【讨论】:

    • 你是对的,$.contains 仅适用于 DOM 元素。我在 .contains 不起作用后尝试了它,由于某种原因它总是会出错。我最终找到了另一种方法来找到最近的点,我可能会在我的问题编辑中包含该点
    猜你喜欢
    • 2010-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多