1

I'm developing HTML5 web-app for iPad that uses Google Maps API. This API is new for me, I have searched answers for many questions related to it. Markers, info windows, routes, geolocation, street view. The most useful information about that all was found in official documentation:

https://developers.google.com/maps/documentation/javascript/3.exp/reference?hl=ru

However, I still cannot find a solution to one (maybe, simple) problem. For example, I've created a new map instance and one marker (destination), started watching for my position (GEO Location) and drew a route to the location of this marker. How can I check when I will reach the destination? I've looked for appropriate events of DirectionsService, DirectionsRenderer and etc. but with no results. Also, I found the same topic on this website, but it is about Android development, not JS (Android: How to check that whether i reach/near my Destination?).

Being hopeless, I invented my own solution, but it seems not to work properly (I have not fully tested it yet, I'll do it in the next few days). I thought if I would compare lat&lng of my current position with lat&lng of destination - then I could catch the moment when destination is reached:

if (myPosition.lat() == dest.marker.position.lat() && myPosition.lng() == dest.marker.position.lng()) {
     alert('You have arrived!');
}

This code executes in successful watch position callback of navigator.geolocation.watchPosition

Do you have any ideas?

Community
  • 1
  • 1

3 Answers3

4

LatLng's are values with a high accuracy, you can't expect that the positions of boths markers will match exactly.

Add a tolerance:

//tolerance 50 meters
//requires the geometry-library 
if(google.maps.geometry.spherical
   .computeDistanceBetween(myPosition,dest.marker.position)<50){
  alert('You have arrived!');
} 
Dr.Molle
  • 116,463
  • 16
  • 195
  • 201
0

I don't think it's a good idea to make a home made solution, since maps API can do quit everything.

Using Google Maps API to get travel time data

Community
  • 1
  • 1
gr3g
  • 2,866
  • 5
  • 28
  • 52
0

For Android Studio users:

Get object of currentLocation and destinationLocation

Location currentLocation;      // use fusedLocationProviderService
Location destinationLocation = new Location("");  // set Lat and Long 
destinationLocation.setLatitude(122.1234123); // like this

 if (location.distanceTo(new Location("")) < 30) { // meters
                    Toast.makeText(MainActivity.this, "Toast you have reached",
                            Toast.LENGTH_SHORT).show();
                }

Vijay
  • 1,163
  • 8
  • 22