start navigating to position which is stored in database - android

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

Related

Shared preferences from an intent

I am trying to get the value of my intent saved with shared preferences but struggling. i cant get any values saved. can somebody advise.
I need to take the value from the indent which is launched from the onActivityResult
I could even accept the CharSequence as a string. I need the intent value saved
#Override
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
if (requestCode == PLACE_PICKER_REQUEST
&& resultCode == Activity.RESULT_OK) {
// The user has selected a place. Extract the name and address.
Place place = PlacePicker.getPlace(data, this);
LatLng placeLatLng = place.getLatLng(); // gett lat lng from place
double placeLat = placeLatLng.latitude;
double placeLong = placeLatLng.longitude;
final CharSequence name = place.getName();
final CharSequence address = place.getAddress();
final LatLng location = place.getLatLng();
Marker destination = mMap.addMarker(new MarkerOptions().position(new LatLng(placeLat, placeLong)).title("This is your destination"));
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//Current Location
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location myLocation = locationManager.getLastKnownLocation(provider);
//Current Location LatLong
final double currentLat = myLocation.getLatitude();
final double currentLng = myLocation.getLongitude();
//Directions From Current Location To Destination
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?" + "saddr=" + currentLat + "," + currentLng + "&daddr=" + placeLat + "," + placeLong));
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
//historyDBHandler.addHistory(history);
startActivity(intent);
}
}
private String getInfo(){
SharedPreferences sharedPreferences = getSharedPreferences("Place Details", Context.MODE_PRIVATE);
String mapInfo = sharedPreferences.getString("map Info", "no data recorded");
return mapInfo;
}
private void saveValues(CharSequence name, CharSequence address){
Double text = Double.valueOf(new String().toString());
SharedPreferences sharedPreferences = getSharedPreferences("Place Details", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("map info", "help" );
editor.commit();
}
You're putting with the key "map info" but getting with the key "map Info".

Options to save data from a map marker to be used in a different activity android studio

I am looking advice on options i have to save data from markers and searches from placepicker on google maps.
Ive looked into shared preferences and tried the code out but not working.
I need to take address data from a place picker search and save it so that a history activity can generate a list.
is there any option that place picker saves searches?
im using android studio
This is an example of my place picker
Place place = PlacePicker.getPlace(data, this);
LatLng placeLatLng = place.getLatLng(); // gett lat lng from place
double placeLat = placeLatLng.latitude;
double placeLong = placeLatLng.longitude;
final CharSequence name = place.getName();
final CharSequence address = place.getAddress();
Marker destination = mMap.addMarker(new MarkerOptions().position(new LatLng(placeLat, placeLong)).title("This is your destination"));
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//Current Location
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location myLocation = locationManager.getLastKnownLocation(provider);
//Current Location LatLong
final double currentLat = myLocation.getLatitude();
final double currentLng = myLocation.getLongitude();
List<CharSequence> listItems = new ArrayList<>();
//Directions From Current Location To Destination
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?" + "saddr=" + currentLat + "," + currentLng + "&daddr=" + placeLat + "," + placeLong));
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
listItems.add(name);
listItems.add(address);
startActivity(intent);
}
}
public void saveInfo(View v){
SharedPreferences sharedPreferences = getSharedPreferences("Place Deatils", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
}
get location address using Geocoder and store the information in SharedPreferences
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
try {
addresses = geocoder.getFromLocation(latt,lang, 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5
String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
String area = addresses.get(0).getSubLocality();
String dist= addresses.get(0).getSubAdminArea();
String city = addresses.get(0).getLocality();
mapMarker.setTitle(area+","+dist+","+city);
} catch (IOException e) {
e.printStackTrace();
}
check this SharedPreferences

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.

getLastKnownLocation can't get new data by location.setLatitude/setLongitude?

My full code
https://gist.github.com/anonymous/6dd0f33270cc4f46149e
In line 110~131 I want to change the location, it does work, the mapview will change
private class MapClickedListener implements OnClickListener {
#Override
public void onClick (View v) {
String lng = "121.558561";
lng = edt_lng.getText().toString().trim();
String lat = "25.031005";
lat = edt_lat.getText().toString().trim();
if(lng.equals("")||lat.equals("")){
Toast.makeText(getApplicationContext(), "input again!", Toast.LENGTH_LONG).show();
location = locManager.getLastKnownLocation(bestProvider);
updateToNewLocation(location);
}else{
Location location = new Location(LocationManager.NETWORK_PROVIDER);
location.setLongitude(Double.parseDouble(lng));
location.setLatitude(Double.parseDouble(lat));
updateToNewLocation(location);
}
}
}
On line 133~148, I want to know distance between location and a point I set
private Button.OnClickListener EQ = new Button.OnClickListener(){
#Override
public void onClick (View v) {
eqlocation.setLongitude(120.82);
eqlocation.setLatitude(23.85);
location = locManager.getLastKnownLocation(bestProvider);
float[] result = new float[5];
Location.distanceBetween(location.getLatitude(), location.getLongitude(), eqlocation.getLatitude(), eqlocation.getLongitude(), result);
BigDecimal bd = new BigDecimal(result[0]);
BigDecimal rounded = bd.setScale(2, RoundingMode.HALF_UP);
double dis = rounded.doubleValue();
String dist = String.valueOf(dis/1000);
Toast.makeText(getApplicationContext(), "distance: " + dist + "km", Toast.LENGTH_LONG).show();
}
};
But the result is wrong when I change the location by line 110~131's code.
What should I do to get the right result?
have you tried distanceTo() and checked if you get any better results?, I ususally use distanceTo() with good results.

send variable latitude longitude to google map direction?

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

Categories

Resources