2013-05-07 3 views
8

के निकटतम है मेरे पास एक पॉलीन है जिसे मैंने Google मानचित्र दिशा-निर्देश सेवा से प्राप्त latlngs के साथ खींचा है। अब मैं पॉलीलाइन पर एक बिंदु खोजना चाहता हूं जो किसी दिए गए बिंदु के सबसे नज़दीक है।एक पॉलीलाइन में एक बिंदु खोजें जो एक latlng

स्पष्ट तरीका (मेरे लिए) पॉलीलाइन में सभी बिंदुओं के माध्यम से लूप की तरह है और उनके और दिए गए बिंदु के बीच की दूरी को ढूंढना है, हालांकि यह अक्षम है क्योंकि पॉलीलाइन पर अंक संभावित रूप से बड़े हो सकते हैं।

मुझे ऐसा करने के किसी भी विकल्प को सुनकर खुशी होगी। अग्रिम धन्यवाद।

उत्तर

8

देखें विधेयक चाडविक यहाँ उदाहरण:

http://www.bdcc.co.uk/Gmaps/BdccGmapBits.htm

above example ported to v3 (कोड पर इस उत्तर के निचले भाग)

उनके पृष्ठ पर:

पॉलीलाइन या बहुभुज

कि पद से

को

दूरी सूत्री:

एक समान, बेहतर यहाँ डेमो http://wtp2.appspot.com/cSnapToRouteDemo.html

यह माउस के लिए लाइन पर निकटतम बिंदु खोजने जाता है। यह भी ध्यान रखें कि यह एक Google मानचित्र एपीआई v2 उदाहरण है (लेकिन v3 के साथ सिद्धांत समान होगा)।

// Code to find the distance in metres between a lat/lng point and a polyline of lat/lng points 
// All in WGS84. Free for any use. 
// 
// Bill Chadwick 2007 
// updated to Google Maps API v3, Lawrence Ross 2014 

     // Construct a bdccGeo from its latitude and longitude in degrees 
     function bdccGeo(lat, lon) 
     { 
      var theta = (lon * Math.PI/180.0); 
      var rlat = bdccGeoGeocentricLatitude(lat * Math.PI/180.0); 
      var c = Math.cos(rlat); 
      this.x = c * Math.cos(theta); 
      this.y = c * Math.sin(theta); 
      this.z = Math.sin(rlat);   
     } 
     bdccGeo.prototype = new bdccGeo(); 

     // internal helper functions ========================================= 

     // Convert from geographic to geocentric latitude (radians). 
     function bdccGeoGeocentricLatitude(geographicLatitude) 
     { 
      var flattening = 1.0/298.257223563;//WGS84 
      var f = (1.0 - flattening) * (1.0 - flattening); 
      return Math.atan((Math.tan(geographicLatitude) * f)); 
     } 

     // Returns the two antipodal points of intersection of two great 
     // circles defined by the arcs geo1 to geo2 and 
     // geo3 to geo4. Returns a point as a Geo, use .antipode to get the other point 
     function bdccGeoGetIntersection(geo1, geo2, geo3, geo4) 
     { 
      var geoCross1 = geo1.crossNormalize(geo2); 
      var geoCross2 = geo3.crossNormalize(geo4); 
      return geoCross1.crossNormalize(geoCross2); 
     } 

     //from Radians to Meters 
     function bdccGeoRadiansToMeters(rad) 
     { 
      return rad * 6378137.0; // WGS84 Equatorial Radius in Meters 
     } 

     //from Meters to Radians 
     function bdccGeoMetersToRadians(m) 
     { 
      return m/6378137.0; // WGS84 Equatorial Radius in Meters 
     } 

     // properties ================================================= 


     bdccGeo.prototype.getLatitudeRadians = function() 
     { 
      return (bdccGeoGeographicLatitude(Math.atan2(this.z, 
       Math.sqrt((this.x * this.x) + (this.y * this.y))))); 
     } 

     bdccGeo.prototype.getLongitudeRadians = function() 
     { 
      return (Math.atan2(this.y, this.x)); 
     } 

     bdccGeo.prototype.getLatitude = function() 
     { 
      return this.getLatitudeRadians() * 180.0/Math.PI; 
     } 

     bdccGeo.prototype.getLongitude = function() 
     { 
      return this.getLongitudeRadians() * 180.0/Math.PI ; 
     } 

     // Methods ================================================= 

     //Maths 
     bdccGeo.prototype.dot = function(b) 
     { 
      return ((this.x * b.x) + (this.y * b.y) + (this.z * b.z)); 
     } 

     //More Maths 
     bdccGeo.prototype.crossLength = function(b) 
     { 
      var x = (this.y * b.z) - (this.z * b.y); 
      var y = (this.z * b.x) - (this.x * b.z); 
      var z = (this.x * b.y) - (this.y * b.x); 
      return Math.sqrt((x * x) + (y * y) + (z * z)); 
     } 

     //More Maths 
     bdccGeo.prototype.scale = function(s) 
     { 
      var r = new bdccGeo(0,0); 
      r.x = this.x * s; 
      r.y = this.y * s; 
      r.z = this.z * s; 
      return r; 
     } 

     // More Maths 
     bdccGeo.prototype.crossNormalize = function(b) 
     { 
      var x = (this.y * b.z) - (this.z * b.y); 
      var y = (this.z * b.x) - (this.x * b.z); 
      var z = (this.x * b.y) - (this.y * b.x); 
      var L = Math.sqrt((x * x) + (y * y) + (z * z)); 
      var r = new bdccGeo(0,0); 
      r.x = x/L; 
      r.y = y/L; 
      r.z = z/L; 
      return r; 
     } 

     // point on opposite side of the world to this point 
     bdccGeo.prototype.antipode = function() 
     { 
      return this.scale(-1.0); 
     } 






     //distance in radians from this point to point v2 
     bdccGeo.prototype.distance = function(v2) 
     { 
      return Math.atan2(v2.crossLength(this), v2.dot(this)); 
     } 

     //returns in meters the minimum of the perpendicular distance of this point from the line segment geo1-geo2 
     //and the distance from this point to the line segment ends in geo1 and geo2 
     bdccGeo.prototype.distanceToLineSegMtrs = function(geo1, geo2) 
     {    

      //point on unit sphere above origin and normal to plane of geo1,geo2 
      //could be either side of the plane 
      var p2 = geo1.crossNormalize(geo2); 

      // intersection of GC normal to geo1/geo2 passing through p with GC geo1/geo2 
      var ip = bdccGeoGetIntersection(geo1,geo2,this,p2); 

      //need to check that ip or its antipode is between p1 and p2 
      var d = geo1.distance(geo2); 
      var d1p = geo1.distance(ip); 
      var d2p = geo2.distance(ip); 
      //window.status = d + ", " + d1p + ", " + d2p; 
      if ((d >= d1p) && (d >= d2p)) 
       return bdccGeoRadiansToMeters(this.distance(ip)); 
      else 
      { 
       ip = ip.antipode(); 
       d1p = geo1.distance(ip); 
       d2p = geo2.distance(ip); 
      } 
      if ((d >= d1p) && (d >= d2p)) 
       return bdccGeoRadiansToMeters(this.distance(ip)); 
      else 
       return bdccGeoRadiansToMeters(Math.min(geo1.distance(this),geo2.distance(this))); 
     } 

     // distance in meters from GLatLng point to GPolyline or GPolygon poly 
     function bdccGeoDistanceToPolyMtrs(poly, point) 
     { 
      var d = 999999999; 
      var i; 
      var p = new bdccGeo(point.lat(),point.lng()); 
      for(i=0; i<(poly.getPath().getLength()-1); i++) 
       { 
        var p1 = poly.getPath().getAt(i); 
        var l1 = new bdccGeo(p1.lat(),p1.lng()); 
        var p2 = poly.getPath().getAt(i+1); 
        var l2 = new bdccGeo(p2.lat(),p2.lng()); 
        var dp = p.distanceToLineSegMtrs(l1,l2); 
        if(dp < d) 
         d = dp;  
       } 
      return d; 
     } 

     // get a new GLatLng distanceMeters away on the compass bearing azimuthDegrees 
     // from the GLatLng point - accurate to better than 200m in 140km (20m in 14km) in the UK 

     function bdccGeoPointAtRangeAndBearing (point, distanceMeters, azimuthDegrees) 
     { 
      var latr = point.lat() * Math.PI/180.0; 
      var lonr = point.lng() * Math.PI/180.0; 

      var coslat = Math.cos(latr); 
      var sinlat = Math.sin(latr); 
      var az = azimuthDegrees* Math.PI/180.0; 
      var cosaz = Math.cos(az); 
      var sinaz = Math.sin(az); 
      var dr = distanceMeters/6378137.0; // distance in radians using WGS84 Equatorial Radius 
      var sind = Math.sin(dr); 
      var cosd = Math.cos(dr); 

      return new google.maps.LatLng(Math.asin((sinlat * cosd) + (coslat * sind * cosaz)) * 180.0/Math.PI, 
      (Math.atan2((sind * sinaz), (coslat * cosd) - (sinlat * sind * cosaz)) + lonr) * 180.0/Math.PI); 
     } 
+0

पहला लिंक किया गया डेमो अब और काम नहीं करता है (v3 API)। – heltonbiker

+0

पहला उदाहरण एक बिंदु से एक रेखा तक दूरी ढूंढ रहा है। दूसरा लिंक लाइन पर सबसे नज़दीकी बिंदु ढूंढकर प्रश्न का उत्तर देता है। पहला उदाहरण v2 के लिए v3 wrapper के साथ काम नहीं करता है (मानचित्र नियंत्रण समर्थित नहीं हैं)। उन पुस्तकालयों को v3 पर बंद करना बहुत कठिन नहीं होगा, केवल कुछ ऐसे स्थान हैं जो v2 विशिष्ट वाक्यविन्यास का उपयोग करते हैं, और v3 में उन्हें बदलने के लिए सरल है। – geocodezip

2

मुझे नहीं लगता कि आप सभी बिंदुओं की जांच से बच सकते हैं। क्या होगा अगर चेक किए गए बिंदु निकटतम नहीं है?

यदि आपको यह ऑपरेशन कई बार करना है, तो आप ऐसी डेटा संरचना चुन सकते हैं जो इस तरह की खोज के लिए अनुकूल है, उदाहरण के लिए क्वाड्री। ध्यान दें कि आपको डेसकार्ट्स निर्देशांक के रूप में लैट एलएनजी का उपयोग नहीं करना चाहिए।

भी देखें Finding nearest point in an efficient way अक्षां एलएनजी के लिए 2 डी विमान के लिए है, और नहीं है, लेकिन आप अनुमान लगा सकता है: https://stackoverflow.com/a/16271669/59019

+0

इस उत्तर पॉलीलाइन के निकटतम नोड के लिए कि केवल अक्षां Lons दिया जाता है। यदि आप पॉलीलाइन की नजदीक रेखा के निकटतम पिक्सेल में रूचि रखते हैं, तो यह एक और कहानी है। – jmihalicza

1

jmihalicza के जवाब से प्रेरित होकर, मैं किसी दिए गए LatLng को LatLngs की एक सरणी में निकटतम बिंदु खोजने के लिए इस समारोह के साथ आया था।

फ़ंक्शन निकटतम लैट्लिंग (llng) और लैट्लिंग्स (सूचीडेटा) की एक सरणी लेता है और सरणी में प्रत्येक latlng और दिए गए latlng के बीच की दूरी पाता है, तो उसे कम से कम दूरी मिलती है और प्रदान की गई सूची से Latlng लौटाता है वह दूरी

function closest(llng, listData) { 
    var arr  = listData; 
    var pnt  = llng; 
    var distArr = []; 
    var dist = google.maps.geometry.spherical.computeDistanceBetween; 


    for (index in arr) 
     distArr.push([arr[index], dist(pnt, arr[index])]); 

    return distArr.sort(function(a,b){ 
     return a[1]-b[1]; 
    })[0][0]; 
} 

संपादित

आप LatLngs जो पॉलीलाइन बनाने की सरणी के लिए पहुँच नहीं है, लेकिन पॉलीलाइन खुद के लिए उपयोग किया है, तो आप पॉलीलाइन के getPath method उपयोग कर सकते हैं पथ जो एक है पाने के लिए एमवीसी सरणी ताकि आप उपरोक्त फ़ंक्शन (निकटतम) के साथ उपयोग करने के लिए LatLngs की एक सरणी वापस करने के लिए .getArray() का उपयोग कर सकें।

+1

यह केवल पॉलीलाइन के कशेरुक को वापस करेगा जो बिंदु के सबसे नज़दीक है, पॉलीलाइन पर निकटतम बिंदु नहीं। – ParoX

+0

कोई विचार यह कैसे ठीक करें? –

+0

पॉलीलाइन पर शीर्षकों की प्रत्येक जोड़ी के बीच अधिक कोष्ठक इंटरपोलेट करें, भले ही नए शिखर कोई स्थानिक जानकारी नहीं जोड़ पाएंगे। जो भी संकल्प आपको संतोषजनक लगता है उसे करें। –

8

मैं एक क्लीनर संस्करण है कि वी 3 पर स्थापित किया गया की जरूरत है, इसलिए यहाँ यह है:

/** 
* Snap marker to closest point on a line. 
* 
* Based on Distance to line example by 
* Marcelo, maps.forum.nu - http://maps.forum.nu/gm_mouse_dist_to_line.html 
* Then 
* @ work of Björn Brala - Swis BV who wrapped the algorithm in a class operating on GMap Objects 
* And now 
* Bill Chadwick, who factored the basic algorithm out of the class (removing much intermediate storage of results) 
*  and added distance along line to nearest point calculation 
* Followed by 
* Robert Crowe, who ported it to v3 of the Google Maps API and factored out the marker to make it more general. 
* 
* Usage: 
* 
* Create the class 
*  var oSnap = new cSnapToRoute(); 
* 
* Initialize the subjects 
*  oSnap.init(oMap, oPolyline); 
* 
**/ 

function cSnapToRoute() { 

    this.routePoints = Array(); 
    this.routePixels = Array(); 
    this._oMap; 
    this._oPolyline; 

    /** 
    * @desc Initialize the objects. 
    * @param Map object 
    * @param GPolyline object - the 'route' 
    **/ 
    this.init = function (oMap, oPolyline) { 
     this._oMap = oMap; 
     this._oPolyline = oPolyline; 

     this.loadRouteData(); // Load needed data for point calculations 
    } 

    /** 
    * @desc internal use only, Load route points into RoutePixel array for calculations, do this whenever zoom changes 
    **/ 
    this.loadRouteData = function() { 
     this.routePixels = new Array(); 
     var proj = this._oMap.getProjection(); 
     for (var i = 0; i < this._oPolyline.getPath().getLength(); i++) { 
      var Px = proj.fromLatLngToPoint(this._oPolyline.getPath().getAt(i)); 
      this.routePixels.push(Px); 
     } 
    } 

    /** 
    * @desc Get closest point on route to test point 
    * @param GLatLng() the test point 
    * @return new GLatLng(); 
    **/ 
    this.getClosestLatLng = function (latlng) { 
     var r = this.distanceToLines(latlng); 
     var proj = this._oMap.getProjection(); 
     return proj.fromPointToLatLng(new google.maps.Point(r.x, r.y)); 
    } 

    /** 
    * @desc Get distance along route in meters of closest point on route to test point 
    * @param GLatLng() the test point 
    * @return distance in meters; 
    **/ 
    this.getDistAlongRoute = function (latlng) { 
     var r = this.distanceToLines(latlng); 
     return this.getDistToLine(r.i, r.fTo); 
    } 

    /** 
    * @desc internal use only, gets test point xy and then calls fundamental algorithm 
    **/ 
    this.distanceToLines = function (thisLatLng) { 
     var tm = this._oMap; 
     var proj = this._oMap.getProjection(); 
     var thisPx = proj.fromLatLngToPoint(thisLatLng); 
     var routePixels = this.routePixels; 
     return getClosestPointOnLines(thisPx, routePixels); 
    } 

    /** 
    * @desc internal use only, find distance along route to point nearest test point 
    **/ 
    this.getDistToLine = function (iLine, fTo) { 

     var routeOverlay = this._oPolyline; 
     var d = 0; 
     for (var n = 1 ; n < iLine ; n++) { 
      d += routeOverlay.getPath().getAt(n - 1).distanceFrom(routeOverlay.getPath().getAt(n)); 
     } 
     d += routeOverlay.getPath().getAt(iLine - 1).distanceFrom(routeOverlay.getPath().getAt(iLine)) * fTo; 

     return d; 
    } 


} 

/* desc Static function. Find point on lines nearest test point 
    test point pXy with properties .x and .y 
    lines defined by array aXys with nodes having properties .x and .y 
    return is object with .x and .y properties and property i indicating nearest segment in aXys 
    and property fFrom the fractional distance of the returned point from aXy[i-1] 
    and property fTo the fractional distance of the returned point from aXy[i] */ 


function getClosestPointOnLines(pXy, aXys) { 

    var minDist; 
    var fTo; 
    var fFrom; 
    var x; 
    var y; 
    var i; 
    var dist; 

    if (aXys.length > 1) { 

     for (var n = 1 ; n < aXys.length ; n++) { 

      if (aXys[n].x != aXys[n - 1].x) { 
       var a = (aXys[n].y - aXys[n - 1].y)/(aXys[n].x - aXys[n - 1].x); 
       var b = aXys[n].y - a * aXys[n].x; 
       dist = Math.abs(a * pXy.x + b - pXy.y)/Math.sqrt(a * a + 1); 
      } 
      else 
       dist = Math.abs(pXy.x - aXys[n].x) 

      // length^2 of line segment 
      var rl2 = Math.pow(aXys[n].y - aXys[n - 1].y, 2) + Math.pow(aXys[n].x - aXys[n - 1].x, 2); 

      // distance^2 of pt to end line segment 
      var ln2 = Math.pow(aXys[n].y - pXy.y, 2) + Math.pow(aXys[n].x - pXy.x, 2); 

      // distance^2 of pt to begin line segment 
      var lnm12 = Math.pow(aXys[n - 1].y - pXy.y, 2) + Math.pow(aXys[n - 1].x - pXy.x, 2); 

      // minimum distance^2 of pt to infinite line 
      var dist2 = Math.pow(dist, 2); 

      // calculated length^2 of line segment 
      var calcrl2 = ln2 - dist2 + lnm12 - dist2; 

      // redefine minimum distance to line segment (not infinite line) if necessary 
      if (calcrl2 > rl2) 
       dist = Math.sqrt(Math.min(ln2, lnm12)); 

      if ((minDist == null) || (minDist > dist)) { 
       if (calcrl2 > rl2) { 
        if (lnm12 < ln2) { 
         fTo = 0;//nearer to previous point 
         fFrom = 1; 
        } 
        else { 
         fFrom = 0;//nearer to current point 
         fTo = 1; 
        } 
       } 
       else { 
        // perpendicular from point intersects line segment 
        fTo = ((Math.sqrt(lnm12 - dist2))/Math.sqrt(rl2)); 
        fFrom = ((Math.sqrt(ln2 - dist2))/Math.sqrt(rl2)); 
       } 
       minDist = dist; 
       i = n; 
      } 
     } 

     var dx = aXys[i - 1].x - aXys[i].x; 
     var dy = aXys[i - 1].y - aXys[i].y; 

     x = aXys[i - 1].x - (dx * fTo); 
     y = aXys[i - 1].y - (dy * fTo); 

    } 

    return { 'x': x, 'y': y, 'i': i, 'fTo': fTo, 'fFrom': fFrom }; 
} 
+1

कोड v3 के लिए पूरी तरह से काम करता है। यदि परिणाम के लिए थोड़ा मुश्किल है तो दूरी टोललाइन विधि विशेष रूप से दिलचस्प है। आपको इसे एक पुस्तकालय बनाना चाहिए। मैंने इसे पहले से ही अपने यूआरएल के संदर्भ में अपने कोडबेस में शामिल कर लिया है। धन्यवाद! – brendan

+0

धन्यवाद! यह बहुत अच्छा काम किया। मैं gmaps का उपयोग नहीं कर रहा हूं, इसलिए मैंने अभी getClosestPointOnLines फ़ंक्शन का उपयोग किया है। –

संबंधित मुद्दे