使用从第一点行进的距离计算2点之间的坐标

时间:2014-01-03 15:40:03

标签: javascript google-maps google-maps-api-3

我正在尝试获取点a和b之间当前位置的地理位置坐标,只知道从a点开始的距离,假设用户已经走了一条直线

var disatnce_travelled = 375; //miles
function initialize() {
var locations = new Array(new google.maps.LatLng(40.7142700, -74.0059700),new google.maps.LatLng(43.585278, 39.720278));
var bounds = new google.maps.LatLngBounds();
for(i in locations)
    bounds.extend(locations[i]);
var mapOptions = {
 zoom: 3,
 center: bounds.getCenter(),
 mapTypeId: google.maps.MapTypeId.TERRAIN
};

var map = new google.maps.Map(document.getElementById('map-canvas'),
   mapOptions);

var flightPlanCoordinates = locations;
var flightPath = new google.maps.Polyline({
 path: flightPlanCoordinates,
 strokeColor: '#7f3f98',
 strokeOpacity: 1.0,
 strokeWeight: 2
});

flightPath.setMap(map);
}

google.maps.event.addDomListener(window, 'load', initialize);

1 个答案:

答案 0 :(得分:1)

google.maps.geomettry library有插值方法。 Returns the LatLng which lies the given fraction of the way between the origin LatLng and the destination LatLng.

然而,这似乎沿着测地路径插入。 example

一种选择是使用Mike Williams' epoly library

的v3端口
<script src="https://maps.googleapis.com/maps/api/js?v=3&sensor=false&libraries=geometry"></script>
<script src="scripts/v3_epoly.js"></script>
<script>
var distance_travelled = 375; //miles
function initialize() {
var locations = new Array(new google.maps.LatLng(40.7142700, -74.0059700),new google.maps.LatLng(43.585278, 39.720278));
var bounds = new google.maps.LatLngBounds();
for(i in locations)
    bounds.extend(locations[i]);
var mapOptions = {
 zoom: 2,
 center: bounds.getCenter(),
 mapTypeId: google.maps.MapTypeId.TERRAIN
};

var map = new google.maps.Map(document.getElementById('map-canvas'),
   mapOptions);

var flightPlanCoordinates = locations;
var flightPath = new google.maps.Polyline({
 path: flightPlanCoordinates,
 strokeColor: '#7f3f98',
 strokeOpacity: 1.0,
 strokeWeight: 2
});

flightPath.setMap(map);
var marker = new google.maps.Marker({
   position: flightPath.GetPointAtDistance(distance_travelled*1609.34),
   map:map});
}

google.maps.event.addDomListener(window, 'load', initialize);

working example