Google Maps Navigation in Android - android

I am working with Android and building an app that wants to navigate between 2 points. I already have the points with me. I have the route between them as well on google maps. I just want to navigate between them. Could you please help me on this?

Consider you have two locations source, destination, The below code will redirect you to google navigation, Make it as a method and call when ever you want!.. Let me know if you need any other help
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://ditu.google.cn/maps?f=d&source=s_d" +
"&saddr="+source.getLatitude()+
","+source.getLongitude()+"&daddr="+
destination.latitude+
","+destination.longitude+
"&hl=zh&t=m&dirflg=d"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK & Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
intent.setClassName("com.google.android.apps.maps","com.google.android.maps.MapsActivity");
startActivityForResult(intent, 1);

You can try to use Directions API.
The Google Maps Directions API is a service that calculates directions between locations using an HTTP request.
This service is generally designed for calculating directions for static (known in advance) addresses for placement of application content on a map, however this service is not designed to respond in real time to user input.
A Google Maps Directions API request takes the following form:
https://maps.googleapis.com/maps/api/directions/output?parameters
where output may be either of the following values:
json (recommended) indicates output in JavaScript Object Notation (JSON)
xml indicates output as XML
To access the Google Maps Directions API over HTTP, use:
http://maps.googleapis.com/maps/api/directions/output?parameters
HTTPS is recommended for applications that include sensitive user data, such as a user's location, in requests.
You need this parameter in getting the directions:
origin - The address, textual latitude/longitude value, or place ID from which you wish to calculate directions.
destination - The address, textual latitude/longitude value, or place ID to which you wish to calculate directions. The options for the destination parameter are the same as for the origin parameter, described above.
Check also this tutorial for more information.

It's working fine.
String uri = "http://maps.google.com/maps?f=d&hl=en&saddr="+startingLocation.latitude+","+startingLocation.longitude+"&daddr="+destinationLocation.latitude+","+destinationLocation.longitude;
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(Intent.createChooser(intent, "Select an application"));

Related

Is it possible to draw a path of coordinates and navigate between them, using Google Maps or other apps?

This is a simple question.
I know it's possible to request to navigate to a specific place, as shown here.
I know there are also some other APIs for Google Maps (here and here), but I don't see an option to send multiple coordinates, to see the path between them, and navigate between them.
For the Web version, there is the ability for waypoints (example here and here), but not for Android.
Is it possible to do it, via Google Maps, or another app?
Or maybe even in the Google Maps API (within the current app) ?
Since V3 Google Maps Directions API support up to 23 waypoints (excluding the origin and destination) when calculating routes:
https://maps.googleapis.com/maps/api/directions/json?origin=sydney,au&destination=perth,au&waypoints=via:-37.81223%2C144.96254%7Cvia:-34.92788%2C138.60008&key=YOUR_API_KEY
So, you can draw polyline with up to 23 points and send they coords in request.
UPDATE:
Please see this tutorial.
UPDATE 2:
Example opening navigation mode of Google Maps app for route Tel-Aviv to Jerusalem via Kiryat Malakhi, Beit+Guvrin and geopoint (31.696342, 35.011337) via Intent:
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("https://www.google.com/maps/dir/?api=1&" +
"origin=3780+St+1,+Tel-Aviv+Yafo,+Israel&destination=Jerusalem,+Israel&travelmode=driving" +
"&waypoints=Kiryat+Malakhi,+Israel%7CBeit+Guvrin,+Israel%7C31.696342, 35.011337"));
startActivity(intent);
Result:
Parameter details here.

Is there an Android equivalent to Google Maps URL scheme for iOS?

I want to open Google Maps in Navigation mode from a mobile web link. This seems easy enough for iOS devices using https://developers.google.com/maps/documentation/ios/urlscheme
Is there an equivalent for Android? All I could find was this: https://developer.android.com/guide/appendix/g-app-intents.html
But that doesn't allow you to specify "transitmode" and the other parameters needed to get directions as far as I can tell.
Actually, a slight modification of the methods described in the iOS Doc would work here too (I tested it before putting it here albeit, in a native app and not a web link).
The parameters necessary for this to work are pretty much the same as with the ones listed in the iOS documentation:
From the iOS Docs:
Parameters:
saddr: Sets the starting point for directions searches. This can be a
latitude,longitude or a query formatted address. If it is a query
string that returns more than one result, the first result will be
selected. If the value is left blank, then the user’s current
location will be used.
daddr: Sets the end point for directions searches. Has the same
format and behavior as saddr.
directionsmode: Method of transportation. Can be set to:
driving, transit, or walking.
They are actually, pretty much the same. They are however, no where to be found in the documents. Also, while the first 2 parameters work the usual way here, the last parameter directionsmode does not work as is. A workaround is however, listed below.
That being said, a simple URL can be constructed that can then be passed as an Uri to an Intent that will then handle the application to be launched (Google Maps if installed and/or list of browsers to choose from)
String mapURL = http://maps.google.com/maps?saddr=-33.9417, 150.9473&daddr=-33.92050, 151.04287&dirflg=d
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(mapURL));
startActivity(intent);
A few variations for the Transit Mode:
&dirflg=d = for Driving directions (this is the default mode. leaving it out is the same as putting it in explicityly).
&dirflg=w = for Walking directions
&dirflg=r = for Public transit.
&dirflg=b = for Biking directions.
That being said, at the time of running these test (I admit I was curious enough to test a little further after seeing this question ;-) ), the modes listed in the Travel Modes section don't seem work!
A little proof of sorts:
Note: Credit for the initial discovery of the options

Android: start maps with route

I know it is possible to start google maps with a route of 2 points but I really need to start it with many waypoints. How can this be done?
And another big wish would be: (I really doubt that this is possible but who knows?)
Can you somehow put those waypoints information into an intent so that it isn't necessarily for google maps but for ANY navigation app? So that the user can choose his own navigation app...?
Ok I found a way to do it with google maps:
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("https://maps.google.ch/maps?saddr=[address1]&daddr=[address2] to:[address3] to: [address4]"));
startActivity(intent);
(Replace [address#] with the waypoints)
The problem is that the google maps app doesn't show all the waypoints but only the first and the last. I'd really like to make all waypoints visible and to make navigation actually navigate you to the points and telling you when you've reached one, but google doesn't seem to think that this is needed...

Getting nearby location or Landmarks

Is there any API in google for finding nearby location or Landmarks for my current GPS lat&long position.
Use this :
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=business+near+city");
startActivity(intent);
After geo you put your coordinates, and after q= you input your search tearms, like tourism+city. This will fire up the Google Maps app with the points of interest.
If you want to use a maps view inside your application, you would need to get the data from some service, like your own. However you could pull the data from Google's ajax search like this
http://ajax.googleapis.com/ajax/services/search/local?v=1.0&rsz=large&gl=pl&q=tourism+chicago
more info :
http://code.google.com/apis/websearch/docs/reference.html#_fonje_local
This will give you results that have geo-coordinates and you would need to make a way of parsing the results and maybe get them into a database.
hope it helps...

Android Google Directions Service usable natively? with wrapper framework? Public transit/biking directions available?

Some questions about Android and Google Directions Service:
Is there a native way on Android to use the Directions Service from Google or are there any nice Wrapper Frameworks?
I would like to retrieve the time needed to get from A to B and maybe show the route in a table and/or map.
As far as I know directions can only be retrieved for "walking" and "driving". Is there a way (maybe not from google) to get this Information also for public transports or "biking" ?
I don't know exactly if this is what you want, but you could fire up an intent like this:
String url = "http://maps.google.com/maps?saddr=some+address&daddr=another+Address"
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(url));
To get biking directions add "&dirflg=b" to the url. For public transport add "dirflg=r".
This will display the route in the google Maps app. If you want to get this in your app inside a mapview, please see this code at github. You could hack into the code to add biking and public transport functionality.
Hope this helps somehow.

Categories

Resources