How to find location along a route with Google Maps?

前端 未结 1 1177
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-04 21:48

I\'ve been able to use the DirectionServices portion of the Google Maps API to find a route between two addresses. Given the starting address and a distance (e.g. 5 miles), I\'d

相关标签:
1条回答
  • 2021-02-04 22:16

    Check out http://code.google.com/apis/maps/documentation/flash/reference.html#Route

    Objects of this class are created by the Directions object to store information about a single route in a directions result.

    This allows you to use the getStep method. Steps provide you with distance, duration, latlng etc.

    http://code.google.com/apis/maps/documentation/flash/reference.html#Step

    In order to get the point at (e.g.) 5 miles, you could get the two points closest to this distance. From what I understand these points should be line of sight (i.e. point-to-point with no turns) so you should be able to extrapolate the correct point between them using math.

    There seems to be a related discussion here: http://groups.google.com/group/google-maps-js-api-v3/browse_thread/thread/a475d03a28865614/23ed9e966d10cdd8?pli=1

    Here is an example of an animation which shows the mileage, so I'd be surprised if you couldn't get something working by looping through the points until you've found the value you want (the source on this page should get you started): http://econym.org.uk/gmap/example_cartrip.htm

    Sorry I don't have a more concrete answer - I will try and write some code for this when I have some time.

    EDIT: if you look at the car_trip example, you will find the following:

    // === A method which returns the Vertex number at a given distance along the path ===
    // === Returns null if the path is shorter than the specified distance ===
    GPolygon.prototype.GetIndexAtDistance = function(metres) {
      // some awkward special cases
      if (metres == 0) return this.getVertex(0);
      if (metres < 0) return null;
      var dist=0;
      var olddist=0;
      for (var i=1; (i < this.getVertexCount() && dist < metres); i++) {
        olddist = dist;
        dist += this.getVertex(i).distanceFrom(this.getVertex(i-1));
      }
      if (dist < metres) {return null;}
      return i;
    }
    

    This should allow you to plot the point as desired.

    0 讨论(0)
提交回复
热议问题