Use the GoogleMap API or MapBox Direction API to implement my own navigation in my application.

I would like to implement a navigation map for drivers in my Android application. I don’t want to use the URL scheme to open the Google Maps application to navigate it. I prefer to implement this navigation function inside my application, like what Google Map does.

My requirements are pretty simple. Go from PlaceA to PlaceB.

After I read the documents from google and MapBox. There are api instructions for the user to get all the information, such as routes.

1 GoogleMap API: https://developers.google.com/maps/documentation/directions/intro?hl=en

2 MapBox API: https://www.mapbox.com/developers/api/directions/

My question is below: 1 How do I know when the right time to call the user, he must turn right / left? 2 How do I know how far from my current location to the next maneuver / steps? Do I need to recalculate the distance after my location, since my location is always changing? 3 How do I know if a user has committed a wrong action? For example, it should turn right, but the user has turned left. I need to ask your server to recalculate the route.

Any suggestion? Thanks in advance.

+4
source share
2 answers

, API- Android Mapbox.

1. , , , /?

-, , .setSteps(true) MapboxDirections.Builder(). . , , , . , , , . , (, 0,0189394 , ~ 100 ), . , , :

private double computeDistance(Waypoint from, Waypoint to) {
    double dLat = Math.toRadians(to.getLatitude() - from.getLatitude());
    double dLon = Math.toRadians(to.getLongitude() - from.getLongitude());
    double lat1 = Math.toRadians(from.getLatitude());
    double lat2 = Math.toRadians(to.getLatitude());

    double a = Math.pow(Math.sin(dLat/2), 2) + Math.pow(Math.sin(dLon/2), 2) * Math.cos(lat1) * Math.cos(lat2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

    double R = 3960;

    double distance = R * c;
    return distance;
}

, ( ) ( 0,0189394 ), , .

2. , /? , ?

1. , mapView.setOnMyLocationChangeListener MapView.OnMyLocationChangeListener(). , 1.

3. , ? , , . .

, isOffRoute(), userLocation , , ( 0,2 ). , , , if, :

if (route.isOffRoute(userLocation)) {
    Toast.makeText(this, "You are off-route.", Toast.LENGTH_SHORT).show();
}

, , . , , 1.1, . ​​.

, , .

+7

Source: https://habr.com/ru/post/1623328/


All Articles