【发布时间】:2014-01-25 08:02:03
【问题描述】:
我使用 Google 地图制作了两地之间的路线。效果很好。
但我也有一个数据库,其中包含不同道路上的有趣点 我的国家。我喜欢向他们展示他们是否在生成的路线上。有的地方 这条路线没有一条,不需要显示。
我的带有兴趣点的数据库包含纬度和经度坐标。
我如何检查他们是否在我的路线上?大约有 30 或 40 个有趣的 点在我的数据库中。
// 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'
});
}
});
}
});
});
}
【问题讨论】:
-
将有趣的地方添加到数组中。然后对于路线折线中的每个坐标,将其与感兴趣的地点数组进行检查。
-
How to to Get Places (e.g Gas Stations) along Route Between Origin and Destination in Google Maps API 的可能重复项,请使用您的数据库而不是场所 API。
-
感谢 cmets!下面的答案是我猜你的答案的一个例子。
-
你在哪里使用
isLocationOnEdge?您的程序如何知道何时在行上放置标记? -
新的 google.maps.Marker 创建标记。 var 仅用于将标记存储在以后的体育场中。