Start Android navigation for result - android

I am creating an app where I need to start the navigation app and then use the result (most importantly the driven distance). I am starting the navigation activity with startActivityForResult and using the scheme "google.navigation". Like this:
Intent i = new Intent(
Intent.ACTION_VIEW,
Uri.parse("google.navigation:q=Somewhere"));
startActivityForResult(i,1);
This works great, I get a callback when the activity is done, but the data portion of the activity is empty.
Is there a way to do this?
Is there a navigation history where I can browse the latest trip for data?
Best Regards
Jakob Simon-Gaarde

Is there a way to do this?
No, there is no way for you to force an activity to give you a result. The determination of whether an activity supports a result lies in the authors of the activity, not the caller of the activity.
Is there a navigation history where I can browse the latest trip for data?
There is no documented and supported API for the Google Navigation Android app (which includes the google.navigation scheme).

I don't know about callbacks from Google Navigation. But if you are only interested in getting the driven distance, you could keep listening to the location updates in the background and calculate the distance yourself. The following function uses android.location.Location to calculate distance between two LatLang coordinates:
public double getGeodesicDistance(LatLng from, LatLng to){
Location _fromLoc = new Location("From location");
_fromLoc.setLatitude(from.latitude);
_fromLoc.setLongitude(from.longitude);
Location _toLoc = new Location("To location");
_toLoc.setLatitude(to.latitude);
_toLoc.setLongitude(to.longitude);
return Math.round(_fromLoc.distanceTo(_toLoc)); // distance in metres
}
But if you decide to take this approach, you should detect the sudden jumps in GPS data and handle them accordingly.

You could get the location right before you start the navigation, and get it again once the navigation is closed, and calculate the difference.

Related

How to detect I'm near a waypoint

I want to make a navigation application on Android (Kotlin) using the mapbox-android SDK. I need to create a route with specific waypoints and want to know when I am near one of those waypoints (200m before for example). I already managed to display a map with my waypoints with the NavigationRoute object and launch a turn-by-turn navigation using the NavigationLauncher object. Still, I don't have a clue how to know when I'm getting near a waypoint. I've read things about milestones event listener (step or route), RouteProgress (leg or route), but I don't know if it's the right approach to do this.
I tried to declare a milestone with a STEP_DISTANCE_REMAINING_METERS and add a MilestoneEventListener but nothing fire up in my logs. Like I said, I don't even know if it's the right way to do this.
val milestone = RouteMilestone.Builder()
.setIdentifier(100)
.setTrigger(Trigger.all(
Trigger.lt(TriggerProperty.STEP_DISTANCE_REMAINING_METERS, 200)
))
.build()
navigation = MapboxNavigation(this#MainActivity, Mapbox.getAccessToken())
navigation.addMilestone(milestone)
navigation.addMilestoneEventListener { _, _, milestone ->
Log.d(TAG, "milestone triggered: " + milestone.instruction)
}
navigationMapRoute = NavigationMapRoute(navigation, mapView, map, R.style.NavigationMapRoute)
navigationMapRoute!!.addRoute(currentRoute)
I'm using those versions of mapbox SDK
mapbox-android-sdk:5.3.2
mapbox-android-navigation:0.9.0
mapbox-android-navigation-ui:0.9.0
If you know the location of your waypoints you could just use Location Manager's Proximity Alert

Get current location as fast as possible with Google Play Services?

I am developing an android application in which I need to get my current Location. I have successfully wrote the code and I am getting my current location using Google Play Service.
The problem is sometimes it gives me the location after a long time. I have noticed that it was only for first use of the app.
Any way to avoid this problem and get the current location fast? Is it related to the version of google play service in my code? (I am not using the last one in fact I am using version 9.8.0.)
As #tahsinRupam said, avoid using getLastLocation as it has a high tendency to return null. It also does not request a new location, so even if you get a location, it could be very old, and not reflect the current location. You might want to check the sample code in this thread: get the current location fast and once in android.
public void foo(Context context) {
// when you need location
// if inside activity context = this;
SingleShotLocationProvider.requestSingleUpdate(context,
new SingleShotLocationProvider.LocationCallback() {
#Override public void onNewLocationAvailable(GPSCoordinates location) {
Log.d("Location", "my location is " + location.toString());
}
});
}
You might want to verify the lat/long are actual values and not 0 or something. If I remember correctly this shouldn't throw an NPE but you might want to verify that.
Here's another SO post which might help:
What is the simplest and most robust way to get the user's current location on Android?

GPS location detect slow or unable receive [Here SDK]

when trying start navigation in some indoor environment, it will unable receive the GPS signal or detect with very slow, but when using other navigation app(Waze/Google Map/Here) the problem does not occur.
And also some indoor place can receive the gps signal without any problem.
below is my code for PositionListener
naviManager.addPositionListener(new WeakReference<NavigationManager.PositionListener>(positionListener));
private NavigationManager.PositionListener positionListener = new NavigationManager.PositionListener() {
#Override
public void onPositionUpdated(GeoPosition loc) {
// the position we get in this callback can be used
// to reposition the map and change orientation.
loc.getCoordinate();
loc.getHeading();
loc.getSpeed();
// also remaining time and distance can be
// fetched from navigation manager
naviManager.getTimeToArrival(true, Route.TrafficPenaltyMode.DISABLED);
naviManager.getDestinationDistance();
}
};
How are you starting the positionManager, maybe you have only passed the GPS option and not the GPS_NETWORK option ?
positioningManager.start(PositioningManager.LocationMethod.GPS_NETWORK);

OnProvideAssistDataListener example

Can someone please provide an example for a real case where I might need to use OnProvideAssistDataListener. I can't seem to wrap my head around it. I look at the source code, and then I look online. Someone online says
Application.OnProvideAssistDataListener allows to place into the
bundle anything you would like to appear in the
Intent.EXTRA_ASSIST_CONTEXT part of the assist Intent
I have also been reading through the Intent Docs.
There is an Now On Tap functionality implemented by Google. By long pressing the Home Button, you will get some  information displayed on the screen.  The information you get depends on what you're viewing on your screen at that time. (for eg: Music app displays information about music on the screen).
To provide additional information to the assistant, your app provides global application context by registering an app listener using registerOnProvideAssistDataListener() and supplies activity-specific information with activity callbacks by overriding onProvideAssistData() and onProvideAssistContent(). 
Now when the user activates the assistant, onProvideAssistData() is called to build a full ACTION_ASSIST Intent with all of the context of the current application represented as an instance of the AssistStructure. You can override this method to place anything you like into the bundle to appear in the EXTRA_ASSIST_CONTEXT part of the assist intent.
In the example below, a music app provides structured data to describe the music album that the user is currently viewing:
#Override
public void onProvideAssistContent(AssistContent assistContent) {
super.onProvideAssistContent(assistContent);
String structuredJson = new JSONObject()
.put("#type", "MusicRecording")
.put("#id", "https://example.com/music/recording")
.put("name", "Album Title")
.toString();
assistContent.setStructuredData(structuredJson);
}
For more info refer https://developer.android.com/training/articles/assistant.html

Showing a route between two points with multiple waypoints

I am new to android and developing a navigation based application for android.My question is I want to show a route with multiple way-points between source and destination node in Google maps and I don't know how to do it. I have search for this but most of the results are for two points.
Please help me to solve my problem
e.g:- when app user submits the destination with some way-points the map should display the route from the source to destination with those way-points on the phone.
Thanks !
I don't think the Maps intent offers those capabilities, but you do have some other options
Better but more work
1)Create your own map activity that displays routes for multi-location(using a mapview and overlays). Here is an example of how to convert the kml file to a route
Quicker and Easier
2)Create a simple webview (or you can just intent a new one with a given url) and dynamically build the url for a google maps api request with waypoints. See Google's website for documentation on getting a map with routes via waypoints.
"I have search for this but most of the results are for two points." You already found everything you need to do this.
For this, you're not "drawing through the waypoints along the way," you're actually going to have to draw a route between 2 points for each waypoint you have:
IE. You want to get from point A to point D, and on the way there are points B and C. The solution is to draw a route from A to B, B to C, and finally C to D.
You can use the map intent like this:
String address = "http://maps.google.com/maps?daddr=" + "Latitude" + "," + "Longitude" + "+to:" +"Latitude" + "," + "Longitude";
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(address));
startActivity(intent);
in this case the start point will be your current location and you can add as many intermediate points as you want using "+to:"

Categories

Resources