Title: Nearest Marker
Last modified: August 20, 2016

---

# Nearest Marker

 *  Resolved [b a](https://wordpress.org/support/users/mpa4hu/)
 * (@mpa4hu)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/nearest-marker/)
 * My last part and then I dissapear 😀
    ( Of course in the end I’ll show you the
   project and give you georgian translation 🙂 )
 * Here is my code ( I know it’s a little bit terrible but still, it works)
 *     ```
       /*
       * Center maps on user location when available.
       */
       // An Example Google V3 customization
   
       GeoMashup.addAction( 'loadedMap', function( properties, mxn ) {
       	var pointerz;
       	var geomarker;
       	var start, end;
       	var directionsDisplay;
       	var siberia = new google.maps.LatLng(60, 105);
       	var newyork = new google.maps.LatLng(40.69847032728747, -73.9514422416687);
       	var browserSupportFlag =  new Boolean();
   
       	if ( 'global' != properties.map_content ) {
       		// Don'ts center non-global maps on user
       		return;
       	}
       	var google_map = mxn.getMap();
   
       	var directionsService = new google.maps.DirectionsService();
       	directionsDisplay = new google.maps.DirectionsRenderer();
       	directionsDisplay.setMap(google_map);
   
       	var term45 = window.parent.document.getElementById("term-45").style.display="none";
       	var paybox = window.parent.document.getElementById("paybox");
       	var maptype = window.parent.document.getElementById("maptype");
   
       	function calcRoute(start, end) {
       	  var request = {
       	    origin:start,
       	    destination:end,
       	    travelMode: google.maps.TravelMode.DRIVING
       	  };
       	  directionsService.route(request, function(result, status) {
       	    if (status == google.maps.DirectionsStatus.OK) {
       	      directionsDisplay.setDirections(result);
       	    }
       	  });
       	}
   
       	var payboxcount = 1;
           paybox.addEventListener("click", function(){
           	payboxcount++;
   
       		if ( payboxcount % 2 === 0 ) {
       			// uncheck other marker categories
       			var check = window.parent.document.getElementById("gm-category-check-all");
       			//double click check_all if any selected but not all
       			if ( $( check ).is( ':checked' ) ) {
       				$(check).click();
       			} else {
       				$(check).click();
       				$(check).click();
       			}
       			$(paybox).addClass('btn-new-maps-selected');
       			// Try W3C Geolocation (Preferred)
       			if(navigator.geolocation) {
       				browserSupportFlag = true;
       				navigator.geolocation.getCurrentPosition(function(position) {
   
       				var pointerz = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
   
       				geomarker = new google.maps.Marker({
       					    position: pointerz,
       					    map: google_map,
       					    title:"პირვეტ ს მარსა!"
       					});
       					start = pointerz;
       					end = "Samgori";
       					GeoMashup.showCategory(45);
       					// To add the marker to the map, call setMap();
       					geomarker.setMap(google_map);
       					geomarker.setAnimation(google.maps.Animation.BOUNCE);
   
       					calcRoute(start, end);
   
       				}, function() {
       					handleNoGeolocation(browserSupportFlag);
       				});
       			// Try Google Gears Geolocation
       			} else if (google.gears) {
       				browserSupportFlag = true;
       				var geo = google.gears.factory.create('beta.geolocation');
       				geo.getCurrentPosition(function(position) {
       					 var pointerz = new google.maps.LatLng(-25.363882, 131.044922);
   
       				geomarker = new google.maps.Marker({
       					    position: pointerz,
       					    map: google_map,
       					    title:"Hello World!"
       					});
   
       					GeoMashup.showCategory(45);
       					// To add the marker to the map, call setMap();
       					geomarker.setMap(google_map);
       					geomarker.setAnimation(google.maps.Animation.BOUNCE);
       					google_map.setZoom(16)
       					google_map.setCenter( pointerz );
       				}, function() {
       					handleNoGeoLocation(browserSupportFlag);
       				});
       			// Browser doesn't support Geolocation
       			} else {
       				browserSupportFlag = false;
       				handleNoGeolocation(browserSupportFlag);
       			}
   
       			function handleNoGeolocation(errorFlag) {
       				if (errorFlag == true) {
       					initialLocation = newyork;
       				} else {
       					initialLocation = siberia;
       				}
       				map.setCenter(initialLocation);
       			}
       		} else {
       		// DESTROY EVERYTHING I CREATED
       			GeoMashup.hideCategory(45);
       			geomarker.setMap(null);
       			$(paybox).removeClass('btn-new-maps-selected');
   
       		}
   
       	},false);
   
       	var maptypecount = 1;
       	maptype.addEventListener("click", function(){
       		maptypecount++;
           	$(maptype).toggleClass('btn-new-maps-selected');
   
       		if ( maptypecount % 2 === 0 ) {
       			mxn.setMapType( 2 );
       		} else {
       			mxn.setMapType( 1 );
       		}
   
       	},false);
       } );
       ```
   
 * So the only thing I left is to add calculation from var pointerz to some_nearest_marker_from_this_category
 * I found this:
 *     ```
       function rad(x) {return x*Math.PI/180;}
       function find_closest_marker( event ) {
           var lat = event.latLng.lat();
           var lng = event.latLng.lng();
           var R = 6371; // radius of earth in km
           var distances = [];
           var closest = -1;
           for( i=0;i<map.markers.length; i++ ) {
               var mlat = map.markers[i].position.lat();
               var mlng = map.markers[i].position.lng();
               var dLat  = rad(mlat - lat);
               var dLong = rad(mlng - lng);
               var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                   Math.cos(rad(lat)) * Math.cos(rad(lat)) * Math.sin(dLong/2) * Math.sin(dLong/2);
               var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
               var d = R * c;
               distances[i] = d;
               if ( closest == -1 || d < distances[closest] ) {
                   closest = i;
               }
           }
   
           alert(map.markers[closest].title);
       }
       ```
   
 * there is `map.markers.length`, when I try `mxn.markers.length` it works but now
   I can’t get it’s longitude and latitude for this function, like mxn.markers[1].
   getPosition or ANYTHING ))
 * [http://wordpress.org/extend/plugins/geo-mashup/](http://wordpress.org/extend/plugins/geo-mashup/)

Viewing 7 replies - 1 through 7 (of 7 total)

 *  Plugin Author [Dylan Kuhn](https://wordpress.org/support/users/cyberhobo/)
 * (@cyberhobo)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/nearest-marker/#post-3600558)
 * The [Mapstraction documentation](http://mapstraction.com/mxn/build/latest/docs/symbols/mxn.Mapstraction.html)
   is indeed missing object properties. What you’re looking for is `mxn.markers[
   1].location.lat` and `mxn.markers[1].location.lon`.
 *  Thread Starter [b a](https://wordpress.org/support/users/mpa4hu/)
 * (@mpa4hu)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/nearest-marker/#post-3600666)
 * Oh yes, that worked perfect, but I still cant filter some category for markers.
 * I have hidden all the otehr categories and showing only one with `GeoMashup.showCategory();`.
   can I filter by visable markers or do I need to save other markers somewhere,
   delete all and then showsomecategoy?
 *  Thread Starter [b a](https://wordpress.org/support/users/mpa4hu/)
 * (@mpa4hu)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/nearest-marker/#post-3600694)
 * I dump every object and found interesting ones
    here is a code I used if somebody
   will need it for something similar
 *     ```
       for( i=0;i<mxn.markers.length; i++ ) {
       	var term = properties.object_data.objects[i].terms;
       	for(var key in term) {
       		termvalue = term[key];
       	}
       	if (termvalue == 45) {
       		var mlat = mxn.markers[i].location.lat;
       		var mlng = mxn.markers[i].location.lng;
       		var dLat  = rad(mlat - vlat);
       		var dLong = rad(mlng - vlng);
       		var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
       									Math.cos(rad(vlat)) * Math.cos(rad(vlat)) * Math.sin(dLong/2) * Math.sin(dLong/2);
       		var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
       		var d = R * c;
       		distances[i] = d;
       		if ( closest == -1 || d < distances[closest] ) {
       			closest = i;
       		}
       	}
       }
       var endpoint = new google.maps.LatLng(mxn.markers[closest].location.lat, mxn.markers[closest].location.lng);
       calcRoute(pointerz, endpoint);
       ```
   
 *  Plugin Author [Dylan Kuhn](https://wordpress.org/support/users/cyberhobo/)
 * (@cyberhobo)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/nearest-marker/#post-3600697)
 * Glad you’re having fun. Try out some browser developer tools if you haven’t yet.
 * [http://www.codeschool.com/courses/discover-devtools](http://www.codeschool.com/courses/discover-devtools)
 * It’s always better to use documented properties and methods when you can.
 * [http://code.cyberhobo.net/jsdoc/geo-mashup-1.5/symbols/GeoMashup.html#.getMarkerObjects](http://code.cyberhobo.net/jsdoc/geo-mashup-1.5/symbols/GeoMashup.html#.getMarkerObjects)
 *  Thread Starter [b a](https://wordpress.org/support/users/mpa4hu/)
 * (@mpa4hu)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/nearest-marker/#post-3600714)
 * hehe interesting one. I also tried codeacademy if you know, interesting as well.
 * I’ll try this from apirl.
 * GeoMashup.getMarkerObjects(marker) will it return objects nearby? I thought it
   is used to get objects at particular location, like if at 45,45 is no object 
   but 45,46 is, it returns undefined.
 *  Plugin Author [Dylan Kuhn](https://wordpress.org/support/users/cyberhobo/)
 * (@cyberhobo)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/nearest-marker/#post-3600716)
 * It will only give you objects represented by a marker, but I think that will 
   be more reliable than using `properties.object_data.objects` as you are, which
   isn’t guaranteed to be in the same order as the markers array.
 *  Thread Starter [b a](https://wordpress.org/support/users/mpa4hu/)
 * (@mpa4hu)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/nearest-marker/#post-3600719)
 * Thank you Dylan for your attention,
    I changed `var term = properties.object_data.
   objects[i].terms;`with `var term = GeoMashup.getMarkerObjects(mxn.markers[i])[
   0].terms;`

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Nearest Marker’ is closed to new replies.

 * ![](https://ps.w.org/geo-mashup/assets/icon.svg?rev=1631806)
 * [Geo Mashup](https://wordpress.org/plugins/geo-mashup/)
 * [Support Threads](https://wordpress.org/support/plugin/geo-mashup/)
 * [Active Topics](https://wordpress.org/support/plugin/geo-mashup/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/geo-mashup/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/geo-mashup/reviews/)

 * 7 replies
 * 2 participants
 * Last reply from: [b a](https://wordpress.org/support/users/mpa4hu/)
 * Last activity: [13 years, 2 months ago](https://wordpress.org/support/topic/nearest-marker/#post-3600719)
 * Status: resolved