How to center Google maps driving directions between destination and source - android

I am using google maps URL commands to draw driving directions from a source to destination using the following commands:
String geoUriString = "http://maps.google.com/maps?" +
"saddr=" + latitude + "," + longitude + "&daddr=" + destLAT+ "," + destLONG;
then
Intent mapCall = new Intent(Intent.ACTION_VIEW, Uri.parse(geoUriString));
startActivity(mapCall);
This opens a list of options, when I choose google maps, it shows me the driving directions from the source to destination.
I have this question:
how can I zoom the map out to fit the drawn path from source to destination? (because now it is zoomed in too much, the full path is not shown at once)

var mapOptions = {
zoom: 4,
center: new google.maps.LatLng(18.49866955, 73.8957357166667),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map_canvas'),
mapOptions);

Related

Use OSMDROID to display my server generated map tiles

I am trying to display map tiles using osmdroid. but it is displaying blank screen.
This is my code:
map.setZ(100);
map.setTileSource(new OnlineTileSourceBase("", 12, 18, 256, ".png",
new String[]{"https://storage.googleapis.com/brainpool/user-files/brainpoollicense/maptiles/PL53LY4K0EDYMX/"}) {
#Override
public String getTileURLString(long pMapTileIndex) {
return getBaseUrl()
+ MapTileIndex.getZoom(pMapTileIndex)
+ "/" + MapTileIndex.getX(pMapTileIndex)
+ "/" + MapTileIndex.getY(pMapTileIndex)
+ mImageFilenameEnding;
}
});
map.setMultiTouchControls(true);
IMapController mapController = map.getController();
GeoPoint startPoint;
startPoint = new GeoPoint(151.2927108765, -32.779342448);
mapController.setZoom(12F);
mapController.setCenter(startPoint);
map.invalidate();
createmarker();
This is output:
Also, note that i am getting this in output always:
W/OsmDroid: Problem downloading MapTile: /12/1674/2 HTTP response: Not Found
My question is when getTileURLString method is called? because it is called for the values which are taking wrong z/x/y which do not exist on the server.

Create a circuit route by passing waypoints to GoogleMaps android

I am working on functionality where I send waypoints to Gmapsapp through Intent so that use can navigate to the destination by the custom waypoints that I send
I when I plot this route in my embedded Google Maps , I can see Circuit route , but when I see the same route in Gmapsapp , the Circuit is broken.
My code :
String srcAdd = "saddr="+latLngArrayList.get(0).latitude+","+latLngArrayList.get(0).longitude;
String desAdd = "&daddr="+latLngArrayList.get(latLngArrayList.size() - 1).latitude+","+latLngArrayList.get(latLngArrayList.size() - 1).longitude;
String wayPoints = "";
for (int j = 1; j < latLngArrayList.size() - 1; ++j) {
wayPoints =wayPoints+"+to:"+latLngArrayList.get(j).latitude+","+latLngArrayList.get(j).longitude;
}
String link="https://maps.google.com/maps?"+srcAdd+desAdd+wayPoints;
final Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(link));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);
circuit route
no circuit route
I would suggest having a look at Google Maps URLs API that was launched in May 2017. This API provides universal cross-platform links that you can use in your applications to launch intents for Google Maps. One of the supported modes is directions mode. You can read about it here.
As you use Directions API and posted sample coordinates of waypoints, I was able to test the results in web service and in Google Maps URLs.
The web service results are tested in Directions calculator tool:
https://directionsdebug.firebaseapp.com/?origin=19.07598304748535%2C72.87765502929688&destination=19.07598304748535%2C72.87765502929688&waypoints=18.7284%2C73.4815%7C18.6876%2C73.4827%7C18.5839587%2C73.5125092%7C18.5369444%2C73.4861111%7C18.480567%2C73.491658
The route that we get via Directions API is the following:
The Google Maps URLs link for these waypoints is the following:
https://www.google.com/maps/dir/?api=1&origin=19.07598304748535,72.87765502929688&destination=19.07598304748535,72.87765502929688&waypoints=18.7284,73.4815%7C18.6876,73.4827%7C18.5839587,73.5125092%7C18.5369444,73.4861111%7C18.480567,73.491658&travelmode=driving
The route that you get using Google Maps URLs is shown in this screenshot.
As you can see both routes are the same, so Directions API and Google Maps URLs work as expected. I believe you should change your code for intents to use Google Maps URLs:
String srcAdd = "&origin=" + latLngArrayList.get(0).latitude + "," + latLngArrayList.get(0).longitude;
String desAdd = "&destination=" + latLngArrayList.get(latLngArrayList.size() - 1).latitude + "," + latLngArrayList.get(latLngArrayList.size() - 1).longitude;
String wayPoints = "";
for (int j = 1; j < latLngArrayList.size() - 1; j++) {
wayPoints = wayPoints + (wayPoints.equals("") ? "" : "%7C") + latLngArrayList.get(j).latitude + "," + latLngArrayList.get(j).longitude;
}
wayPoints = "&waypoints=" + wayPoints;
String link="https://www.google.com/maps/dir/?api=1&travelmode=driving"+srcAdd+desAdd+wayPoints;
final Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(link));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);
In addition you can use dir_action=navigate parameter in order to open turn-by-turn navigation directly.

Google map direction intent with multiple location on route and starting point as current location, android

I want to start Google map intent for direction with current location as starting position, one location as entry position and other location as destination.
After googling for this, following is the closest query I came with
https://maps.google.com/maps?f=d&daddr=41.3951982,-72.855568 to: 41.279386, -72.825098
OR
"https://www.google.com/maps/dir//41.3951982,-72.855568/41.279386,-72.825098"
Both of them work good when I paste on browser, but not on my mobile device. I just get source and destination. Any ideas?
EDIT:
Actually the second one works. Unlike browser, it only shows starting location and first point, but has a full navigation route. :)
There isn't any google approved way of doing it (as far as I looked to documentation). But I found this solution working pretty good
private void navigate(List<LatLng> latLngs) {
String uri = "";
for (LatLng latLng : latLngs) {
if (TextUtils.isEmpty(uri)) {
uri = String.format(
"http://maps.google.com/maps?saddr=%s, %s",
String.valueOf(latLng.latitude).replace(",", "."),
String.valueOf(latLng.longitude).replace(",", ".")
);
} else {
if (!uri.contains("&daddr")) {
uri += String.format(
"&daddr=%s, %s",
String.valueOf(latLng.latitude).replace(",", "."),
String.valueOf(latLng.longitude).replace(",", ".")
);
} else {
uri += String.format(
"+to:%s, %s",
String.valueOf(latLng.latitude).replace(",", "."),
String.valueOf(latLng.longitude).replace(",", ".")
);
}
}
}
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(intent);
}
Google Maps app on Android doesn't support multiple destinations. You can try to draw on the map by using native Google Maps Android API. You can get the directions from Google Maps Directions API.

Open map at specific coordinates not working on Android

I'm building an application with the functionality of sending specific pre-determined (but dynamic) coordinates to the user's map app so he can trace a route to it.
Currently, I'm using:
String coordinates = String.format("geo:0,0?q=" + latitude + "," + longitude);
Intent intent = new Intent( Intent.ACTION_VIEW, Uri.parse(coordinates) );
startActivity( intent );
However, when it does open the map, instead of the requested location I get a "no results for [latitude], [longitude]" toast and my current location instead.
It's certainly not an issue with the coordinates themselves as manually searching for them work just fine and printing the request Uri show that it's correctly constructed. Surprisingly, only sending the first two digits of both coords sort of works and, while doesn't send me where I want to, does not give the toast error message.
Do I need to do any extra formatting when passing the values or some other thing?
I'm using them raw, -23.561261 and -46.681212 for example, am located in Brazil if that makes any difference and, yes, I do have to send the coordinates as sadly the data is inconsistent with the formatting of the actual addresses.
UPDATE: As it turns out, the code is fine, it works on my razr-i, however, in the Galaxy Express I used for the original tests, it's still a no go.
Any idea of what is going on? Both devices are running Android 4.1.2
Don't quite remember what the error was since it's been so long ago, but long story short, here's the working code:
String coordinates = "http://maps.google.com/maps?daddr=" + latitude + "," + longitude;
Intent intent = new Intent( Intent.ACTION_VIEW, Uri.parse(coordinates) );
startActivity( intent );
Your code is correct,
the values that you send are double?
double latitude = -23.561261;
double longitude = -46.681212;
String coordinates = String.format("geo:0,0?q=" + latitude + "," + longitude);
Intent intentMap = new Intent( Intent.ACTION_VIEW, Uri.parse(coordinates) );
startActivity( intentMap );
must be something similar to load directly in your browser the url:
http://www.google.com/maps?q=-23.561261+-46.681212
The problem is in locale. Your code will work on any phone if you call format() like this:
String coordinates = String.format(Locale.ENGLISH, "geo:%f,%f", latitude, longitude);

Infobox on polygons in Bing Maps Android SDK

I'm using the Bing Maps Android SDK and I'm looking for a way to click on the polygons that I have created and show an infobox. I've been able to accomplish this for a pushpin, but not for a polygon. I have seen this answer, but my app needs will make hundreds of such polygons, and I'm looking for a faster solution using the addHandler method on polygons. I know this is possible for the AJAX v7 flavour of the SDK, which is the underlying base of the Android SDK.
The code I tried for the AJAX version (tested using this emulator.)
map.entities.clear();
latlon = map.getCenter();
var polygon = new Microsoft.Maps.Polygon([new Microsoft.Maps.Location(latlon.latitude, latlon.longitude-0.15), new Microsoft.Maps.Location(latlon.latitude+0.1, latlon.longitude-0.05), new Microsoft.Maps.Location(latlon.latitude+0.1, latlon.longitude+0.05), new Microsoft.Maps.Location(latlon.latitude, latlon.longitude+0.15), new Microsoft.Maps.Location(latlon.latitude-0.1, latlon.longitude+0.05), new Microsoft.Maps.Location(latlon.latitude-0.1, latlon.longitude-0.05), new Microsoft.Maps.Location(latlon.latitude, latlon.longitude-0.15)], null);
Microsoft.Maps.Events.addHandler(polygon, 'click', DisplayInfo);
map.setView( {zoom:10});
map.entities.push(polygon);
function DisplayInfo (e) {
var vertices = e.target.getLocations();
var verticeCenter = new Microsoft.Maps.Location(0,0);
//Calculating location of center
for (i=0; i<vertices.length-1; i++) {
verticeCenter.latitude = verticeCenter.latitude + vertices[i].latitude;
verticeCenter.longitude = verticeCenter.longitude + vertices[i].longitude;
}
verticeCenter.latitude = verticeCenter.latitude / (vertices.length - 1);
verticeCenter.longitude = verticeCenter.longitude / (vertices.length - 1);
defaultInfobox = new Microsoft.Maps.Infobox(verticeCenter, {width: 200, height: 50} );
map.entities.push(defaultInfobox);
}
However, I pushed a similar code to the BingMapsAndroid.js from the assets folder of the SDK, but that doesn't work. The handler is attached, as I checked using the hasHandler method. Touches are recorded and their lat and long values are sent to the log, but the polygon event is not evoked even when the touch lies inside a polygon.
Polygon test function in BingMapsAndroid.js:
this.PolygonTest = function() {
_map.entities.clear();
latlon = new Microsoft.Maps.Location(1,1);
console.log("Polygon test function");
var polygon = new Microsoft.Maps.Polygon([new Microsoft.Maps.Location(latlon.latitude, latlon.longitude-0.15), new Microsoft.Maps.Location(latlon.latitude+0.1, latlon.longitude-0.05), new Microsoft.Maps.Location(latlon.latitude+0.1, latlon.longitude+0.05), new Microsoft.Maps.Location(latlon.latitude, latlon.longitude+0.15), new Microsoft.Maps.Location(latlon.latitude-0.1, latlon.longitude+0.05), new Microsoft.Maps.Location(latlon.latitude-0.1, latlon.longitude-0.05), new Microsoft.Maps.Location(latlon.latitude, latlon.longitude-0.15)], null);
try {
Microsoft.Maps.Events.addHandler(polygon, 'click', function(e) { console.log("Polygon click!"); }); //This is never evoked
Microsoft.Maps.Events.addHandler(_map, 'click', function(e) { var point = new MM.Point(e.getX(), e.getY()); var loc = e.target.tryPixelToLocation(point); console.log("lat: " + loc.latitude + ", lon: " + loc.longitude); });
} catch(e) {
alert("Error");
}
_map.setView( {zoom:10});
_map.entities.push(polygon);
if (Microsoft.Maps.Events.hasHandler(polygon,'click')) {
console.log("Polygon has click handler."); //This works
}
//This function should be added to the click handler for polygon. I'll add it when I know the handler works.
function DisplayInfo (e) {
console.log("Polygon has been clicked.");
var vertices = e.target.getLocations();
var verticeCenter = new Microsoft.Maps.Location(0,0);
for (i=0; i<vertices.length-1; i++) {
verticeCenter.latitude = verticeCenter.latitude + vertices[i].latitude;
verticeCenter.longitude = verticeCenter.longitude + vertices[i].longitude;
}
verticeCenter.latitude = verticeCenter.latitude / (vertices.length - 1);
verticeCenter.longitude = verticeCenter.longitude / (vertices.length - 1);
defaultInfobox = new Microsoft.Maps.Infobox(verticeCenter, { width: 200, height: 50 });
_map.entities.push(defaultInfobox);
}
}
After much testing, I found the issue lies in Android. I tried the code on Bing Maps Interactive SDK for AjaxV7 and these are my results:
Desktop browsers: works (as written in the question)
iPhone 4S: works
Android 2.3.4 with default browser: does not work
Android 2.3.4 with Opera: works
Android ICS with Opera: works
Android ICS with default browser: does not work

Categories

Resources