捕捉到最近的标记方法总是返回false
Snapping to closest Marker Method always returning false
希望您能帮助我解决这个问题:)
这是我的情况:
最近我一直在使用谷歌地图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);
- 从Javascript方法返回全局变量
- Meteor方法在客户端返回null,在客户端运行的相同方法返回正确的值
- AngularJS错误:提供程序必须从$get工厂方法返回值
- 修复AngularJS错误:提供程序必须从$get工厂方法返回值
- 从 javascript 中的对象方法返回一个对象
- 为什么我的Mongoose findAll方法返回500错误.
- 角度方法返回未绘制的代码
- 解析查询查找方法返回对象而不是数组
- Jquery Ajax调用没有't点击操作方法/返回整个HTML页面
- ajax方法返回错误时如何打印输出
- Meteor 方法返回客户端和服务器上的不同对象
- 如何将异步方法返回的值发送到javascript中的另一个异步方法
- Javascript getDay方法返回错误的数字
- Coffeescription类中的方法返回函数而不是字符串
- 当使用Q/promises/异步函数时,如何将一个值从一个类方法返回到另一个类
- find()方法返回具有不需要的属性的对象
- 如何在JSF中将toDataURL方法返回的画布内容作为图像存储在磁盘上
- 从请求.js请求方法返回结果
- jQuery的.children()方法返回'undefined'
- Angularjs 控制器在服务方法返回之前从服务中分配变量