Android - Open Google Map and Do a search - android

I need to make an Android app that open Google Map(already installed on my device) and do a search with given text
EX: When I type "Restaurant" in textBox and click button "Search" in my app, it will open Google Maps and search for "Restaurant"
Can anyone help?! Thanks in advance

Make use of the Google Maps Intent to perform this task. Intents help communicate the data from one place to another. In this case its your app and the Google Maps app.
What you need to do is to pass the data that someone has searched in the text box of your app to the Google Maps app directly if its installed. Using geolocation API convert the enterred text into Lat/Lng values. You need to set the package as com.google.android.apps.maps and start the activity.
Here is the full code:
// Create a Uri from an intent string. Use the result to create an Intent.
Uri gmmIntentUri = Uri.parse("google.streetview:cbll=46.414382,10.013988");
// Create an Intent from gmmIntentUri. Set the action to ACTION_VIEW
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
// Make the Intent explicit by setting the Google Maps package
mapIntent.setPackage("com.google.android.apps.maps");
// Attempt to start an activity that can handle the Intent
startActivity(mapIntent);
For complete documentation read the official page.

Related

Open google maps activity from my application and if user reaches the destination redirect the user to my app

I want to open google maps turn by turn direction screen on button click and want to check if user reaches the destination redirect the user to my app currently i am using this code to open google maps
String uri = String.format(Locale.ENGLISH, "geo:0,0?q="+ destinationLoc.latitude+","+destinationLoc.longitude
+ "(The Embroidery Store)" );
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
I want to redirect the user to my app after he reaches the destination.
You may want to look into the concept of geo-fencing.
The following links might help:
https://developer.android.com/training/location/geofencing.html
https://github.com/googlesamples/android-Geofencing

How to get a callback from google maps in Android application, when navigation is complete

I am building an android application, that will show certain points on google maps, for which I have (lat,lang) for all the points shown, with me. Whenever, a user clicks on one of these locations on google maps, I am starting a new Google Maps intent which shows navigation to the user and is supposed to take the user to that (lat, lang).
//suppose this function is called on onclick of google maps event
function onclick(){
Uri gmmIntentUri = Uri.parse("google.navigation:q=32.885240,-96.763475");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
if (mapIntent.resolveActivity(getPackageManager()) != null) {
startActivity(mapIntent);
}
}
This will start the navigation for the user.
Now what I want to achieve is this -
Whenever this navigation is complete, I want some kind of flag to be passed to my android application, so that my application knows that the navigation has been finished.
If it is possible to automatically close the google maps application after the navigation has been completed, it will be nice to have.
Any help is greatly appreciated. Thanks. Anyone has any solutions for this?
You need to call the finish() method on the activity when the destination is reached. So here are the steps that you need to implement in your program.
First of all know the destination coordinates from the user input and pass it in the Direction API.
https://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&key=API_KEY
When you know the destination coordinates you can pass that when the marker representing that is clicked in the navigation intent. (which you have already done)
Make use of the location listener interface that tracks the current location coordinates.
Put an if...else condition where you check that the current location is equivalent to the destination.
When the current location == Destination make a call to activity.finish() which will finally close the maps activity hosting.
Hope that Helps!!

Is it possible to programmatically access the Google Maps' navigation history?

I wonder if it possible to retrieve the Google Maps navigation history. In other words, is there a way of retrieving the recent routes taken/searched by the user of the Google Maps app?
Thanks
You can not finish Google Navigation App.
There is only one work around which I found,
Re-start the navigation with the current location, hence the navigation will automatically be finished.
public static void startNavigationIntent(Context activity, String destination)
{
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("google.navigation:q=" + destination));
activity.startActivity(intent);
}
And then bring your app to the foreground.
Intent bringToForegroundIntent = new Intent(BackgroundLocationService.this, DashboardActivity.class);
bringToForegroundIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(bringToForegroundIntent);
There is no API available to get information out of the Navigation App. But you can feed some data to Maps to request a route through an Intent / URI.
But I don't think that the Maps app will save the whole route anyway, only the destination. The user selects any saved destination and you can get there from any source.

Launch directly the directions on Google Maps

I want to launch the Google Maps app from my android application with the driving directions view.
I gone through the following posts but they don't serve my purpose.
Launch Google Maps to show road directions
How to programmatically launch map appliction in android to show direction?
I tried the following code...
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?f=d&daddr=51.448,-0.972"));
intent.setComponent(new ComponentName("com.google.android.apps.maps",
"com.google.android.maps.MapsActivity"));
startActivity(intent);
Which gives me the following view,
Then I need to click 'Get Directions' button to get the following view,
And then I need to click 'DIRECTIONS LIST' to get the following view,
Is there a way to directly launch the 3rd view (as above) from my android application?
Yes You can directly open direction view Using google.navigation.
Just pass Destination address and it will give direction from current location to destination address:
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("google.navigation:q=destination address"));
startActivity(intent);

Open default navigation app using an address

How can i start an Intent to open either the default navigation application (e.g. Google Navigation or Google Maps) or give the user the opportunity to select among navigation applications.
Thanks in advance.
You have to use an Intent with ACTION_VIEW and the geo data scheme.
The problem using geo:latitude,longitude is that Google Maps only centers at your point, without any pin or label.
That's quite confusing, especially if you need to point to a precise place or/and ask for directions.
If you use the query parameter geo:lat,lon?q=name in order to label your geopoint, it uses the query for search and dismiss the latitude and longitude parameters.
I found a way to center the map with latitude and longitude and display a pin with a custom label, very nice to display and useful when asking for directions or any other action:
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("geo:0,0?q=37.423156,-122.084917 (" + name + ")");
startActivity(intent);
This intent is from the list of official "G apps" intent list here.
Try:
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("google.navigation:q=New+York+NY")); startActivity(i);
Or use this, this way the stardard navigation app will be launched:
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?saddr=20.344,34.34&daddr=20.5666,45.345"));
startActivity(intent);
Add this line before startActivity if you don't want the popup dialog:
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
Also see: How to check programmatically if an application is installed or not in Android? and Android: detect when app is installed, you can make your own Window so the user can choose between some application.

Categories

Resources