javascript - Find the closest marker to a new marker added Customer Service app -


i working on fast food customer service application call center should able add address of caller (a marker on map) clicking on map has old markers (the branches) , automatically should calculate closest branch marker added.

i add markers array , used this calculation function, nevertheless need know how call function after clicking event , connect new marker added. code follow

identifying branches

var map; var markers = []; var marker, i; var locations = [  ['title a', 3.180967,101.715546, 1],  ['title b', 3.200848,101.616669, 2],  ['title c', 3.147372,101.597443, 3],  ['title d', 3.19125,101.710052, 4]]; 

the initialization function

function initialize() {    var haightashbury = new google.maps.latlng(3.171368, 101.653404);    var mapoptions = {       zoom: 12,       center: haightashbury,       maptypeid: google.maps.maptypeid.terrain    };     map = new google.maps.map(document.getelementbyid('map-canvas'),       mapoptions);     google.maps.event.addlistener(map, 'click', function(event) {       addmarker(event.latlng);    });     (i = 0; < locations.length; i++) {         marker = new google.maps.marker({          position: new google.maps.latlng(locations[i][1], locations[i][2]),          map: map       });        google.maps.event.addlistener(marker, 'click', (function(marker, i) {          return function() {             infowindow.setcontent(locations[i][0]);             infowindow.open(map, marker);          }       })(marker, i));        google.maps.event.addlistener(map, 'click', find_closest_marker);     } } 

adding new marker

function addmarker(location) {    var marker1 = new google.maps.marker({       position: location,       map: map    });     markers.push(marker);  } 

adding new marker array

setallmap(map)  {    (var = 0; < markers.length; i++)    {       markers[i].setmap(map);    } } 

calculating function

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 = 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); } 

thank you.

use google maps geometry library calculate distances. both parameters must latlng objects.

google.maps.geometry.spherical.computedistancebetween(markerposition, mainposition); 

ofcourse need load geometry library

&libraries=geometry 

working example of code in jsfiddle. closest marker printed in console (f12). note comments in code, mistakes fixed.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -