Show Direction Between two location in Android - android

I am developing application to track user location. In that I need to know how to show shortest direction from one location to another in Map Activity.I should not be a straight line. It should be like a road path.

If you want to draw a polyline between 2 points and following road, you can try with the library Google-Directions-Android
You can add the library on gradle with compile 'com.github.jd-alexander:library:1.0.7'
You can use all your point (Latlng) and use them into the waypoint method.
Routing routing = new Routing.Builder()
.travelMode(/* Travel Mode */)
.withListener(/* Listener that delivers routing results.*/)
.waypoints(/*waypoints*/)
.build();
routing.execute();
actual code
start = new LatLng(18.015365, -77.499382);
waypoint= new LatLng(18.01455, -77.499333);
end = new LatLng(18.012590, -77.500659);
Routing routing = new Routing.Builder()
.travelMode(Routing.TravelMode.WALKING)
.withListener(this)
.waypoints(start, waypoint, end)
.build();
routing.execute();

String uri = String.format(Locale.ENGLISH, "http://maps.google.com/maps?saddr=%s&daddr=%s", "Malakwal", "Lahore");
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
intent.setPackage("com.google.android.apps.maps");
startActivity(intent);

Related

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);

Google Maps GroundOverlay Display

I am trying to use groundoverlay on Google Maps just to see if I can get an image on Google Maps. I have no error and looking through the documentation and examples. The image does not display.
Here is part of my code where I am implementing groundoverlay:
private GroundOverlay mGroundOverlay;
private static final LatLng NEWARK = new LatLng(40.714086, -74.228697);
.....
Under
protected void onCreate(Bundle savedInstanceState) {
.....
public void setImage(){
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(NEWARK, 11));
mGroundOverlay = googleMap.addGroundOverlay(new GroundOverlayOptions()
.image(BitmapDescriptorFactory.fromResource(R.drawable.battery)).anchor(0, 1)
.position(NEWARK , 85000f,6500f));
}
I have the image "battery.jpeg" in my resource folder, drawable-hpdi
Can some look at my code and tell me what I am missing? Since I am a beginner in Android I would like an explanation as well.
Maybe I'm reading your intention incorrectly, but if you are trying to use the Android Maps API, you'll have to get a key from the API dashboard; the irreplaceable vogella has a tutorial:
http://www.vogella.com/tutorials/AndroidGoogleMaps/article.html
if you are a new droidgrammer, I can think of no better programmer to learn from.
also, could you post your logcat? FYI: you can use Logcat Extreme on a rooted device to get live logging overlaid on your device that you can also send to file... it's the bee's knees and pouring over logcat readouts will accelerate your education greatly. =]
gl hf
Check this one
LatLng c1 = new LatLng( -33.67032801,151.30209310);
LatLng c2 = new LatLng( -33.66687426,151.30269820);
LatLng c3 = new LatLng( -33.66657936,151.30026840);
LatLng c4 = new LatLng( -33.67003309,151.29966320);
LatLngBounds latLngBounds = new LatLngBounds(c1, c3).including(c2).including(c4);
GroundOverlayOptions groundOverlayOptions = new
GroundOverlayOptions();
BitmapDescriptor bitmapDescriptor =
BitmapDescriptorFactory.fromAsset("h1.png");
groundOverlayOptions.image(bitmapDescriptor);
groundOverlayOptions.positionFromBounds(latLngBounds);
map.addGroundOverlay(groundOverlayOptions);

Tool to draw polygon on google map by passing coordinates

I am doing android app for field drawing and now I am making share function. Is there are any way to pass field coordinates from mobile to maps.google.com, make link, and send this link for example to friend via sms or email, that he just need to push the link and my drawed field will show up to him in browser? Is there any tool to do this or I need to create some web app for this?
Try out it
// To draw boundray on map
private void drawPolygon() {
polygonOptions = new PolygonOptions(); // consider new options
setStrokeWidth();
for (LatLng latLng : drawCoordinates) {
Marker marker = googleMap.addMarker(new MarkerOptions().position(latLng).title(latLng.toString()));
marker.setVisible(false);
polygonOptions.add(marker.getPosition()).strokeColor(Color.RED);
polygonOptions.fillColor(Color.TRANSPARENT);
polygonOptions.visible(true);
}
List<LatLng> points = polygonOptions.getPoints();
if (!points.isEmpty()) {
polygon = googleMap.addPolygon(polygonOptions);
Log.i("Poly lines","Successfully added polyline on map");
}
}

Connect GeoPoints Android

I have an Android application and I'm trying to populate a Google Map with a route, this route is store on a JSON array this way:
JSONArray.getString("points") = ["-33.45591917507404, -70.59198361376951","-33.453484420618416, -70.61635952929686"]
So in this case I have
Point A=(-33.49088437162095, -70.64043194102163) and
Point B=(-33.49423964397045, -70.63992768572683)
And my route or path is A-----B
I'm new to Android and I was wondering how to get this to work. Also, in this case my route is A-----B, but it can also be A----B----C----D. I can have any number of points on the path.
One thing you can try is the Google Maps Polyline object: https://developers.google.com/maps/documentation/javascript/reference#Polyline You specify to setPath() an ordered list of the points (either an Array of LatLng or an MVCArray or LatLng), you want to connect together on the map, and Google Maps will make a polyline for you to your specifications.
// path = Array of LatLng or MVCArray of LatLng
routeLine = new Polyline();
routeLine.setPath(JSONArray);
In your case, passing JSONArray into setPath should work OK.
If you want to get fancy and incorporate directions, you need to use the Google Directions API. https://developers.google.com/maps/documentation/javascript/directions
// All waypoints must be stopovers for the Directions service to optimize their route.
// start & end are of type String or LatLng, waypts is an array of String or LatLng
request = {
origin: start,
destination: end,
waypoints: waypts,
optimizeWaypoints: [type Boolean],
travelMode: [type TravelMode]
};
// go on and actually perform the request
directionsService.route(request, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
// if successful, this will run
}
});
Once you finish constructing your object, you should be able to display it by running setMap(map:Map), such as
routeLine.setMap(map)

Categories

Resources