I am trying to create an app that will launch a Google Maps Intent that will show the route to point A, passing through points B and C. I have the addresses and the LatLng of theses points.
Until now I tried to use just make a route from one pont to another using what is in this answer but it didn't work. When the Google Maps app opens it says that no route was found. The origin and destination fields are filled with the latitude and longitude of my points.
What am I doing wrong? Is there another way of doing this?
EDIT: Code I'm using to start the intent:
double ori_latitude = -90.0000000;
double ori_longitude = -60.0000000;
double dest_latitude = -90.0000000;
double dest_longitude = -54.0000000;
String uri = String.format(Locale.ENGLISH, "http://maps.google.com/dir ?saddr=%f,%f&daddr=%f,%f", ori_latitude, ori_longitude, dest_latitude, dest_longitude);
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri));
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);
Just a quick glance says your code would work, but your coordinates point nowhere special, so I wouldn't be surprised if no route existed.
From a quick test, this works for me.
Seattle, WA to San Francisco, CA
double[] origin = { 47.605617, -122.332137 };
double[] dest = { 37.774821, -122.419462 };
Uri.Builder builder = new Uri.Builder()
.scheme("http")
.authority("maps.google.com/maps")
.appendQueryParameter("saddr", String.format(Locale.ENGLISH, "%f,%f", origin[0], origin[1]))
.appendQueryParameter("daddr", String.format(Locale.ENGLISH, "%f,%f", dest[0], dest[1]));
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(builder.build().toString()));
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);
Code you have written is correct. There is issue with these coordinates.
Try changing coordinates, and see the magic. It works perfectly.
For eg:
double ori_latitude = 34.052222;
double ori_longitude = -118.243611;
double dest_latitude = 37.322778;
double dest_longitude = -122.031944;
Also, check the rules at
Google Maps
Tips for formatting your coordinates
Here are some tips for formatting your coordinates so they work on Google Maps:
Use the degree symbol instead of "d".
Use periods as decimals, not commas.
Incorrect: 41,40338, 2,17403. Correct: 41.40338, 2.17403.
List your latitude coordinates before longitude coordinates.
Check that the first number in your latitude coordinate is between -90 and 90.
Check that the first number in your longitude coordinate is between -180 and 180.
Related
I'm using MapQuest (since Google Map API will be down in september) and I'd like to add a point at an exact coordinates position.
I've created a movieClip named geoPoint and traced a circle in it.
Now, I've managed to display the coordinates in a text field like that :
if (Geolocation.isSupported){
var my_geo:Geolocation = new Geolocation();
my_geo.addEventListener(GeolocationEvent.UPDATE, onGeoUpdate);
my_geo.setRequestedUpdateInterval(50);
var my_txt:TextField = new TextField();
my_txt.wordWrap=true;
addChild(my_txt);
}
function onGeoUpdate(e:GeolocationEvent):void{
my_txt.text = "My Latitude is "+e.latitude+" and my Longitude is "+e.longitude;
}
Now is it possible to add my movieClip at the e.latitude and e.longitude point ?
I've tried :
var geo:MovieClip;
geo = new geoPoint;
addChild(geo(new LatLng(e.latitude, e.longitude)));
but it doesn't work.
Someone knows how can I do it ?
Thanks !
It should be
geo = new geoPoint();
You should also probably figure how to translate latitude and longitude to x and y coordinates, because I'm doubtful the next line will work. You're calling a MovieClip constructor that takes a LatLng argument.
For what you need to achive, you may have to use a class provided by the MapQuest Api, like PolygonOverlay or CircleOverlay. There is an example here under "Shape Collections" http://developer.mapquest.com/content/as3/v7/7.0.6_MQ/samples/samplesexplorer/index.html#
Is there a working way (i heard that as of update 7.0 there were some changes) to display a marker with a position given by latitude/longitude with a label?
geo:?q=latitude, longitude (Label) does not work...
Show point on google map via an intent:
String uri = "geo:"+ latitude + "," + longitude;
startActivity(new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri)));
You can also choose to place a point like so:
String uri = "geo:"+ latitude + "," + longitude + "?q=my+street+address";
startActivity(new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri)));
The Possible Query params options are the following:
Show map at location: geo:latitude,longitude
Show zoomed map at location: geo:latitude,longitude?z=zoom
Show map at location with point: geo:0,0?q=my+street+address
Show map of businesses in area: geo:0,0?q=business+near+city
I am trying to implement a functionality such that when a user enters an address in a text field and clicks on a button, that address should be copied into the destination field of Google maps and the user location should be set to "My Location" (his current location).
You can open google map in following way:
String CURRENT_LOCATION = "current_location_latitude, current_location_longitude";
String DESTINATION_LOCATION = "address_from_your_text_view";
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?saddr="+ CURRENT_LOCATION +" daddr="+DESTINATION_LOCATION));
startActivity(intent);
Note: You should get device/user current location from LocationManager
In order to show route on Google Map, Just call an intent which passes current and destination latitude and longitude. You may also pass address in any case if don't know lat-long. After doing this, its Google Map's job to show location. You may also show street view.
In below code, there are three parameters : current_lat, current_longi, dest_address
final Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?"
+ "saddr="+ current_lat+","+current_longi + "&daddr="+dest_address ));
intent.setClassName("com.google.android.apps.maps","com.google.android.maps.MapsActivity");
startActivity(intent);
If you have current address and destination address, then you can write like this :
Uri.parse("http://maps.google.com/maps?"
+ "saddr="+curr_address+ "&daddr="+dest_address ));
If you have current and destination latitude and longitude both then you can write like this :
Uri.parse("http://maps.google.com/maps?"
+ "saddr="+ current_lat+","+current_longi + "&daddr="+ destt_lat+","+dest_longi ));
When you call this intent, Google Map shows option whether to draw route by bus or by walk.
I have given coordinates for latitude and longitude, and I want make a location object with those.There isn't a constructor that takes to doubles to make a location, so I tried like this:
Location l = null;
l.setLatitude(lat);
l.setLongitude(lon);
but it crashes. Any different idea?
Just create a new Location with new Location("reverseGeocoded");
like this
GeoPoint newCurrent = new GeoPoint(59529200, 18071400);
Location current = new Location("reverseGeocoded");
current.setLatitude(newCurrent.getLatitudeE6() / 1e6);
current.setLongitude(newCurrent.getLongitudeE6() / 1e6);
current.setAccuracy(3333);
current.setBearing(333);
This crashes because you cannot call methods on an non existent object.
I presume you are talking about android.location.Location?
This is usually returned by the various positioning services of Android.
What do you want to do with it?
Do you want to reverse geocode it? As in find an address for that geo coordinate?
Or do you want to use it as a "fake" position and feed it to other applications?
There are BTW two constructors. One takes the name of a positioning service and the other is a copy constructor and takes an existing Location.
So you could create a Location like this:
Location l = new Location("network");
But I do not think that this will result in something you want to have.
Here is a link to the documentation:
https://developer.android.com/reference/android/location/Location.html#Location%28java.lang.String%29
Location l = new Location("any string");
l.setLatitude(lat);
l.setLongitude(lon);
Try this:
public double lat;
public double lon;
public void onLocationChanged(Location loc)
{
lat=loc.getLatitude();
lon=loc.getLongitude();
Toast.makeText(getBaseContext(),"Latitude:" + lat +Longitude"+lon,Toast.LENGTH_LONG).show();
}
This is maybe a noob question but im not 100% sure about it.
How can i make a Location Object using Geo points? I want to use it to get the distance between two points.
I already found a thread where it says
Location loc1 = new Location("Location");
loc.setLatitude(geoPoint.getLatitudeE6);
loc.setLongitude(geoPoint.getLongitudeE6);
Location loc2 = new Location("Location2");
loc2.setLatitude(geoPoint.getLatitudeE6);
loc2.setLongitude(geoPoint.getLongitudeE6);
and then i would use the distanceTo() to get the distance between the two points.
My Questions
What is the Providername for? ...new Location("What is this here???")
So do i have to define a Provider before or something?
I want to use this code in a for() to calaculate between more GeoPoints.
And btw - i have to convert the E6 Values back to normal?
Not exactly
loc.setLatitude() takes a double latitude. So the correct code is:
loc.setLatitude( geoPoint.getLatitudeE6() / 1E6);
Location() constructor take the name of the GPS provider used. It can be LocationManager.GPS_PROVIDER or NETWORK_PROVIDER among other values
To get the distance between two point you can use the Location class and more precisely the distanceBetween static method.
The doc is quite clear on what it does but here a quick code sample:
float[] results = new float[3];
Location.distanceBetween(destLatitude, destLongitude, mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude(), results);
// result in meters, convert it in km
String distance = String.valueOf(Math.round(results[0] / 1000)) + " km");
To convert from minute/second to degree you can use the convert method of the Location class.
Log.i("MapView", "Map distance to mark (in meters): " + myLocation.distanceTo(GeoToLocation(point)) + "m");
then
public Location GeoToLocation(GeoPoint gp) {
Location location = new Location("dummyProvider");
location.setLatitude(gp.getLatitudeE6()/1E6);
location.setLongitude(gp.getLongitudeE6()/1E6);
return location;
}