From 0e60d1e6087985f6cb25cbaadb786f20f05d8938 Mon Sep 17 00:00:00 2001
From: Jacek Kowalski <Jacek@jacekk.info>
Date: Sun, 23 Apr 2017 23:36:05 +0000
Subject: [PATCH] Match inacurrate clicks to nearest feature

---
 map.js |   40 ++++++++++++++++++++++++++++++++++++++++
 1 files changed, 40 insertions(+), 0 deletions(-)

diff --git a/map.js b/map.js
index 7547af4..b1ed525 100644
--- a/map.js
+++ b/map.js
@@ -19,6 +19,7 @@
 var feature_timer = null;
 
 var map = null;
+var map_sphere = null;
 var popup_element = document.getElementById('popup');
 var fail_element = document.getElementById('fail');
 
@@ -430,6 +431,26 @@
 	featureClicked(feature);
 }
 
+function getDistance(c1, c2) {
+	if(c1.getGeometry) {
+		c1 = c1.getGeometry().getCoordinates();
+	}
+	if(c2.getGeometry) {
+		c2 = c2.getGeometry().getCoordinates();
+	}
+	
+	var c1 = ol.proj.transform(c1, 'EPSG:3857', 'EPSG:4326');
+	var c2 = ol.proj.transform(c2, 'EPSG:3857', 'EPSG:4326');
+	return map_sphere.haversineDistance(c1, c2);
+}
+
+function returnClosest(point, f1, f2) {
+	if(!f1) return f2;
+	if(!f2) return f1;
+	
+	return (getDistance(point, f1) < getDistance(point, f2)) ? f1 : f2;
+}
+
 function init() {
 	if(!window.jQuery) {
 		fail(lang.jquery_not_loaded);
@@ -490,10 +511,29 @@
 			})
 		]),
 	});
+	map_sphere = new ol.Sphere(6378137);
 	
 	// Display popup on click
 	map.on('singleclick', function(e) {
+		var point = e.coordinate;
 		var feature = map.forEachFeatureAtPixel(e.pixel, function(feature) { return feature; });
+		
+		if(!feature) {
+			if(stops_layer.getVisible()) {
+				feature = returnClosest(point, feature, stops_source.getClosestFeatureToCoordinate(point));
+			}
+			if(stop_points_layer.getVisible()) {
+				feature = returnClosest(point, feature, stop_points_source.getClosestFeatureToCoordinate(point));
+			}
+			if(vehicles_layer.getVisible()) {
+				feature = returnClosest(point, feature, vehicles_source.getClosestFeatureToCoordinate(point));
+			}
+			
+			if(getDistance(point, feature) > 200) {
+				feature = null;
+			}
+		}
+		
 		featureClicked(feature);
 	});
 

--
Gitblit v1.9.1