2

I am working on an app for a fast food customer service using Google maps API. The customer service worker should be able to add a marker on the map (containing the caller address) and automatically the map should show him which is the closest branch to the caller (The new marker just got added by the worker).

I could show the branches as markers from an array and I could add the new marker to the map, nevertheless did not find anywhere possible to calculate the distance between the old markers and the new added marker.

Except that I found this that calculate from the current location and not from an added marker which is still far from what I need.

Anyone could give me specific code for how to do it?

Community
  • 1
  • 1
Hashim Adel
  • 715
  • 3
  • 8
  • 14

1 Answers1

1

Probably you have map 'click' event and you add new marker inside that event. And you have a solution that shared before in this link. What you need is to call find_closest_marker() after you add your marker and call the function with the new added marker's position property:

marker.setMap(map);
find_closest_marker(marker.position);

After that, just you need is to change event in find_closest_marker() function with something like

var lat = event.lat();.

Community
  • 1
  • 1
alpakyol
  • 2,402
  • 2
  • 29
  • 35
  • This looks helpful, thank you, will try it and if anyone has any other ideas feel free to share. – Hashim Adel Mar 05 '14 at 08:39
  • Can you be more specific starting from calling the function with the new added marker's postion? or give an example how to do it? – Hashim Adel Mar 06 '14 at 07:27
  • @HashimAdel Why do you need it? You have already said that I can show markers from an array which means you can create and add markers to both map and array. Moreover, it is difficult to reference your new added marker as I don't know your code. And in my shared code `marker` in `marker.setMao(map)` is your new added marker object. – alpakyol Mar 06 '14 at 07:42