如何确定点K(lat,lng)是否在Google Map中从A(lat,lng)到B(lat,lng)的简单折线上

时间:2015-08-14 16:47:22

标签: google-maps

我在谷歌地图中有一条折线,从A点(lat,lng)到B(lat,lng)。如果有人给我一个点K(lat,lng)并且说它在从A(lat,lng)到B(lat,lng)或B(lat,lng)到A(lat,lng)的折线上,那么如何我可以确定它是真的吗?我正在考虑以下内容:

if A.lat <= K.lat AND B.lat >= K.lat OR B.lat <= K.lat AND A.lat >= K.lat then 
        K is on the line
    else 
        K is not on the line 

这种逻辑应该有效吗?我问,因为在我的测试中,大多数情况下它起作用但是在一些罕见的情况下它不起作用,即使这一点实际上是从线上获取的。

注意:使用Google Map Javascrpt API

1 个答案:

答案 0 :(得分:3)

请参阅the documenation for google.maps.geometry.poly.isLocationOnEdge

isLocationOnEdge(point:LatLng, poly:Polygon|Polyline, tolerance?:number)
boolean 
Computes whether the given point lies on or near to a polyline, or the edge of a polygon, within a specified tolerance. Returns true when the difference between the latitude and longitude of the supplied point, and the closest point on the edge, is less than the tolerance. The tolerance defaults to 10-9 degrees.

proof of concept fiddle

代码段

 public static void runCC(String software,String id,String name,String job,String country)
       {
         SocketServer dc = new SocketServer(software,id,name,job,country);
         String ServerReplyMessage = dc.getDownload();
         JOptionPane.showMessageDialog(null,"Downloading :\n" +ServerReplyMessage);
         int answer = JOptionPane.showConfirmDialog(null, "Do you want to continue?", "Confirm",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
        if (answer == JOptionPane.NO_OPTION)
        {
            JOptionPane.showMessageDialog(null,"Please click close button");
        }
       else
       {
           JOptionPane.showMessageDialog(null,"Please proceed");
       }
var map;
var infowindow = new google.maps.InfoWindow();
function initialize() {
  map = new google.maps.Map(document.getElementById('map'), {
    zoom: 3,
    center: {lat: 0, lng: -180},
    mapTypeId: google.maps.MapTypeId.TERRAIN
  });

  var flightPlanCoordinates = [
    {lat: 37.772, lng: -122.214},
    {lat: 21.291, lng: -157.821},
    {lat: -18.142, lng: 178.431},
    {lat: -27.467, lng: 153.027}
  ];
  var flightPath = new google.maps.Polyline({
    path: flightPlanCoordinates,
    geodesic: true,
      clickable: false,
    strokeColor: '#FF0000',
    strokeOpacity: 1.0,
    strokeWeight: 2
  });

  flightPath.setMap(map);
  google.maps.event.addListener(map,'click',function(e) {
      var marker = new google.maps.Marker({
          position: e.latLng,
          map: map,
          draggable: true
      });
      var contentStr;
      if (google.maps.geometry.poly.isLocationOnEdge(marker.getPosition(), flightPath, 1e-5)) {
          contentStr = "on line";
      } else { 
          contentStr = "not on line";
      }
      marker.contentStr = contentStr;
      google.maps.event.addListener(marker,'click',function(e) {
          infowindow.setContent(this.contentStr+"<br>"+this.getPosition().toUrlValue(6)+"<br><a href='javascript:map.setCenter(new google.maps.LatLng("+this.getPosition().toUrlValue()+"));map.setZoom(19);'>Zoom In</a>");
          infowindow.open(map,this);
      });
      google.maps.event.addListener(marker, 'dragend', function() {
        if (google.maps.geometry.poly.isLocationOnEdge(this.getPosition(), flightPath, 1e-5)) {
          contentStr = "on line";
        } else { 
          contentStr = "not on line";
        }
        this.contentStr = contentStr;
      });
      google.maps.event.trigger(marker,'click');
  });
      
    
}

google.maps.event.addDomListener(window, "load", initialize);
html, body, #map {
    height: 100%;
    width: 100%;
    margin: 0px;
    padding: 0px
}

相关问题