Javascript 谷歌地图Api检查地点是否在路线上
我用谷歌地图在两个地方之间画了一条路线。那很好 但我也有一个数据库,里面有关于世界上不同道路的有趣点 我的国家。我喜欢向他们展示他们是否在生成的路线上。是谁的地方 这条路线没有一条,不需要显示 我的数据库包含有趣的点,包括纬度和经度坐标 我怎样才能确认他们在我的路线上?大约有30或40个有趣的项目 我的数据库中的点Javascript 谷歌地图Api检查地点是否在路线上,javascript,google-maps,Javascript,Google Maps,我用谷歌地图在两个地方之间画了一条路线。那很好 但我也有一个数据库,里面有关于世界上不同道路的有趣点 我的国家。我喜欢向他们展示他们是否在生成的路线上。是谁的地方 这条路线没有一条,不需要显示 我的数据库包含有趣的点,包括纬度和经度坐标 我怎样才能确认他们在我的路线上?大约有30或40个有趣的项目 我的数据库中的点 // set request var request = { origin: initialLocation, destination: destination,
// set request
var request = {
origin: initialLocation,
destination: destination,
travelMode: google.maps.TravelMode.DRIVING
};
// make route
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
// set route
directionsDisplay.setDirections(response);
}
});
更新:
构建新功能“isLocationOnEdge”。但当标记不在道路上时,检查会运行标记:
// get flitsers on the route
function getFlitsersOnRoute(){
// set request
var request = {
origin: current_location,
destination: $('#gegevens').html(),
travelMode: google.maps.TravelMode.DRIVING
};
// make route
directionsService.route(request, function(response, status) {
// isLocationOnEdge
var isLocationOnEdge = google.maps.geometry.poly.isLocationOnEdge;
var coords = response.routes[0].overview_path;
var image = base_url+'external/afbeeldingen/google_markers/flitser.png';
// get flitsers
$.ajax({
type: "POST",
async:false,
url: base_url+"advies/get/9",
success: function (data) {
var result = jQuery.parseJSON(data);
// loop trough flitsers
$.each(result.flitsers.m, function(i, item) {
// latitude longitude
var latlng = item["@attributes"].gps.split(",");
// make google latlng
var myLatlng = new google.maps.LatLng(latlng[0],latlng[1]);
if (myLatlng,coords)
{
// set and define the marker
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
size: new google.maps.Size(15, 15),
icon: image,
title: 'Flitser'
});
}
});
}
});
});
}
在Google Maps API请求中包括几何体库:
http://maps.googleapis.com/maps/api/js?v=3.exp&
库=几何体
传感器=真或假
伪代码:
// Make isLocationOnEdge easier to access
var isLocationOnEdge = google.maps.geometry.poly.isLocationOnEdge;
for (var i = 0; i < interestingPlaces.length; i++) {
if (isLocationOnEdge(interestingPlaces[i],
response.routes[0].overview_path))
{
// Do something with interestingPlaces[i]
}
}
//使IsLocationNodePage更易于访问
var isLocationOnEdge=google.maps.geometry.poly.isLocationOnEdge;
对于(var i=0;i
在Google Maps API请求中包括几何体库:
http://maps.googleapis.com/maps/api/js?v=3.exp&
库=几何体
传感器=真或假
伪代码:
// Make isLocationOnEdge easier to access
var isLocationOnEdge = google.maps.geometry.poly.isLocationOnEdge;
for (var i = 0; i < interestingPlaces.length; i++) {
if (isLocationOnEdge(interestingPlaces[i],
response.routes[0].overview_path))
{
// Do something with interestingPlaces[i]
}
}
//使IsLocationNodePage更易于访问
var isLocationOnEdge=google.maps.geometry.poly.isLocationOnEdge;
对于(var i=0;i
isLocationOnEdge(点:板条、多边形、多边形、多段线、公差?:编号)
若要确定点是否落在多段线上或附近,或落在多边形边缘上或附近,请将该点、多段线/多边形以及可选的公差值(以度为单位)传递给google.maps.geometry.poly.isLocationOnEdge()。如果点与直线或边上最近点之间的距离在指定公差范围内,则函数返回true。默认公差值为10-9度
isLocationOnEdge(点:板条、多边形、多边形、多段线、公差?:编号)
若要确定点是否落在多段线上或附近,或落在多边形边缘上或附近,请将该点、多段线/多边形以及可选的公差值(以度为单位)传递给google.maps.geometry.poly.isLocationOnEdge()。如果点与直线或边上最近点之间的距离在指定公差范围内,则函数返回true。默认公差值为10-9度
将有趣的位置添加到数组中。然后,对于管线多段线中的每个坐标,根据有趣的位置数组进行检查。如果可能重复,请使用您的数据库而不是位置API。谢谢您的评论!下面的答案是我猜你答案的一个例子。你在哪里使用
isLocationOnEdge
?您的程序如何知道何时在线路上放置标记?新的google.maps.marker会创建标记。var仅用于将标记存储在以后的体育场中。将有趣的位置添加到数组中。然后,对于管线多段线中的每个坐标,根据有趣的位置数组进行检查。如果可能重复,请使用您的数据库而不是位置API。谢谢您的评论!下面的答案是我猜你答案的一个例子。你在哪里使用isLocationOnEdge
?您的程序如何知道何时在线路上放置标记?新的google.maps.marker会创建标记。var仅用于将标记存储在稍后的体育场中。感谢您的评论!这就是我要找的。我现在只有一个错误。它是否像:myLatlng=new google.maps.LatLng(LatLng[0],LatLng[1]);if(isLocationOnEdge(myLatlng,response.routes[0].overview_path)){}错误:“TypeError:b.get不是函数”如果没有更多的代码很难说,但看起来新的google.maps.LatLng(LatLng[0],LatLng[1])是你的问题。您似乎正在发送两个latLng
s以创建新的latLng
。API需要两个数字。所以google.maps.LatLng(Number,Number)
应该更好。我在问题中添加了一些代码。我的错误已解决,但脚本尚未运行。感谢您的评论!这就是我要找的。我现在只有一个错误。它是否像:myLatlng=new google.maps.LatLng(LatLng[0],LatLng[1]);if(isLocationOnEdge(myLatlng,response.routes[0].overview_path)){}错误:“TypeError:b.get不是函数”如果没有更多的代码很难说,但看起来新的google.maps.LatLng(LatLng[0],LatLng[1])是你的问题。您似乎正在发送两个latLng
s以创建新的latLng
。API需要两个数字。所以google.maps.LatLng(Number,Number)
应该更好。我在问题中添加了一些代码。我的错误已解决,但脚本尚未运行。