我想从谷歌地图获得所有纬度和经度。
例如:假设我输入了源和目的地的纬度和经度,并在地图上绘制路径,如下所示:
所以现在我想要这个A源和B目的地之间的所有纬度和经度。并且所有lat和lon都必须存储在数据库中。
所以,当我跟踪我的车并且那辆车没有沿着这条路径行驶时,那时它会触发一些动作,例如给我发电子邮件等。
我正在使用asp.net c#开发。
所以我怎么能得到所有的lat和lon?
我正在使用以下脚本
<script>
function initialize() {
var map = new google.maps.Map(document.getElementById('map_canvas'), {
zoom: 8,
center: new google.maps.LatLng(22.304634, 73.161070),
mapTypeId: google.maps.MapTypeId.ROADMAP
}),
directions = new google.maps.DirectionsService(),
displayer = new google.maps.DirectionsRenderer({
draggable: true
});
displayer.setMap(map);
directions.route({
origin: new google.maps.LatLng(22.304634, 73.161070),
destination: new google.maps.LatLng(23.022242, 72.548844),
travelMode: google.maps.DirectionsTravelMode.DRIVING
}, function (result) {
displayer.setDirections(result);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
google.maps.event.addListener(displayer, 'directions_changed', some_method);
</script>
答案 0 :(得分:5)
您必须遍历overview_path
对象的google.maps.DirectionsRoute
属性。然后创建您的对象(或仅序列化overview_path
)以将其发送到服务器并将其存储在数据库中。
了解如何循环overview_path
:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Geocoding service</title>
<style>
html, body, #map-canvas { height: 100%; min-height: 600px; min-width: 700px; margin: 0px; padding: 0px }
#map-canvas { height: 50%; }
#panel { position: absolute; top: 5px; left: 50%; margin-left: -180px; z-index: 5; background-color: #fff; padding: 5px; border: 1px solid #999; }
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
</head>
<body>
<div id="panel">
<label>Origin
<input id="origin" type="text" value="">
</label>
<label>Destination
<input id="destination" type="text" value="">
</label>
<input type="button" value="GetDirections" onclick="calcRoute()">
</div>
<div id="map-canvas"></div>
<div id="vertex-container">
<label>Points</label>
<ul id="vertex">
</ul>
</div>
<script type="text/javascript">
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer();
var mapOptions = {
zoom: 7,
center: new google.maps.LatLng(48.85727000, 2.35238)
};
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
directionsDisplay.setMap(map);
}
function calcRoute() {
var start = document.getElementById('origin').value;
var end = document.getElementById('destination').value;
var request = {
origin: start,
destination: end,
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function (response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
if (response.routes && response.routes.length > 0) {
var routes = response.routes;
for (var j = 0; j < routes.length; j++) {
var points = routes[j].overview_path;
var ul = document.getElementById("vertex");
for (var i = 0; i < points.length; i++) {
var li = document.createElement('li');
li.innerHTML = getLiText(points[i]);
ul.appendChild(li);
}
}
}
}
});
}
function getLiText(point) {
var lat = point.lat(),
lng = point.lng();
return "lat: " + lat + " lng: " + lng;
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</body>
</html>