Open Google Maps navigation directions with both coordinates and address - android

We open Google Maps navigation we use lat/long coordinates. The problem is that the address isn't displayed correctly because it's calculated by Google Maps based on the coordinates. I can't find any way to provide the address, which we already have, alongside the coordinates.
The coordinates are actually based on a Google Maps search itself, but when the coordinates are converted back to an address in Google Maps a different address is displayed.
This code snippet is coming from a Flutter app but it should still be pretty self-explanatory.
final intent = AndroidIntent(
action: 'action_view',
data:
'google.navigation:q=${destination.latitude},${destination.longitude}&mode=d',
package: 'com.google.android.apps.maps',
);
The destination is:
{
"lat": 35.6533691,
"lng": 139.7694851,
"address": "5-1 Toyomicho, Chuo City, Tokyo 104-0055, Japan",
"name": "FamilyMart"
}
The Google Maps Intents for Android documentation doesn't offer any extra help. It says that you can only provide either address or coordinates, and neither one really works correctly for us.

If I remember correctly a few years ago, it was possible to set an random name for the location. It doesn't work now, but it makes sense: otherwise, the user can be misled by showing him some address in the input fields, and navigate by completely different coordinates. You are right: for random place conversion lat/lng to address and vice versa can lead to wrong result, but seems for named, widely known places like 'FamilyMart' (it shown as "default" POI on Google Maps) conversion is pretty accurate. Try to use "full" address: not only address value from line "address": "5-1 Toyomicho, Chuo City, Tokyo 104-0055, Japan", but place name from line "name": "FamilyMart" concatenated with it. Something like that:
"FamilyMart, 5-1 Toyomicho, Chuo City, Tokyo 104-0055, Japan"
and try to launch navigation mode via "classical" intent this way:
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("google.navigation:mode=d&q="+Uri.encode("FamilyMart, 5-1 Toyomicho, Chuo City, Tokyo 104-0055, Japan")));
startActivity(intent);
or
final intent = AndroidIntent(
action: 'action_view',
data:
'google.navigation:q=${FULL_ADDRESS}&mode=d',
package: 'com.google.android.apps.maps',
);
If it not works your way is either to navigate by coordinates (without a beautiful caption of the location name), or to write your own fragment, similar to the standard Google Maps.
Also, you can allow user to launch Navigation Directions manually from Google Maps application called by intent:
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("geo:35.6533691+139.7694851?z=19&q="+Uri.encode("FamilyMart, 5-1 Toyomicho, Chuo City, Tokyo 104-0055, Japan")));
startActivity(intent);
It set map centered to "FamilyMart, 5-1 Toyomicho, Chuo City, Tokyo 104-0055, Japan" that found on map visible on screen at zoom level 19 near with center at(35.6533691,139.7694851) lat/lng coordinates.

String strUri = "http://maps.google.com/maps?q=loc:" + latitude + "," + longitude + " (" + yourLocationName + ")";
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(strUri));
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);

Related

Latitude and Longitude from google's geocoder not working on waze

I am trying to make this link work on waze app:
https://waze.com/ul?ll=-58.440634,-34.611324&z=10
Those are the lattitude and longitude given by google's geocoder with address Felipe Vallese 490, Buenos Aires):
https://google-developers.appspot.com/maps/documentation/utils/geocoder/#q%3Dfelipe%2520vallese%2520490
On the web it points out the location:
But in the app (clicking the link on an email and selecting open with waze) it says "uh oh could not locate endpoint".
If i search the address "Felipe Vallese 490" in the app it finds it and let me navigate to it as well.
Reading the docs i see there's a search way:
https://waze.com/ul?q=Felipe%20Vallese%20490
In the app it give me the list of results and another tab with "Places", there i can select the location but i think giving this option to users is not very suitable, i think this will be ok if i can make it show "places" tab as default not search results.
I also tried with different lat and long with same results.
What could it be?.
Reggards!
If you directly want to open the Waze you can read the below link:
https://developers.google.com/waze/deeplinks#navigate-to-location
overall Waze deep link should be something like this format:
https://www.waze.com/ul?ll=35.699636,51.337608&navigate=yes&zoom=17
Otherwise, if you want to open all navigation apps list and select an application using the below code:
val uri = Uri.parse(
String.format(
"geo:0,0?q=%s,%s",
latitude,
longitude
) //result would be like : geo:0,0?q=35.699636,51.337608
)
val geoIntent = Intent(ACTION_VIEW, uri).apply {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
flags = FLAG_ACTIVITY_REQUIRE_NON_BROWSER
}
}
startActivity(geoIntent)

Using Google Maps and Baidu Maps in same app

I'm wondering if there are anyone out that have implemented Google Maps V2 and Baidu Maps in the same version; because GM doesn't work as intended in China?
Or should I split the project into two branches instead? However it would be nice to skip having two branches to maintain.
My solution for this was to implement GM as usual, however if the user has China set (via settings) static maps is to be used, BUT the static map is fetched from Baidu instead of google.
staticUrl = "http://api.map.baidu.com/staticimage?center="
+ location.getLongitude() + "," + location.getLatitude()
+ "&width=" + width + "&height=" + width + "&zoom=15"
+ "&markers=" + location.getLongitude() + "," + location.getLatitude();
Result of https://api.map.baidu.com/staticimage?center=121,31&width=300&height=300&zoom=15:
This method is NOT recommended if trying to implement a real map solution.
Since I have different locations only used by different countries, this solution could be used.
So, that is how I solved it. Hope someone finds this helpful.
Also, I have found that if you use http://ditu.google.cn while in China, it does work.
When using on-line maps in China for your application, whether it's Google Maps or Baidu, there is a transformation of latitude and longitude for legal reasons.
The satellite view in Google Maps uses "Earth" (WGS-84) coordinates. The map view of GMaps in China uses "Mars" coordinates (GCJ-02), and there is code to convert between the two. Baidu maps use the "Bearpaw" coordinates, with a different offset. The Baidu Map API has a demo converting between Google's coordinates and its own systems.
In China, GPS, like everything, has an extra layer of complication :)
If you have built this app, please post the details. Having an English interface to Baidu maps would be great.
You can use both Google Maps and Baidu Maps side by side, but make sure to convert from the WGS-84 coordinates (used by most of the world) to Baidu's coordinates (BD-09, different from China's GCJ-02). Here's some code that does that, based on an example from the Baidu Maps API:
// Google coordinates
var gPoint = new BMap.Point(121.4914, 31.2423); // lon, lat of the Bund Museum in Shanghai - https://www.google.com/maps/#31.2423,121.4914,19z
// gPoint = new BMap.Point(-122.0851053, 37.4219593); // lon, lat of the Googleplex (no Baidu map data but zooms out in Mountain View)
var labelOffset = { offset: new BMap.Size(20, -10) };
// Initialize map
var map = new BMap.Map('allmap');
map.centerAndZoom(gPoint, 15);
map.addControl(new BMap.ScaleControl({anchor: BMAP_ANCHOR_TOP_LEFT})); // add scale
map.addControl(new BMap.NavigationControl());
map.addControl(new BMap.MapTypeControl()); // map type control: street/satellite/2.5D
map.enableScrollWheelZoom(); // mouse wheel scroll is disabled by default
// Add Google marker and label
var markerG = new BMap.Marker(gPoint);
map.addOverlay(markerG);
markerG.setLabel(new BMap.Label('Google coordinates marker appears<br/>at incorrect location on Baidu Map', labelOffset));
// Coordinate conversion ... GCJ-02 coordinates ... Baidu coordinates
BMap.Convertor.translate(gPoint, 2, function (point) {
var marker = new BMap.Marker(point);
map.addOverlay(marker);
marker.setLabel(new BMap.Label('Converted to Baidu coordinates:<br/>' +
point.lng + ', ' +
point.lat +
'<br/>(note the offset of ' + (map.getDistance(gPoint, point)).toFixed(2) + ' meters)',
labelOffset));
map.addOverlay(new BMap.Polyline([gPoint, point])); // draw a line between points
});
<style type="text/css">
body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
</style>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=gd0GyxGUxSCoAbmdyQBhyhrZ"></script>
<script type="text/javascript" src="http://developer.baidu.com/map/jsdemo/demo/convertor.js"></script>
<div id="allmap"></div>
If the snippet above doesn't work due to the way StackOverflow sandboxes scripts, try the JSbin demo of Google -> Baidu coordinates conversion instead.
If you must perform the conversion offline, check out the evil transform project on GitHub.
It's unclear though what coordinate types browsers localized in Chinese will return via the navigator.geolocation API. I've made a test app for that and posted the question at
Showing navigator.geolocation.getCurrentPosition in Baidu Maps.
Further reading:
What causes the GPS shift in China?
Restrictions on geographic data in China
PROBABLY a bit late to the party, but I accidentally stumbled across something recently which might help you.
I tried baidu maps and it was shockingly difficult to setup and terrible to use so I had a look around and suddenly, google maps worked for me without a vpn!
I realised that the old google china server was still active and if you try:
maps.google.cn
you'll find that creating an iframe using the google.cn address works!
Try to use this way with Google coordinate
http://api.map.baidu.com/marker?location=39.916979519873,116.41004950566&output=html
If your server can access GM without issues (eg. your hosting is not in China mainland or it is but has uncensored connection), why don't you have server do loading data from GM and route it to user instead? We did that for few projects in the past, worked like a charm.
p.s. you could make php pull static map from GM for requested long/lat, store it into temp file on server, then pass back url to the temp file. From user's perspective they would be looking at (static) GM.
p.p.s. If you need user to be able to use GM's UI (do pan/zoom) then you'd need a bit more complex php that would alter all JS loaded from GM so all data would still be requested to your server which would then get maps - so basically to avoid any requests from client machine to be sent to GM server, but all to be sent to yours instead.

Android Google api v2 navigation route map without using parsing

I want to create a route map navigation in Android google map v2 along with all those details which google provides( like at this crossing you have to turn left). I know this can be done using a parsing techniques i have done that but what im looking for is, is there any new method predefined within android v2 google map which will give all these details and will show the route map within two points.
Again Im mentioning it not using the JSON parsing technique. anything newly implemented for v2
Any sample projects or examples is very much helpful.
thanks
Without using json parsing technique to draw routes can be done by one method linking to google navigation by doing stuff like this.
String address = marker.getTitle();
Intent intent;
if (address == null) {
LatLng pt = marker.getPosition();
String lat = Double.toString(pt.latitude); //this i mentioned with current location
String lng = Double.toString(pt.longitude);
intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("google.navigation:q=" + lat + "," + lng));
However for Google map direction, json parsing is best.
No, there is only the Google Directions API available for a route response, which includes JSON parsing.

Android Navigation intent

If you mail a Google Maps direction to your Android phone, you have the possibility to open it in the Maps appliaction, this seems perfectly logical, as does the code behind it.
Now, once in the Maps App, you have the possibility to open these directions in the Navigation App, with those exact directions.
How does this work? It must not be that difficult to do it, I know about the intent with
"google.navigation:q=..."
But this only works on some devices and only with coördinates or addresses... No Maps directions?
Can anyone help me out with this?
EDIT:
This is what the URL looks like:
https://maps.google.com/maps?saddr=Durbanville,+Cape+Town,+Western+Cape,+South+Africa&daddr=Parow+North,+Cape+Town,+South+Africa+to:Somerset+West,+Cape+Town,+South+Africa+to:Milnerton,+Cape+Town,+South+Africa&hl=en&ll=-33.955037,18.657532&spn=0.25032,0.528374&sll=-33.911454,18.601913&sspn=0.250448,0.528374&geocode=FczB-_0dzIkcASlBKWkzGlfMHTFTuxOUSmpCAw%3BFQTi-v0d5oMbASld0qgMSFrMHTG2XqWY145Ttw%3BFfUG-P0dPHEfASk398T7ZbXNHTG5a6EH84n4Qg%3BFVU8-_0doEkaASnrz9UPVVnMHTFz2N4nnkA7XQ&oq=parow&mra=ls&t=m&z=12
If you create a web url in the format
http://maps.google.com/maps?saddr=[lat 1],[lon 1]&daddr=[lat 2],[lon 2]
where [lat 1] and [lon 1] are the latitude and longitude for the start point, and likewise [lat 2] and [lon 2] are the end point, and set it as a String, you can then send this to an intent:
Intent navIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(navigationUrl));
activity.startActivity(navIntent);
This will give the user the option of opening a Web Browser, Google Maps, or Navigation.
EDIT:
For multiple destinations add the following to the end of the url (for each additional place)
+to:[lat 3],[lon 3]
as required.
So for 4 destinations you would have:
http://maps.google.com/maps?saddr=[lat 1],[lon 1]&daddr=[lat 2],
[lon 2]+to:[lat 3],[lon 3]+to:[lat 4],[lon 4]
After following answers above, I find myself in a country that Google Navigation service is not available. But the navigation intent still can be accomplished with Google Map by this:
Uri routeUri = Uri.parse("http://maps.google.com/maps?&saddr=" +
MyApplication.Lat + "," +
MyApplication.Lon + "&daddr=" + merchantAddr.getText());
Intent i = new Intent(Intent.ACTION_VIEW, routeUri);
startActivity(i);
It seems we can set the start point to Geo location with lat/lon, and end point to an address, and vice versa. The geo location seemed to be transformed to an address or road name in the Google Map.
The url should like this:
String navigationUrl = "http://maps.google.com/maps?saddr=latitude,longitude&daddr=latitude,longitude";
Intent navIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(navigationUrl));
startActivity(navIntent);

Showing a marker at a geo:-url in Android Maps

Is it possible not only to have Google Maps on Android show a given coordinate in the Maps Application but have also a marker (or pin) set at the location?
I read the documentation at https://developer.android.com/guide/appendix/g-app-intents.html but it only lets me set the zoom level.
Right now I use the following code to show a place in Google Maps on Android:
Double lat = 53.3;
Double lng = 13.4;
final String uriString = "geo:" + lat + ',' + lng + "?z=15";
Intent showOnMapIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uriString));
startActivity(showOnMapIntent);
Is it possible to have a marker there or do I need to use a MapActivity?
Where can I find the complete documentation on the url parameters that the Maps application understands?
Can I use a different url prefix? For example "https://maps.google.de/maps?"?
Maps has an intent-filter matching this scheme/host/pathPrefix.
Where can I find documentation on which parameters Google Maps for Android actually supports with this url?
Dirk, have you tried geo:0,0?q=lat,lng?
it is displaying a marker on my nexus 5.
It works with the follwoing url:
final String uriString = "http://maps.google.com/maps?q=" + lat + ',' + lng + "("+ label +")&z=15";
The reverse geo coded address is shown.
I found documentation to the supported parameters here. This is not the documentation for Androids Google Maps but what I tried works.
Note: it should be noted that the original question was in regards to the the geo: URI to launch the Google Maps app (see Android Developer Reference Intents List, whereas this answer might launch a web view with the Google Maps website depending on what App the user chooses for this Intent.
try this:
http://maps.google.com/?saddr=34.052222,-118.243611
and to get the complete route between two points:
http://maps.google.com/?saddr=34.052222,-118.243611&daddr=37.322778,-122.031944

Categories

Resources