Commit 7153518aeef3e682f4f0f2f38fc013b45c2edc6f
- Diff rendering mode:
- inline
- side by side
prototypes.js
(16 / 5)
|   | |||
| 2133 | 2133 | return null; | |
| 2134 | 2134 | var smallestDistance = null; | |
| 2135 | 2135 | var smallestDistancePoint = null; | |
| 2136 | var index = 0; | ||
| 2136 | var index = 0; // Index is used to find out the position of the point in the ordered list of points | ||
| 2137 | 2137 | var maxDistance = this.HOVER_MAX_DISTANCE * this.map.getResolution(); | |
| 2138 | 2138 | for(var j=0; j<this.features.length; j++) | |
| 2139 | 2139 | { | |
| … | … | ||
| 2141 | 2141 | continue; | |
| 2142 | 2142 | ||
| 2143 | 2143 | var points = this.features[j].geometry.components; | |
| 2144 | for(var i=0; i<points.length; i++,index++) | ||
| 2144 | var p1,p2,d,u,px; | ||
| 2145 | for(var i=0; i<points.length-1; i++,index++) | ||
| 2145 | 2146 | { | |
| 2146 | var distanceX = Math.abs(points[i].x-lonlat.lon); | ||
| 2147 | var distanceY = Math.abs(points[i].y-lonlat.lat); | ||
| 2147 | p1 = points[i]; | ||
| 2148 | p2 = points[i+1]; | ||
| 2149 | d = { x : p2.x-p1.x, y : p2.y-p1.y }; | ||
| 2150 | u = ((lonlat.lon-p1.x)*d.x + (lonlat.lat-p1.y)*d.y) / (d.x*d.x + d.y*d.y); // See http://local.wasp.uwa.edu.au/~pbourke/geometry/pointline/ | ||
| 2151 | |||
| 2152 | if(u < 0 || u > 1) | ||
| 2153 | continue; | ||
| 2154 | |||
| 2155 | px = { x : p1.x+u*d.x, y : p1.y+u*d.y }; | ||
| 2156 | |||
| 2157 | var distanceX = Math.abs(px.x-lonlat.lon); | ||
| 2158 | var distanceY = Math.abs(px.y-lonlat.lat); | ||
| 2148 | 2159 | if(distanceX > maxDistance || distanceY > maxDistance) | |
| 2149 | 2160 | continue; | |
| 2150 | 2161 | var distance = Math.sqrt(Math.pow(distanceX, 2) + Math.pow(distanceY, 2)); | |
| … | … | ||
| 2163 | 2163 | continue; | |
| 2164 | 2164 | if(smallestDistance == null || distance < smallestDistance) | |
| 2165 | 2165 | { | |
| 2166 | smallestDistancePoint = [ index, points[i] ]; | ||
| 2166 | smallestDistancePoint = [ index+u, px ]; | ||
| 2167 | 2167 | smallestDistance = distance; | |
| 2168 | 2168 | } | |
| 2169 | 2169 | } |

