简体   繁体   中英

Generate new Lat/Lng between existing Lat/Lng provided by Google Maps Directions API

I am using the Google Maps Directions Service to request a path from a start point to an end point. This obviously returns a list of Lat/Lng markers which are joined to draw a path. The code looks like this:

  directionsService.route(request, function(result, status) {
    if (status == 'OK') {
      directionsDisplay.setDirections(result);
      var myRoute = result.routes[0].legs[0];
      //console.log(myRoute);

      for (var i = 0; i < myRoute.steps.length; i++) {
      var marker = new google.maps.Marker({
        position: myRoute.steps[i].start_point,
        map: map
      });
      console.log(myRoute.steps[i].start_point.lat(),myRoute.steps[i].start_point.lng() );
      attachInstructionText(marker, myRoute.steps[i].instructions);
      markerArray[i] = marker;
  }

    }
  });

在此处输入图片说明

I am having trouble figuring out an algorithm which will generate more Lat/Lng 'markers' between the existing ones.

To visualize this even further here is another diagram. Red X's show the points that the call returns. I have then added Blue X's on-top to show what I want the result to look like. 在此处输入图片说明

Thanks!!!

For each line connecting the red points:

Use this function to determine the distance between the two points. Figure out how close together you want the blue Xs to be - for example, 30m? 50m? Variable depending on zoom? Using that, determine how many new points you want to come up with between the two endpoints. Then, for each lat/lng, divide the difference between the endpoints by (new points + 1) to come up with how much lat/lng difference each new point should have.

For example: Start with endpoints

Lat 100, Lng 80

and

Lat 110, Lng 60

(distance too large, but this is just an example) Say you do a calculation and find that you need 4 additional points. Latitude difference between each point would be:

(110 - 100) / (4 + 1) = 10 / 5 = 2

Latitude difference between each point would be:

(60 - 80) / (4 + 1) = -20 / 5 = -4

Generate 4 new points, using those differences to calculate their lat/lng:

(100, 80) (start)
(102, 76)
(104, 72)
(106, 68)
(108, 64)
(110, 60) (end)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM