Getting direction between coordinates without using Google direction web service - android

I know it is possible to get direction between coordinates using Google's direction web service. But is there any other way to get equally accurate route?
I found few questions on SO those use http://maps.google.com/ to get direction. But then also found few answers on other questions which state that it is no longer supported.
I am confused as this is the first time I am dealing with Google maps for android.
Is there any built in android sdk method to get direction?

you can send an intent to the google maps app and have the app do all the work for you but its not documented and probably never will be.
to do that you can either give it a lattitue/longitude like this
Intent NavIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("google.navigation:q=" +latitude +","+longitude));
startActivity(NavIntent);
or using the same intent but with an address and see if maps can resolve the address. (I dont have an example with an address).
Other than using google directions API or other 3rd party direction web services there really is no other way to get directions in your app unless you look at Open Street Maps where you calculate the directions yourself using way points but that is quite involved

Firing an intent to the google maps app is what I've choosen to use in my application, the philosophy here is that Android apps should exploit and complement each other functionnlities
Intent i = new Intent(Intent.ACTION_VIEW,Uri.parse(getDirectionUrl(srcLat, srcLng, dstLat, dstLng)));
if (isGoogleMapsInstalled(this)) {
i.setComponent(new ComponentName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity"));
}
startActivity(i);
method to build the directions URL:
public static String getDirectionUrl (double srcLat, double srcLng, double dstLat, double dstLng) {
//return google map url with directions
StringBuilder urlString = new StringBuilder();
urlString.append("http://maps.google.com/maps?f=d&saddr=")
.append(srcLat)
.append(",")
.append(srcLng)
.append("&daddr=")
.append(dstLat)
.append(",")
.append(dstLng);
return urlString.toString();
}
Method to test if Maps is installed:
public static boolean isGoogleMapsInstalled(Context c) {
try
{
#SuppressWarnings("unused")
ApplicationInfo info = c.getPackageManager().getApplicationInfo("com.google.android.apps.maps", 0 );
return true;
}
catch(PackageManager.NameNotFoundException e)
{
return false;
}
}
The downside is that Google may change the URL structure.

Related

Find nearest exit while rerouting navigation in a restricted road using here maps

I am using here maps android sdk premium version for map navigation in my android app.
I have successfully implemented here maps navigation. While navigating, if I go out of route then reroute calculation occurs and new route to given destination is shown but if I go through a restricted road, then here maps only says rerouting which is true since from restricted road it cannot calculate the route.
But is there any way I can show nearest exit from the restricted road to get a new route to destination on here maps android? Right now, on the restricted roads, no route is shown.
#Override
public void onRouteUpdated(#NonNull Route route) {
Log.d(TAG, "onRouteUpdated: called.");
//remake new route
map.removeMapObject(mapRoute);
// create a new MapRoute object
mapRoute = new MapRoute(route);
// display new route on the map
map.addMapObject(mapRoute);
}
The above code is not useful while on restricted road. Tried using listener as:
private NavigationManager.RerouteListener rerouteListener = new NavigationManager.RerouteListener() {
#Override
public void onRerouteBegin() {
super.onRerouteBegin();
Toast.makeText(activity, "reRouteListener begin...", Toast.LENGTH_SHORT).show();
}
#Override
public void onRerouteEnd(#NonNull RouteResult routeResult, RoutingError routingError) {
super.onRerouteEnd(routeResult, routingError);
StringBuilder stringBuilder = new StringBuilder();
for (RouteResult.ViolatedOption violatedOption: routeResult.getViolatedOptions()) {
stringBuilder.append(violatedOption.toString());
}
Toast.makeText(activity, "routeresult end: " + stringBuilder, Toast.LENGTH_SHORT).show();
}
};
Sometimes got violated options as START_DIRECTION and sometimes nothing. But still stuck on how to achieve nearest route as such on restricted road.
[update] :
I tried suggestion from #Datasun. But I got invalid routing on initiating new route from restricted road (tried from height restricted road). My problem is while navigating, if user goes to restricted road, the route disappears and it says route recalculating. I want to show user nearest exit from the restricted road or some notification that they are on restricted road. Right now I'm getting violated options of START_DIRECTION but sometimes nothing on the restricted road. What am I missing?
To summarize, within the Navigate SDK the issue is not reproducable any more.
In general the HERE Premium SDK (3.x) is superseded by new 4.x SDK variants and the Premium SDK will be maintained until 31 December 2022 with only critical bug fixes and no feature development / enhancements.
Current users of the HERE Premium SDK (3.x) are encouraged to migrate to Lite, Explore or Navigate HERE SDK (4.x) variants based on licensed use cases before 31 December 2022. Most of the Premium SDK features are already available in the new SDK variants.

Stuck on getting Yelp API to work with Lat/Long in Android

I'm pretty much on the last step to getting my app to work before I start focusing on the front-end design. As someone who just started programming just last year and haven't made any serious apps before, this is very exciting, but I hit a wall that I need help getting out of. I know this is a stretch but here goes.
I was able to use the google maps API and OSRM API with no problems using the simple URL object from java.net.URL, but that was because the APIs did not require any type of OAuth authentication.
Link
For Yelp's API, I need to use OAuth and they provide sample code located here
I copied both YelpAPI.java and TwoStepOAuth.java and the search works when I try searching by location name. However, I need to change the code to work with lat/long coordinates.
https:// www.yelp.com/developers/documentation/v2/search_api
By going to their Search API documentation, I thought that replacing:
request.addQuerystringParameter("location", location);
with
request.addQuerystringParameter("ll", coordinate);
where coordinate can be any lat/long coordinate such as
String coordinate = 34.095287, -118.1270146
However, that did not work. I'm not sure where to go at this point. I've been stuck on this issue for two days and I'm unable to find any resources online to help me. Thanks for reading.
to search yelp data by lat, lng I use the below method
public String search(String term, double latitude, double longitude) {
OAuthRequest request = new OAuthRequest(Verb.GET, "http://api.yelp.com/v2/search");
request.addQuerystringParameter("term", term);
request.addQuerystringParameter("ll", latitude + "," + longitude);
this.service.signRequest(this.accessToken, request);
Response response = request.send();
return response.getBody();
}
Now all you just need is to cast your string co-ordinates to double.

Display text on google map in android

I am displaying a location on Google map by passing latitude longitude values.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("geo:0,0?q=" + (" "+dblLatitude+", "+dblLongitude+" ")));
try {
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
setContentView(R.layout.map_locate);
}
Its working fine for me. But I also want to show text on above the located point. Any idea how to pass the text dynamically like I am passing lat, long values?
To show a label on Google Maps application you have to add it in brackets after coordinates.
See this answer: https://stackoverflow.com/a/7405992/2183804
You can put extra data into the intent object using e.g. the putExtra(String,String) method on the intent object.
As you firing an intent to turn on the native Google Maps Application. I don't think that what you are trying to achieve is possible. AFAIK all you can add to the desired location is an icon.
If you want to add to it more functionality you would have to develop your own map application.
UPDATE:
To develop you own map, you can take a look at this blog post I wrote on that matter:
Google Maps API V2
If you have questions don't hesitate to ask.

osmdroid, Maps API v2, and fragments

Ok. You'd think this would be relatively simple, but nope.
I am using Open street maps on my website, as the data is free to use, edit, and update - my project follows this mantra, and alongside googles API usage restrictions/saving data restrictions, google maps simply was not suitable for me.. at least on the web.
Given this I thought i'd follow suit with my android app, and so I setup osmdroid utilizing open street maps data.
I can have a nice map in my activity.. I also followed a tutorial, and it seemed to work at least such that I can have a map view in a fragment within a tabhost/viewpager. What i really want however (for tablets), is a list of establishments in a fragment on the left, and a map fragment on the right showing where these establishments are.
Simple enough i would have thought, but hours and hours of research suggest this is not possible unless you use some complex code and some deprecated methods..
So.. the fragment (containing a map view) loads and runs perfectly in a tabhost.. i can access methods within the fragment from my activity etc.. yet nothing to just simply have two fragments side by side.
Now.. I know google have just come out with their API v2.. I installed it and had a play.. not really knowing how osmdroid works, I thought i could update so I have MapFragment instead of SherlockFragment (I use ABS).. this then just threw up logcat errors asking for API keys etc.. and given that I dont want to use google map data, I assumed that I had gone wrong.
So.. could anyone advise on how I can get a list fragment, and a map fragment side by side using anything that is available on the market, but preferably utilizing Open source map data such that their are no usage restrictions.
I'm sure an overall overview of "what is available, and how it works" would be very much appreciated by loads of users.. so if anyone could advise it would be amazing !
Before the new Google Maps API came out, I was forced into using OSMDroid for my purposes. However, after having to dig through the source several times to figure out why it was doing what it was (often incorrectly), I was dying for a new library.
Fortunately, the new Google Maps API fits the bill. You can use the Google Maps API v2 in your application and never have to use Google's basemaps, but yes, unfortunately you must get an API key first.
I am assuming you want to use OpenStreetMap as an online map source, so the code below will be tailored towards that.
GoogleMap map = <get-map-instance>;
map.setMapType(GoogleMap.MAP_TYPE_NONE);
TileOverlayOptions options = new TileOverlayOptions();
options.tileProvider(new UrlTileProvider(256, 256) {
#Override
public URL getTileUrl(int x, int y, int z) {
try {
String f = "http://tile.openstreetmap.org/%d/%d/%d.png";
return new URL(String.format(f, z, x, y));
}
catch (MalformedURLException e) {
return null;
}
}
});
map.addTileOverlay(options);
You can find a more formalized version of this code in my Google Maps API v2 add-ons library here.
Also, if you are targeting pre-Fragment Android versions, which I believe you are based on you using ABS, instead of using MapFragment, make sure you use SupportMapFragment.
The Sample Code delivered with Google Maps Android API v2 contains the class com.example.mapdemo.TileOverlayDemoActivity. I little update to this class will show OpenStreetMap tiles within the new v2 framework. Cool! No need to use osmdroid or sherlock-fragments anymore. However... the tiles are not cached :(
Anyway, all additions/updates to the TileOverlayDemoActivity class are shown below:
private static final String OPEN_STREET_MAP_URL_FORMAT =
"http://tile.openstreetmap.org/%d/%d/%d.png";
private void setUpMap() {
mMap.setMapType(GoogleMap.MAP_TYPE_NONE);
mMap.setMyLocationEnabled(true);
TileProvider tileProvider = new UrlTileProvider(256, 256) {
#Override
public synchronized URL getTileUrl(int x, int y, int zoom) {
String s = String.format(Locale.US, OPEN_STREET_MAP_URL_FORMAT, zoom, x, y);
URL url = null;
try {
url = new URL(s);
} catch (MalformedURLException e) {
throw new AssertionError(e);
}
return url;
}
};
mMap.addTileOverlay(new TileOverlayOptions().tileProvider(tileProvider));
}

Android Google Maps talking to Google Maps Enterprise Data?

Is it possible to switch the Android Google Maps code to utilize custom data provided by a Google Enterprise?
Details:
Google provides an "enterprise" version of its maps service, where you can host your own data. See here:
http://www.google.com/enterprise/earthmaps/earth_technical.html
But Android's Maps API has no way of using any alternative data sources other than what is freely available on the web.
http://code.google.com/android/add-ons/google-apis/index.html
There are other tools like Open Street Maps or AndNav, but here, I need to use Google.
There is no API for switching the MapView data source, and since it is not open source, there's no obvious way to change it.
You can, however, use WebView to embed a standard Web-based Google Maps that could, presumably, come from your "enterprise" edition.
With the new V2 api on android you can set the tile source by providing a custom TileProvider. I am not familiar with the enterprise offerings, but this should be possible if the tiles are available via a url. The exact implementation will depend on how the tiles are accessed, but here some code to get started:
map.setMapType(GoogleMap.MAP_TYPE_NONE);
TileOverlayOptions options = new TileOverlayOptions();
options.tileProvider(new UrlTileProvider(256, 256) {
#Override
public URL getTileUrl(int x, int y, int z) {
try {
String f = "http://yourURL/%d/%d/%d.png";
return new URL(String.format(f, z, x, y));
}
catch (MalformedURLException e) {
return null;
}
}
});
map.addTileProvider(options);

Categories

Resources