send variable latitude longitude to google map direction? - android

i want to passing variable latitude longitude to google map direction?
can help me see the error or have a new solution?
this is my java code :
public void onClickShowMap(View v) {
String latitude = ((TextView) findViewById(R.id.latitude)).getText().toString();
String longitude = ((TextView) findViewById(R.id.longitude)).getText().toString();
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?f=d&daddr="+latitude+","+longitude));
startActivity(intent);
}

Use this code,
final Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?" + "saddr=" + lat
+ "," + lon + "&daddr=" + lattitude + ","
+longitude));
intent.setClassName("com.google.android.apps.maps",
"com.google.android.maps.MapsActivity");
((Activity) this).startActivity(intent);

Related

How can I put a name to a location

I have this code to show my location to another location but it's not the issue, how can I add a name for it?
locationname.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
String DESTINATION_LOCATION = "37.967775, 23.720689";
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?saddr="
+ "&daddr=" + DESTINATION_LOCATION));
startActivity(intent);
}
});
I have this:
String DESTINATION_LOCATION = "My location name";
But it doesn't fit in the code!
Does anybody know?
Thanks
This will open with default map and adds marker with name
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:<lat>,<long>?q=<lat>,<long>(Label+Name)"));
startActivity(intent);
Option 2
String urlAddress = "http://maps.google.com/maps?q="+ myLatitude +"," + myLongitude +"("+ labelLocation + ")&iwloc=A&hl=es";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlAddress);
startActivity(intent);

update map dircetions in android after specific time?

I am working on an android map app.I am getting direction from latitude and longitude.
But the problem is this i want to update map directions after some time or after location change.
My code is below can any one help me in this.
Button.OnClickListener addressclick = new Button.OnClickListener(){
#Override
public void onClick(View v) {
/*TextView tv = (TextView)v;
String latitude ="0";
String longitude = "0";
String label = "Location";
String uriBegin = "geo:" + latitude + "," + longitude;
String query = tv.getText().toString() + "(" + label + ")";
String encodedQuery = Uri.encode(query);
String uriString = uriBegin + "?q=" + encodedQuery+"&z=10";
Uri uri = Uri.parse(uriString);
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, uri);
startActivity(intent);*/
TextView tv = (TextView)v;
String addressStr = tv.getText().toString();
Geocoder geoCoder = new Geocoder(context);
try {
List<Address> addresses =
geoCoder.getFromLocationName(addressStr, 1);
if (addresses.size() > 0) {
latitude = addresses.get(0).getLatitude();
longitude =addresses.get(0).getLongitude(); }
} catch (IOException e) { // TODO Auto-generated catch block
e.printStackTrace();
}
String directionweburl = "http://maps.google.com/maps?daddr="+Double.toString(latitude)+","+Double.toString(longitude)+"&saddr="+Double.toString(currentlat)+","+Double.toString(currentlong);
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(directionweburl));
myIntent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(myIntent);
}
};
what am i missing here?anyone can tell?
I think you should start a service and put regular check in it. After some time, you can update map directions or check current location in order to update location info.

start navigating to position which is stored in database

I have an application where I am getting the latitude and longitude coordinates.
I want , when to press a button to start navigating to that position .
Now , I am storing latitude and longitude in database.
So , I want to extract the location first.When I press the button to navigate I open an alertdialog and in 'YES' I do:
public void navigation(final View v){
AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
alt_bld.setMessage("Do you want to navigate to the saved position?")
.setCancelable(false)
.setPositiveButton("Navigate",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// Action for 'Yes' Button
String query = "SELECT latitude,longitude FROM MEMORIES ";
Cursor c1 = sqlHandler.selectQuery(query);
if (c1 != null && c1.getCount() != 0) {
if (c1.moveToFirst()) {
do {
String mylatitude=c1.getString(c1.getColumnIndex("latitude"));
String mylongitude=c1.getString(c1.getColumnIndex("longitude"));
Double lat=Double.parseDouble(mylatitude);
Double lon=Double.parseDouble(mylongitude);
} while (c1.moveToNext());
}
}
c1.close();
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?saddr=" +
gps.getLocation().getLatitude() + "," +
gps.getLocation().getLongitude() + "&daddr=" + mylatitude + "," + mylongitude ));
startActivity(intent);
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// Action for 'NO' Button
dialog.cancel();
}
});
AlertDialog alert = alt_bld.create();
// Title for AlertDialog
alert.setTitle("Navigation");
alert.show();
}
I have the location stored (lat and lon) and i want to start navigating to that position.
How can I do that?
Thanks!
---------------------UPDATE-------------------------------------
If I do sth like:
String mylat="";
String mylon="";
#Override
protected void onCreate(Bundle savedInstanceState) {
...
...
public void navigation(final View v){
....
String mylatitude=c1.getString(c1.getColumnIndex("latitude"));
String mylongitude=c1.getString(c1.getColumnIndex("longitude"));
mylat=mylatitude;//stored coordinates from database
mylon=mylongitude;
String f="45.08" //current location (start points)
String s="23.3";
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?saddr=" +
f + "," +
s + "&daddr=" + mylat + "," + mylon ));
startActivity(intent);
The intent starts and I am in the map application where the start points are the "f" and "s" I defined above but the destination points are 0.0 , 0.0 ;
So , I have 2 problems:
1) How to put to destination points my stored locations (mylatitude ,mylongitude (which I copy to mylat,mylon)
2) How to get current location (initial points) because my gps class doesn't work on that.
In the example below ImplLocationService is a class/service within my application that is providing the latitude and longitude coordinates of the current location within the device. The Location class is similar to Android's offering but is slightly different and is providing the destination latitude and longitude coordinates. If you were to pull these values from a database, the approach below is the same.
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?saddr=" +
ImplLocationService.getCurrentLocation().getLatitude() + "," +
ImplLocationService.getCurrentLocation().getLongitude() + "&daddr=" +
location.getPoint().latitude + "," + location.getPoint().longitude));
startActivity(intent);
While storing the lat and long store it in this way...
String mylocation=latitude + ":" + longitude;
And while retriving it
String latitude = mylocation.subString(0, myLocation.indexOf(":") - 1 );
String longitude = mylocation.subString(myLocation.indexOf(":"));
and pass it as
j.putExtra("lon", longitude);
j.putExtra("lat", latitude);
one possible way is to store your latitude and longitude values in shared preferences.
-To store values you can use
SharedPreferences sp = getSharedPreferences("MyPrefs", MODE_PRIVATE);
Editor editor = sp.edit();
editor.putString("mytext", text);
editor.commit();
-and to retrive those values you can use
String value = prefs.getString("MyPrefs, "mytext");
in your code your are storing latitude and longitudes in double do not forget to convert them in toString()...
hope this helps

Get location name from fetched coordinates

What i have: currently my app is only telling me the coordinates of my current location.
What i want: Get location name from coordinates fetched by gps, so that i could know where exactly i am. (Name of location)
Here is complete code from fetching long - lat to getting address:
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
String provider = locationManager.getBestProvider(new Criteria(), true);
Location locations = locationManager.getLastKnownLocation(provider);
List<String> providerList = locationManager.getAllProviders();
if(null!=locations && null!=providerList && providerList.size()>0){
double longitude = locations.getLongitude();
double latitude = locations.getLatitude();
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
try {
List<Address> listAddresses = geocoder.getFromLocation(latitude, longitude, 1);
if(null!=listAddresses&&listAddresses.size()>0){
String _Location = listAddresses.get(0).getAddressLine(0);
}
} catch (IOException e) {
e.printStackTrace();
}
}
You can us the GeoCoder which is available in android.location.Geocoder package. The JavaDocs gives u full explaination. The possible sample for u.
List<Address> list = geoCoder.getFromLocation(location
.getLatitude(), location.getLongitude(), 1);
if (list != null & list.size() > 0) {
Address address = list.get(0);
result = address.getLocality();
return result;
The result will return the name of the location.
Here i am given a single just pass the latitude and longitude in this function then you got all the information related to this latitude and longitude.
public void getAddress(double lat, double lng) {
Geocoder geocoder = new Geocoder(HomeActivity.mContext, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
Address obj = addresses.get(0);
String add = obj.getAddressLine(0);
GUIStatics.currentAddress = obj.getSubAdminArea() + ","
+ obj.getAdminArea();
GUIStatics.latitude = obj.getLatitude();
GUIStatics.longitude = obj.getLongitude();
GUIStatics.currentCity= obj.getSubAdminArea();
GUIStatics.currentState= obj.getAdminArea();
add = add + "\n" + obj.getCountryName();
add = add + "\n" + obj.getCountryCode();
add = add + "\n" + obj.getAdminArea();
add = add + "\n" + obj.getPostalCode();
add = add + "\n" + obj.getSubAdminArea();
add = add + "\n" + obj.getLocality();
add = add + "\n" + obj.getSubThoroughfare();
Log.v("IGA", "Address" + add);
// Toast.makeText(this, "Address=>" + add,
// Toast.LENGTH_SHORT).show();
// TennisAppActivity.showDialog(add);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
I hope you get the solution to your answer.

Unable to get city from lat,long values?

Geocoder g = new Geocoder(this, Locale.getDefault());
java.util.List<android.location.Address> result = null;
// testing
try{
result = g.getFromLocation(43.324722,21.903333, 1);
if (result.size() > 0) {
selectedCity = result.get(0).getLocality();
}else
//no city found
}catch(Exception ex)
{
}
i am using above code to get city value in android but it always show no city found any one guide me what could be the problem?
public void getAddress(double lat, double lng) {
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
Address obj = addresses.get(0);
String add = obj.getAddressLine(0);
GUIStatics.currentAddress = obj.getSubAdminArea() + ","
+ obj.getAdminArea();
GUIStatics.latitude = obj.getLatitude();
GUIStatics.longitude = obj.getLongitude();
GUIStatics.currentCity= obj.getSubAdminArea();
GUIStatics.currentState= obj.getAdminArea();
add = add + "\n" + obj.getCountryName();
add = add + "\n" + obj.getCountryCode();
add = add + "\n" + obj.getAdminArea();
add = add + "\n" + obj.getPostalCode();
add = add + "\n" + obj.getSubAdminArea();
add = add + "\n" + obj.getLocality();
add = add + "\n" + obj.getSubThoroughfare();
Log.v("IGA", "Address" + add);
// Toast.makeText(this, "Address=>" + add,
// Toast.LENGTH_SHORT).show();
// TennisAppActivity.showDialog(add);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
I am using this function for getting all the information related to latitude and longitude just pass the latitude and longitude in this function then you find your answer.
I hope this is very help full to you.

Categories

Resources