计算给定时间的地理位置 [英] Calculate the geo position at given time

查看:85
本文介绍了计算给定时间的地理位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Google API指示我可以将所有信息从一个地方移到另一个地方,我也可以完成所有步骤。



但是我不知道在给定时间之后是否有方法检索/计算位置(Lat / Lng)。假设,为了简化,我的速度是不变的,我有办法回答这个问题:我将在X分钟内到达哪里?

注意



我知道距离旅行=时间*速度但通过这种方式,我知道我旅行了多少公里。我想知道的是一段时间后的经纬度。

工作示例,使用 epoly.js迈克威廉姆斯的第三方库已移植到v3中

  var directionDisplay; 
var directionsService = new google.maps.DirectionsService();
var map;
var polyline = null;
$ b $函数createMarker(latlng,label,html){
// alert(createMarker(+ latlng +,+ label +,+ html +,+ color +) );
var contentString ='< b>'+ label +'< / b>< br>'+ html;
var marker = new google.maps.Marker({
position:latlng,
map:map,
title:label,
zIndex:Math.round(latlng .lat()* - 100000)<5
});
marker.myname = label;
// gmarkers.push(marker);

google.maps.event.addListener(marker,'click',function(){
infowindow.setContent(contentString);
infowindow.open(map,marker);
});
返回标记;


函数initialize(){
directionsDisplay = new google.maps.DirectionsRenderer({suppressMarkers:true});
var chicago = new google.maps.LatLng(41.850033,-87.6500523);
var myOptions = {
zoom:6,
mapTypeId:google.maps.MapTypeId.ROADMAP,
center:chicago
}
map = new google .maps.Map(document.getElementById(map_canvas),myOptions);
polyline = new google.maps.Polyline({
path:[],
strokeColor:'#FF0000',
strokeWeight:3
});
directionsDisplay.setMap(map);
calcRoute();


函数calcRoute(){

var start = document.getElementById(start).value;
var end = document.getElementById(end).value;
var travelMode = google.maps.DirectionsTravelMode.DRIVING

var request = {
origin:start,
destination:end,
travelMode:travelMode
};
directionsService.route(request,function(response,status){
if(status == google.maps.DirectionsStatus.OK){
polyline.setPath([]);
)var bounds = new google.maps.LatLngBounds();
startLocation = new Object();
endLocation = new Object();
directionsDisplay.setDirections(response);
var route = response.routes [0];
var summaryPanel = document.getElementById(directions_panel);
summaryPanel.innerHTML =;

//对于每条路线,显示摘要信息
var path = response.routes [0] .overview_path;
var legs = response.routes [0] .legs;
for(i = 0; i if(i == 0){
startLocation.latlng = legs [i] .start_location;
startLocation.address = legs [i] .start_address;
// marker = google.maps.Marker({map:map,position:startLocation.latlng});
marker = createMarker(legs [i] .start_location,start,legs [i] .start_address,green);
}
endLocation.latlng = legs [i] .end_location;
endLocation.address = legs [i] .end_address;
var steps = legs [i] .steps;
for(j = 0; j var nextSegment = steps [j] .path; (k = 0; k polyline.getPath()。push(nextSegment [k]);
;
bounds.extend(nextSegment [k]);
}
}
}

polyline.setMap(map);

computeTotalDistance(response);
} else {
alert(directions response+ status);
}
});
}

var totalDist = 0;
var totalTime = 0;
函数computeTotalDistance(结果){
totalDist = 0;
totalTime = 0;
var myroute = result.routes [0];
for(i = 0; i< myroute.legs.length; i ++){
totalDist + = myroute.legs [i] .distance.value;
totalTime + = myroute.legs [i] .duration.value;
}
totalDist = totalDist / 1000.
document.getElementById(total)。innerHTML =total distance is:+ totalDist +km< br> total time is:+ (totalTime / 60).toFixed(2)+minutes;
document.getElementById(totalTime)。value =(totalTime / 60。)。toFixed(2);
}

函数putMarkerOnRoute(time){
var distance =(time * 60 / totalTime)* totalDist * 1000;
// alert(Time:+ time +totalTime:+ totalTime +totalDist:+ totalDist +dist:+ distance);
if(!marker){
marker = createMarker(polyline.GetPointAtDistance(distance),time:+ time,marker);
} else {
marker.setPosition(polyline.GetPointAtDistance(distance));
marker.setTitle(time:+ time);
}
}



v3_epoly.js

  / ********************************* ************************************ \ 
* *
* epolys .js by Mike Williams *
*由Larry Ross更新至API v3 *
* *
* Google Maps API扩展*
* *
*添加各种方法到google.maps.Polygon和google.maps.Polyline *
* *
* .GetPointAtDistance()会返回沿路径指定距离* b $ b *的GLatLng。 *
*距离以米为单位指定*
*如果路径短于此值,则返回null *
* *
* .GetPointsAtDistance()返回一个GLatLng数组*
*指定的路径间隔。 *
*距离以米为单位指定*
* *
*************************** ********************************************
* *
*此Javascript由Mike Williams提供*
* Blackpool Community Church Javascript Team *
* http://www.blackpoolchurch.org/ *
* http:// econym .org.uk / gmap / *
* *
*本作品以Creative Commons License *
* http://creativecommons.org/licenses/by/2.0/uk/ *
* *
*************************************** ****************************** **
* *
*版本1.1 2007年6月6日*
*版本1.2 2007年7月1日 - 修正:边界省略顶点零*
* add:轴承*
*版本1.3 2008年11月28日add:GetPointsAtDistance()*
*版本1.4 2009年1月12日修复:GetPointsAtDistance()*
* 3.0版本2010年8月11日更新到v3 *
* *
\ *********************************** ********************************** /

// ===首先支持(v3)中尚未存在的方法
google.maps.LatLng.prototype.distanceFrom = function(newLatLng){
var EarthRadiusMeters = 6378137.0; //米
var lat1 = this.lat();
var lon1 = this.lng();
var lat2 = newLatLng.lat();
var lon2 = newLatLng.lng();
var dLat =(lat2-lat1)* Math.PI / 180;
var dLon =(lon2-lon1)* Math.PI / 180;
var a = Math.sin(dLat / 2)* Math.sin(dLat / 2)+
Math.cos(lat1 * Math.PI / 180)* Math.cos(lat2 * Math。 PI / 180)*
Math.sin(dLon / 2)* Math.sin(dLon / 2);
var c = 2 * Math.atan2(Math.sqrt(a),Math.sqrt(1-a));
var d = EarthRadiusMeters * c;
return d;
}

google.maps.LatLng.prototype.latRadians = function(){
return this.lat()* Math.PI / 180;


google.maps.LatLng.prototype.lngRadians = function(){
return this.lng()* Math.PI / 180;
}

// ===一种以米为单位返回路径长度的方法===
google.maps.Polygon.prototype.Distance = function(){
var dist = 0;
for(var i = 1; i< this.getPath()。getLength(); i ++){
dist + = this.getPath()。getAt(i).distanceFrom(this.getPath ().getAt第(i-1));
}
return dist;
}

// ===一种方法,沿路径返回给定距离的点的GLatLng ===
// ===如果路径比指定距离短===
google.maps.Polygon.prototype.GetPointAtDistance =函数(米){
//一些特殊情况
if(meters == 0)return this.getPath()getAt(0)。
if(meters <0)返回null;
if(this.getPath()。getLength()< 2)return null;
var dist = 0;
var olddist = 0; $ i
for(var i = 1;(i olddist = dist;
dist + = this.getPath()。getAt(i).distanceFrom(this.getPath()。getAt(i-1));
}
if(dist return null;
}
var p1 = this.getPath()。getAt(i-2);
var p2 = this.getPath()。getAt(i-1);
var m =(meters-olddist)/(dist-olddist);
return new google.maps.LatLng(p1.lat()+(p2.lat() - p1.lat())* m,p1.lng()+(p2.lng() - p1.lng ())*米);
}

// ===一种方法,它返回沿路径给定间隔的点的GLatLngs数组===
google.maps.Polygon.prototype.GetPointsAtDistance =函数(米){
var next =米;
var points = [];
//一些特殊情况
if(meters <= 0)返回点;
var dist = 0;
var olddist = 0; $ i
(var i = 1;(i< this.getPath()。getLength()); i ++){
olddist = dist;
dist + = this.getPath()。getAt(i).distanceFrom(this.getPath()。getAt(i-1));
while(dist> next){
var p1 = this.getPath()。getAt(i-1);
var p2 = this.getPath()。getAt(i);
var m =(next-olddist)/(dist-olddist);
points.push(new google.maps.LatLng(p1.lat()+(p2.lat() - p1.lat())* m,p1.lng()+(p2.lng() - p1.lng())* M));
next + =米;
}
}
回报点;
}

// ===将上述所有函数复制到GPolyline ===
google.maps.Polyline.prototype.Distance = google.maps.Polygon.prototype。距离;
google.maps.Polyline.prototype.GetPointAtDistance = google.maps.Polygon.prototype.GetPointAtDistance;
google.maps.Polyline.prototype.GetPointsAtDistance = google.maps.Polygon.prototype.GetPointsAtDistance;


With Google API directions I can get all the information to go from one place to another and I can get all the steps as well.

But I don't know if there is a way to retrieve/calculate the position(Lat/Lng) after a given time. Assuming, to simplify, my speed is constant is there I way to answer the question: where will I be in X minutes ?

NOTE

I know that Distance travelled = Time * Speed but in this way I know how many KM I travelled. What I want to know is the (Lat/Lng) after a period of time.

解决方案

working example using GetPointAtDistance from the epoly.js third party library from Mike Williams ported to v3

  var directionDisplay;
  var directionsService = new google.maps.DirectionsService();
  var map;
  var polyline = null;

function createMarker(latlng, label, html) {
// alert("createMarker("+latlng+","+label+","+html+","+color+")");
    var contentString = '<b>'+label+'</b><br>'+html;
    var marker = new google.maps.Marker({
        position: latlng,
        map: map,
        title: label,
        zIndex: Math.round(latlng.lat()*-100000)<<5
        });
        marker.myname = label;
        // gmarkers.push(marker);

    google.maps.event.addListener(marker, 'click', function() {
        infowindow.setContent(contentString); 
        infowindow.open(map,marker);
        });
    return marker;
}

  function initialize() {
    directionsDisplay = new google.maps.DirectionsRenderer({suppressMarkers:true});
    var chicago = new google.maps.LatLng(41.850033, -87.6500523);
    var myOptions = {
      zoom: 6,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      center: chicago
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    polyline = new google.maps.Polyline({
    path: [],
    strokeColor: '#FF0000',
    strokeWeight: 3
    });
    directionsDisplay.setMap(map);
    calcRoute();
  }

  function calcRoute() {

    var start = document.getElementById("start").value;
    var end = document.getElementById("end").value;
    var travelMode = google.maps.DirectionsTravelMode.DRIVING

    var request = {
        origin: start,
        destination: end,
        travelMode: travelMode
    };
    directionsService.route(request, function(response, status) {
      if (status == google.maps.DirectionsStatus.OK) {
        polyline.setPath([]);
        var bounds = new google.maps.LatLngBounds();
        startLocation = new Object();
        endLocation = new Object();
        directionsDisplay.setDirections(response);
        var route = response.routes[0];
        var summaryPanel = document.getElementById("directions_panel");
        summaryPanel.innerHTML = "";

        // For each route, display summary information.
    var path = response.routes[0].overview_path;
    var legs = response.routes[0].legs;
        for (i=0;i<legs.length;i++) {
          if (i == 0) { 
            startLocation.latlng = legs[i].start_location;
            startLocation.address = legs[i].start_address;
            // marker = google.maps.Marker({map:map,position: startLocation.latlng});
            marker = createMarker(legs[i].start_location,"start",legs[i].start_address,"green");
          }
          endLocation.latlng = legs[i].end_location;
          endLocation.address = legs[i].end_address;
          var steps = legs[i].steps;
          for (j=0;j<steps.length;j++) {
            var nextSegment = steps[j].path;
            for (k=0;k<nextSegment.length;k++) {
              polyline.getPath().push(nextSegment[k]);
              bounds.extend(nextSegment[k]);
            }
          }
        }

        polyline.setMap(map);

        computeTotalDistance(response);
      } else {
        alert("directions response "+status);
      }
    });
  }

var totalDist = 0;
var totalTime = 0;
      function computeTotalDistance(result) {
      totalDist = 0;
      totalTime = 0;
      var myroute = result.routes[0];
      for (i = 0; i < myroute.legs.length; i++) {
        totalDist += myroute.legs[i].distance.value;
        totalTime += myroute.legs[i].duration.value;      
      }
      totalDist = totalDist / 1000.
      document.getElementById("total").innerHTML = "total distance is: "+ totalDist + " km<br>total time is: " + (totalTime / 60).toFixed(2) + " minutes";
      document.getElementById("totalTime").value = (totalTime/60.).toFixed(2);
      }

      function putMarkerOnRoute(time) {
        var distance = (time*60/totalTime) * totalDist*1000;
        // alert("Time:"+time+" totalTime:"+totalTime+" totalDist:"+totalDist+" dist:"+distance);
    if (!marker) {
          marker = createMarker(polyline.GetPointAtDistance(distance),"time: "+time,"marker");
    } else {
          marker.setPosition(polyline.GetPointAtDistance(distance));
          marker.setTitle("time:"+time);
        }
      }

v3_epoly.js

/*********************************************************************\
*                                                                     *
* epolys.js                                          by Mike Williams *
* updated to API v3                                  by Larry Ross    *
*                                                                     *
* A Google Maps API Extension                                         *
*                                                                     *
* Adds various Methods to google.maps.Polygon and google.maps.Polyline *
*                                                                     *
* .GetPointAtDistance() returns a GLatLng at the specified distance   *
*                   along the path.                                   *
*                   The distance is specified in metres               *
*                   Reurns null if the path is shorter than that      *
*                                                                     *
* .GetPointsAtDistance() returns an array of GLatLngs at the          *
*                   specified interval along the path.                *
*                   The distance is specified in metres               *
*                                                                     *
***********************************************************************
*                                                                     *
*   This Javascript is provided by Mike Williams                      *
*   Blackpool Community Church Javascript Team                        *
*   http://www.blackpoolchurch.org/                                   *
*   http://econym.org.uk/gmap/                                        *
*                                                                     *
*   This work is licenced under a Creative Commons Licence            *
*   http://creativecommons.org/licenses/by/2.0/uk/                    *
*                                                                     *
***********************************************************************
*                                                                     *
* Version 1.1       6-Jun-2007                                        *
* Version 1.2       1-Jul-2007 - fix: Bounds was omitting vertex zero *
*                                add: Bearing                         *
* Version 1.3       28-Nov-2008  add: GetPointsAtDistance()           *
* Version 1.4       12-Jan-2009  fix: GetPointsAtDistance()           *
* Version 3.0       11-Aug-2010  update to v3                         *
*                                                                     *
\*********************************************************************/

// === first support methods that don't (yet) exist in v3
google.maps.LatLng.prototype.distanceFrom = function(newLatLng) {
  var EarthRadiusMeters = 6378137.0; // meters
  var lat1 = this.lat();
  var lon1 = this.lng();
  var lat2 = newLatLng.lat();
  var lon2 = newLatLng.lng();
  var dLat = (lat2-lat1) * Math.PI / 180;
  var dLon = (lon2-lon1) * Math.PI / 180;
  var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
    Math.cos(lat1 * Math.PI / 180 ) * Math.cos(lat2 * Math.PI / 180 ) *
    Math.sin(dLon/2) * Math.sin(dLon/2);
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
  var d = EarthRadiusMeters * c;
  return d;
}

google.maps.LatLng.prototype.latRadians = function() {
  return this.lat() * Math.PI/180;
}

google.maps.LatLng.prototype.lngRadians = function() {
  return this.lng() * Math.PI/180;
}

// === A method which returns the length of a path in metres ===
google.maps.Polygon.prototype.Distance = function() {
  var dist = 0;
  for (var i=1; i < this.getPath().getLength(); i++) {
    dist += this.getPath().getAt(i).distanceFrom(this.getPath().getAt(i-1));
  }
  return dist;
}

// === A method which returns a GLatLng of a point a given distance along the path ===
// === Returns null if the path is shorter than the specified distance ===
google.maps.Polygon.prototype.GetPointAtDistance = function(metres) {
  // some awkward special cases
  if (metres == 0) return this.getPath().getAt(0);
  if (metres < 0) return null;
  if (this.getPath().getLength() < 2) return null;
  var dist=0;
  var olddist=0;
  for (var i=1; (i < this.getPath().getLength() && dist < metres); i++) {
    olddist = dist;
    dist += this.getPath().getAt(i).distanceFrom(this.getPath().getAt(i-1));
  }
  if (dist < metres) {
    return null;
  }
  var p1= this.getPath().getAt(i-2);
  var p2= this.getPath().getAt(i-1);
  var m = (metres-olddist)/(dist-olddist);
  return new google.maps.LatLng( p1.lat() + (p2.lat()-p1.lat())*m, p1.lng() + (p2.lng()-p1.lng())*m);
}

// === A method which returns an array of GLatLngs of points a given interval along the path ===
google.maps.Polygon.prototype.GetPointsAtDistance = function(metres) {
  var next = metres;
  var points = [];
  // some awkward special cases
  if (metres <= 0) return points;
  var dist=0;
  var olddist=0;
  for (var i=1; (i < this.getPath().getLength()); i++) {
    olddist = dist;
    dist += this.getPath().getAt(i).distanceFrom(this.getPath().getAt(i-1));
    while (dist > next) {
      var p1= this.getPath().getAt(i-1);
      var p2= this.getPath().getAt(i);
      var m = (next-olddist)/(dist-olddist);
      points.push(new google.maps.LatLng( p1.lat() + (p2.lat()-p1.lat())*m, p1.lng() + (p2.lng()-p1.lng())*m));
      next += metres;    
    }
  }
  return points;
}

// === Copy all the above functions to GPolyline ===
google.maps.Polyline.prototype.Distance             = google.maps.Polygon.prototype.Distance;
google.maps.Polyline.prototype.GetPointAtDistance   = google.maps.Polygon.prototype.GetPointAtDistance;
google.maps.Polyline.prototype.GetPointsAtDistance  = google.maps.Polygon.prototype.GetPointsAtDistance;

这篇关于计算给定时间的地理位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆