问题描述
||
我需要允许旅行者使用谷歌地图绘制路线,然后查询我的兴趣点数据库(例如麦当劳的位置),然后显示一英里或两英里内的所有这些位置他们将要走的路线。问题是,如何有效地获取来自Google的“行车路线”信息(本质上是一个经纬度对数组),然后将其转换为SQL查询以获取位于距路线?
它不必是非常精确的,并且“随着鸟儿飞翔”与路线的距离就很好。我最担心的是它的合理效率。
在数据库中,基本上每个条目都有一个纬度和经度,就可以进行设置,但是我可以根据需要更改数据库模式。
举例来说,这个网站做了我想做的事情(如果您指定起点和终点,它将显示您要驶入的高速公路附近的人字形车站):
http://www.chevron.com/products/stations/stationfinder/planyourroute.aspx
(来源:karmatics.com)
解决方法
看一下这个
http://google-maps-utility-library-v3.googlecode.com/svn/tags/routeboxer/1.0/examples/routeboxer-v3.html
这里是文档:http://google-maps-utility-library-v3.googlecode.com/svn/tags/routeboxer/1.0/docs/examples.html
您可以从RouteBoxer获取框坐标,并将其发送到服务器端脚本进行处理
,
<!DOCTYPE html>
<html>
<head>
<meta name=\"viewport\" content=\"initial-scale=1.0,user-scalable=no\">
<meta charset=\"utf-8\">
<title>Polygon arrays</title>
<style>
#map {
height: 100%;
}
html,body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div id=\"map\"></div>
<script>
var map;
function initMap() {
map = new google.maps.Map(document.getElementById(\'map\'),{
center: {lat: 19.2570,lng: 72.8712},zoom: 10,});
var directionService = new google.maps.DirectionsService();
var directionsRenderer = new google.maps.DirectionsRenderer({ map: map });
var request = {
origin: \"<?php echo $source;?>\",destination: \"<?php echo $destination;?>\",travelMode: google.maps.DirectionsTravelMode.DRIVING
}
directionService.route(request,function(result,status) {
if (status == google.maps.DirectionsStatus.OK) {
var path = result.routes[0].overview_path;
var poly = new google.maps.Polyline({
strokeColor: \'#FF0000\',strokeOpacity: 1.0,strokeWeight: 3,map: map,});
poly.setPath(path);
var myTollLocations = [
<?php
isset($hello); //$hello is array which comes from database
foreach ($hello as $key => $value) {
?>
new google.maps.LatLng(<?php echo $hello[$key]->latitude;?>,<?php echo $hello[$key]->longitude;?>),<?php
}
?>
];
for (var i = 0; i < myTollLocations.length ; i++) {
if (google.maps.geometry.poly.isLocationOnEdge(myTollLocations[i],poly,0.005)) {
console.log(\"found\");
}else{
console.log(\"Not Found!\");
}
};
<?php
$markersLocation = array($source,$destination);
foreach ($markersLocation as $key => $values) {
?>
//Source Marker( convert address to LatLng for marker)
var geocoder = new google.maps.Geocoder();
geocoder.geocode( { \'address\': \'<?php echo $markersLocation[$key]?>\'},function(results,status) {
if (status == google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
}
console.log(latitude);
console.log(longitude);
var myLatLng = {lat: latitude,lng: longitude};
var marker = new google.maps.Marker({
position: myLatLng,title: \'source\',icon: \'http://innowrap.com/clients/digitoll/ic_red_marker.png\'
});
});
<?php
}
?>
} else {
alert(\"Directions query failed: \" + status);
}
});
}
</script>
<script src=\"https://maps.googleapis.com/maps/api/js?key=ADD YOUR API KEY&libraries=geometry&callback=initMap\"
async defer></script>
</body>
</html>