Scenario:
So what I have done so far is Created an AsyncTask to handle my GeoCoder that updates every 3 min (for testing purposes). Then I set up a TimerTask that shows a toast message with the users current Address every 4 minutes. (TimerTasks not included in code)
So heres the problem:
When I am in my app, everything is fine, however when my app is running in the background, the Toast messages stay stuck at whatever address the app was last set at before I exited my app. I know for sure that the AsyncTask does run in the background (Checked LogCats) and it seems like everything is running in the background fine, I just cant display the current address on my Toast.
All thoughts and inputs will be appreciated!
Here is my code:
public class statuspage extends MapActivity {
LocationManager locationManager;
MapView mapView;
Criteria criteria;
Location location;
Geocoder gc;
Address address;
String bestProvider;
String LOCATION_SERVICE = "location";
String addressString = "Searching for Nearest Address";
StringBuilder sb;
private MapController mapController;
private MyLocationOverlay myLocation;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.statuspage);
// Get Mapping Controllers etc //
mapView = (MapView) findViewById(R.id.mapView);
mapController = mapView.getController();
mapController.setZoom(17);
mapView.setBuiltInZoomControls(true);
// Add the MyLocationOverlay //
myLocation = new MyLocationOverlay(this, mapView);
mapView.getOverlays().add(myLocation);
myLocation.enableCompass();
myLocation.enableMyLocation();
// Animates the map to GPS Position //
myLocation.runOnFirstFix(new Runnable() {
#Override
public void run() {
mapController.animateTo(myLocation.getMyLocation());
}
});
}
#Override
protected boolean isRouteDisplayed() {
// Location Manager Intiation
locationManager = (LocationManager) statuspage.this
.getSystemService(LOCATION_SERVICE);
criteria = new Criteria();
// More accurate, GPS fix.
criteria.setAccuracy(Criteria.ACCURACY_FINE); // More accurate, GPS fix.
bestProvider = locationManager.getBestProvider(criteria, true);
location = locationManager.getLastKnownLocation(bestProvider);
updateWithNewLocation(location);
locationManager.requestLocationUpdates(bestProvider, 60000, 10,
locationListener); // 1800000 = 30 Min
return false;
}
class GeoCoder extends AsyncTask<Void, Void, Void> {
String lat = "Acquiring";
String lng = "Acquiring";
#Override
protected Void doInBackground(Void... params) {
if (location != null) {
/**
* double latitude = myLocation.getMyLocation().getLatitudeE6();
* double longitude =
* myLocation.getMyLocation().getLongitudeE6();
*/
double latitude = location.getLatitude();
double longitude = location.getLongitude();
lat = "" + latitude;
lng = "" + longitude;
// gc = new Geocoder(statuspage.this, Locale.getDefault());
Geocoder gc = new Geocoder(getApplicationContext(),
Locale.getDefault());
try {
List<Address> addresses = gc.getFromLocation(latitude,
longitude, 1);
sb = new StringBuilder();
if (addresses != null && addresses.size() > 0) {
address = addresses.get(0);
int noOfMaxAddressLine = address
.getMaxAddressLineIndex();
if (noOfMaxAddressLine > 0) {
for (int i = 0; i < address
.getMaxAddressLineIndex(); i++) {
sb.append(address.getAddressLine(i)).append(
"\n");
}
addressString = sb.toString();
}
}
} catch (Exception e) {
addressString = "Sorry, we are trying to find information about this location";
}
}
return null;
}
#Override
protected void onPostExecute(Void result) {
TextView scrollview = (TextView) findViewById(R.id.scrollview);
// Latitude and Longitude TextView
TextView etlongitude = (TextView) findViewById(R.id.etlongitude);
TextView etlatitude = (TextView) findViewById(R.id.etlatitude);
// TextView to display GeoCoder Address
scrollview.setGravity(Gravity.CENTER);
scrollview.setText("Your location:" + "\n"
+ "(Accurate to 500 meters)" + "\n" + (addressString));
Log.d("Address", (addressString));
// Latitude and Longitude TextView Display Coordinates //
etlongitude.setText(lng);
etlatitude.setText(lat);
// Log.d("GeoCoder", "In-Task");
return;
}
When you are using an assync task you cant update the UI in background.Its impossible to connect the UI from inside thread thats from background.The only way to connect the UI is to use the onPostExecute().And use this onPostExecute() function to update the UI.try sending messages from the background and do the UI stuff on the postexecute by checking the messages.This will help you for sure.
I've got the same problem. If I stay on the current activity, no problem, but if I leave the activity while doInBackgroud is running, onPostExecute will exit on the Toast line.
To resolve the problem, you have to use a handler :
In the class
private static final int TOAST = 0;
private Handler mHandler = null;
In OnCreate()
// Creation of the handler to display Toasts
if (mHandler == null) {
mHandler = new Handler() {
#Override
public void handleMessage(Message _msg) {
switch (_msg.what) {
case TOAST:
Toast.makeText(ServerTabHost.this, (String)_msg.obj, Toast.LENGTH_LONG).show();
break;
default : break;
}
super.handleMessage(_msg);
}
};
}
In onPostExecute()
Message msg = new Message();
msg.what = TOAST;
msg.obj = "my toast message";
mHandler.sendMessage(msg);
In your code it looks like this :
public class statuspage extends MapActivity {
// These two lines are for the handler
private static final int TOAST = 0;
private Handler mHandler = null;
LocationManager locationManager;
MapView mapView;
Criteria criteria;
Location location;
Geocoder gc;
Address address;
String bestProvider;
String LOCATION_SERVICE = "location";
String addressString = "Searching for Nearest Address";
StringBuilder sb;
private MapController mapController;
private MyLocationOverlay myLocation;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.statuspage);
// Get Mapping Controllers etc //
mapView = (MapView) findViewById(R.id.mapView);
mapController = mapView.getController();
mapController.setZoom(17);
mapView.setBuiltInZoomControls(true);
// Add the MyLocationOverlay //
myLocation = new MyLocationOverlay(this, mapView);
mapView.getOverlays().add(myLocation);
myLocation.enableCompass();
myLocation.enableMyLocation();
// Animates the map to GPS Position //
myLocation.runOnFirstFix(new Runnable() {
#Override
public void run() {
mapController.animateTo(myLocation.getMyLocation());
}
});
// Creation of the handler to display Toasts
if (mHandler == null) {
mHandler = new Handler() {
#Override
public void handleMessage(Message _msg) {
switch (_msg.what) {
case TOAST:
Toast.makeText(ServerTabHost.this, (String)_msg.obj, Toast.LENGTH_LONG).show();
break;
default : break;
}
super.handleMessage(_msg);
}
};
}
}
#Override
protected boolean isRouteDisplayed() {
// Location Manager Intiation
locationManager = (LocationManager) statuspage.this
.getSystemService(LOCATION_SERVICE);
criteria = new Criteria();
// More accurate, GPS fix.
criteria.setAccuracy(Criteria.ACCURACY_FINE); // More accurate, GPS fix.
bestProvider = locationManager.getBestProvider(criteria, true);
location = locationManager.getLastKnownLocation(bestProvider);
updateWithNewLocation(location);
locationManager.requestLocationUpdates(bestProvider, 60000, 10,
locationListener); // 1800000 = 30 Min
return false;
}
class GeoCoder extends AsyncTask<Void, Void, Void> {
String lat = "Acquiring";
String lng = "Acquiring";
#Override
protected Void doInBackground(Void... params) {
if (location != null) {
/**
* double latitude = myLocation.getMyLocation().getLatitudeE6();
* double longitude =
* myLocation.getMyLocation().getLongitudeE6();
*/
double latitude = location.getLatitude();
double longitude = location.getLongitude();
lat = "" + latitude;
lng = "" + longitude;
// gc = new Geocoder(statuspage.this, Locale.getDefault());
Geocoder gc = new Geocoder(getApplicationContext(),
Locale.getDefault());
try {
List<Address> addresses = gc.getFromLocation(latitude,
longitude, 1);
sb = new StringBuilder();
if (addresses != null && addresses.size() > 0) {
address = addresses.get(0);
int noOfMaxAddressLine = address
.getMaxAddressLineIndex();
if (noOfMaxAddressLine > 0) {
for (int i = 0; i < address
.getMaxAddressLineIndex(); i++) {
sb.append(address.getAddressLine(i)).append(
"\n");
}
addressString = sb.toString();
}
}
} catch (Exception e) {
addressString = "Sorry, we are trying to find information about this location";
}
}
return null;
}
#Override
protected void onPostExecute(Void result) {
// Sending the Toast message through the handler
Message msg = new Message();
msg.what = TOAST;
msg.obj = "My toast message";
mHandler.sendMessage(msg);
TextView scrollview = (TextView) findViewById(R.id.scrollview);
// Latitude and Longitude TextView
TextView etlongitude = (TextView) findViewById(R.id.etlongitude);
TextView etlatitude = (TextView) findViewById(R.id.etlatitude);
// TextView to display GeoCoder Address
scrollview.setGravity(Gravity.CENTER);
scrollview.setText("Your location:" + "\n"
+ "(Accurate to 500 meters)" + "\n" + (addressString));
Log.d("Address", (addressString));
// Latitude and Longitude TextView Display Coordinates //
etlongitude.setText(lng);
etlatitude.setText(lat);
// Log.d("GeoCoder", "In-Task");
return;
}
Personally, I was in a Fragment so I had to create the handler in the host activity and then pass it in the fragment constructor.
Related
I am a very novice programmer.
I am using a handler to get my location data from a service to my main activity but I would like to update the map using the code from the service.
The GPS coordinates are correctly sent via the handler, but I seem to be stuck on how to use the handler to actually present the data by updating the map/ icons on the map.
The map keeps putting the marker at 0.0, nor does it position the map at the new location sent via the handler.
//get GPS latitude and Longitude from service here
private void displayLatLong() {
final TextView latitudeView = (TextView) findViewById(R.id.latitude);
final TextView longitudeView = (TextView) findViewById(R.id.longitude);
final TextView latitudeCoordsView = (TextView) findViewById(R.id.latitudecoordsView);
final TextView longitudeCoordsView = (TextView) findViewById(R.id.latitudecoordsView);
final Handler handler = new Handler();
handler.post(new Runnable() {
#Override
public void run() {
double latitude = 0.0;
double longitude = 0.0;
//latitudeCoords = "not empty"; //debug
//longitudeCoords = "not empty"; //debug
if(bound && locationService != null) {
latitude = locationService.getLastLatitude();
longitude = locationService.getlastLongitude();
}
String latitudeStr = String.format(Locale.getDefault(), "%1$, .5f lat", latitude);
String longitutdeStr = String.format(Locale.getDefault(), "%1$, .5f long", longitude);
latitudeView.setText(latitudeStr);
longitudeView.setText(longitutdeStr);
latCoords = latitude;
longCoords = longitude;
// longitudeCoordsView.setText(longitudeCoords); //debug
// latitudeCoordsView.setText(longitudeCoords); //debug
handler.postDelayed(this, 1000);
}
});
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the SupportMapFragment and request notification
// when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
//Bind location service here so it keeps running even if activity is stopped.
Intent intent = new Intent(this, LocationService.class);
bindService(intent, connection, Context.BIND_AUTO_CREATE);
displayDistance();
displayLatLong();
}
#Override
public void onMapReady(GoogleMap map) {
mMap = map;
LatLng player= new LatLng(latCoords, longCoords);
mMap.addMarker(new MarkerOptions().position(player).title("current player position"));//set marker with player position and description
mMap.moveCamera(CameraUpdateFactory.newLatLng(player)); //move Camera to player position
}locat
You just need to update the map once you get the location.
Add a method that will update the map:
private void updateMapLocation() {
LatLng player= new LatLng(latCoords, longCoords);
mMap.addMarker(new MarkerOptions().position(player).title("current player position"));//set marker with player position and description
mMap.moveCamera(CameraUpdateFactory.newLatLng(player)); //move Camera to player position
}
Then call this method once you have the latCoords and longCoords member variables populated:
//get GPS latitude and Longitude from service here
private void displayLatLong() {
final TextView latitudeView = (TextView) findViewById(R.id.latitude);
final TextView longitudeView = (TextView) findViewById(R.id.longitude);
final TextView latitudeCoordsView = (TextView) findViewById(R.id.latitudecoordsView);
final TextView longitudeCoordsView = (TextView) findViewById(R.id.latitudecoordsView);
final Handler handler = new Handler();
handler.post(new Runnable() {
#Override
public void run() {
double latitude = 0.0;
double longitude = 0.0;
if(bound && locationService != null) {
latitude = locationService.getLastLatitude();
longitude = locationService.getlastLongitude();
}
String latitudeStr = String.format(Locale.getDefault(), "%1$, .5f lat", latitude);
String longitutdeStr = String.format(Locale.getDefault(), "%1$, .5f long", longitude);
latitudeView.setText(latitudeStr);
longitudeView.setText(longitutdeStr);
latCoords = latitude;
longCoords = longitude;
// Add call to update map with location:
if (latCoords > 0 && longCoords > 0) {
updateMapLocation()
}
handler.postDelayed(this, 1000);
}
});
}
i need your help regarding the location update in android. Following is my code for getting location update and it is working fine. But it returns invalid message body when i get the stored variable with location in oncreate method of main class. After thorough research it seems that the variable i called in oncreate method is empty. Can you please tell me how to get the address as it appears in onlocationChanged Method. Thank you!
Calling class with oncreate method:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listener = new Mylocation();
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener);
String address1= listener.getAddress();
{
sendSms(phone, message, false);
} catch (Exception e) {
Log.i("Error Is ", e.toString());
}
}
location class:
class Mylocation implements LocationListener{
double lat, lon;
static final String address="";
public void onLocationChanged(Location location)
{
//...
lat = location.getLatitude();
lon = location.getLongitude();
address = GetAddressDetail(lat, lon);
Log.i("Messge is", address); //working here
}
public String getAddress(){ //not returning the address
return address;
}
public String GetAddressDetail(Double lat2, Double lon2)
{
Geocoder geocoder = new Geocoder(MainActivity.this, Locale.ENGLISH);
try {
List<Address> addresses = geocoder.getFromLocation(lat2,lon2, 1);
if(addresses != null) {
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder("Address:");
for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress.append(returnedAddress.getAddressLine(i));
}
ret = strReturnedAddress.toString();
}
else{
ret = "No Address returned!";
}
}
return ret;
}
}
Make sure your variables are initialized properly. I don't see evidence of this in the question so I am just checking.
// Instantiation
Mylocation listener;
String phone;
String message;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialization
listener = new Mylocation(this);
phone = "";
message = "";
// These two lines aren't really necessary,
// this should be in your MyLocation class
//locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
//locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener);
// Add this line, we are going to initialize this class
// and make sure that address gets set
listener = new Mylocation();
String address1 = listener.getAddress();
try {
// If neither phone nor message is empty lets sendSms()
if (!phone.isEmpty() || !message.isEmpty()) {
sendSms(phone, message, false);
}
} catch (Exception e) {
Log.i("Error Is ", e.toString());
}
}
Change the String address to private and in the getter try return this.address;
class Mylocation implements LocationListener {
double lat, lon;
// Let's make this private so it can't be accessed directly,
// since you have the setter and getter.
private String address = "";
// Make sure you are overriding this method
#Override
public void onLocationChanged(Location location) {
/** ... */
lat = location.getLatitude();
lon = location.getLongitude();
address = GetAddressDetail(lat, lon);
Log.i("Messge is", address);
}
public String getAddress(){
return (address.isEmpty()) ? "Address not set" : this.address;
}
public String GetAddressDetail(Double lat2, Double lon2) {
Geocoder geocoder = new Geocoder(MainActivity.this, Locale.ENGLISH);
try {
List<Address> addresses = geocoder.getFromLocation(lat2,lon2, 1);
if(addresses != null) {
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder("Address:");
for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress.append(returnedAddress.getAddressLine(i));
}
ret = strReturnedAddress.toString();
}
else{
ret = "No Address returned!";
}
}
return ret;
}
}
Edit
I made changes to your code in my answer above, check the comments. I am also going to suggest additional methods for your MyLocation class:
class Mylocation implements LocationListener {
protected LocationManager locationManager;
private Context activityContext;
// The initializing method, this fires off first
// when a new instance of the class is created
public MyLocation(Context context) {
this.activityContext = context;
locationManager = (LocationManager) activityContext.getSystemService(LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);) {
locationManager.requestLocationUpdates(
NETWORK_PROVIDER,
MIN_TIME,
MIN_DISTANCE,
this
);
}
getLocation();
}
private double lat;
private double lon;
public double getLat() {
return this.lat;
}
public double getLng() {
return this.lng;
}
public void getLocation() {
if (location == null) {
locationManager.requestLocationUpdates(NETWORK_PROVIDER, MIN_TIME, MIN_DISTANCE, this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(NETWORK_PROVIDER);
if (location != null) {
// Set the coordinate variables
lat = location.getLatitude();
lon = location.getLongitude();
Log.i("Network", "Lat: " + latitude + " / Lng: " + longitude);
}
}
}
}
}
You are calling getAdress directly after you set the locationmanager to probe for a location. The onLocationChanged method probably hasn't been called yet when you call getAddress this way. I would recommend changing it to something like below to make sure it has been called:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listener = new Mylocation();
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,new LocationListener(){
#Override
public void onLocationChanged(Location location) {
//...
long lat = location.getLatitude();
long lon = location.getLongitude();
String address = GetAddressDetail(lat, lon);
//Do whatever you want to do with the address here, maybe add them to the message or something like that.
sendSms(phone, message, false);
}
});
}
public String GetAddressDetail(Double lat2, Double lon2)
{
Geocoder geocoder = new Geocoder(MainActivity.this, Locale.ENGLISH);
try {
List<Address> addresses = geocoder.getFromLocation(lat2,lon2, 1);
if(addresses != null) {
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder("Address:");
for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress.append(returnedAddress.getAddressLine(i));
}
ret = strReturnedAddress.toString();
}
else{
ret = "No Address returned!";
}
}
return ret;
}
I want to get the current location with name. I did coding for get current location (lat,lang), how can I show the relative place name?
(ie) 13.006389 - 80.2575 : Adyar,Chennai,India
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() {
public void onStatusChanged(String provider, int status, Bundle extras) {
// called when the location provider status changes. Possible status: OUT_OF_SERVICE, TEMPORARILY_UNAVAILABLE or AVAILABLE.
}
public void onProviderEnabled(String provider) {
// called when the location provider is enabled by the user
}
public void onProviderDisabled(String provider) {
// called when the location provider is disabled by the user. If it is already disabled, it's called immediately after requestLocationUpdates
}
public void onLocationChanged(Location location) {
double latitute = location.getLatitude();
double longitude = location.getLongitude();
// do whatever you want with the coordinates
}
});
This will convert the lat & lng into String Address and i have set it in the text field for your example. This is done by using the concept of Reverse Geocoding & there is a class called Geocoder in Android.
// Write the location name.
//
try {
Geocoder geo = new Geocoder(this.getApplicationContext(), Locale.getDefault());
List<Address> addresses = geo.getFromLocation(latitude, longitude, 1);
if (addresses.isEmpty()) {
yourtextboxname.setText("Waiting for Location");
}
else {
yourtextboxname.setText(addresses.get(0).getFeatureName() + ", " + addresses.get(0).getLocality() +", " + addresses.get(0).getAdminArea() + ", " + addresses.get(0).getCountryName());
}
}
Here is code to get current location and draw it on Google Maps
public class Showmap extends MapActivity {
private MapView mapView;
private MapController mapController;
private LocationManager locationManager;
private LocationListener locationListener;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.showmap);
LocationManager locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
locationListener = new GPSLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, locationListener);
mapView = (MapView) findViewById(R.id.mapView);
// enable Street view by default
mapView.setStreetView(true);
// enable to show Satellite view
// mapView.setSatellite(true);
// enable to show Traffic on map
// mapView.setTraffic(true);
mapView.setBuiltInZoomControls(true);
mapController = mapView.getController();
mapController.setZoom(16);
}
protected boolean isRouteDisplayed() {
return false;
}
private class GPSLocationListener implements LocationListener {
#Override
public void onLocationChanged(Location location) {
if (location != null) {
GeoPoint point = new GeoPoint(
(int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
mapController.animateTo(point);
mapController.setZoom(16);
// add marker
MapOverlay mapOverlay = new MapOverlay();
mapOverlay.setPointToDraw(point);
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
String address = ConvertPointToLocation(point);
Toast.makeText(getBaseContext(), address, Toast.LENGTH_SHORT)
.show();
mapView.invalidate();
}
}
public String ConvertPointToLocation(GeoPoint point) {
String address = "";
Geocoder geoCoder = new Geocoder(getBaseContext(),
Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocation(
point.getLatitudeE6() / 1E6,
point.getLongitudeE6() / 1E6, 1);
if (addresses.size() > 0) {
for (int index = 0; index < addresses.get(0)
.getMaxAddressLineIndex(); index++)
address += addresses.get(0).getAddressLine(index) + " ";
Log.i(address, address);
}
} catch (IOException e) {
e.printStackTrace();
}
return address;
}
#Override
public void onProviderDisabled(String provider) {}
#Override
public void onProviderEnabled(String provider) {}
#Override
public void onStatusChanged(String provider,int status,Bundle extras){}
}
class MapOverlay extends Overlay {
private GeoPoint pointToDraw;
public void setPointToDraw(GeoPoint point) {
pointToDraw = point;
}
public GeoPoint getPointToDraw() {
return pointToDraw;
}
#Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
super.draw(canvas, mapView, shadow);
// convert point to pixels
Point screenPts = new Point();
mapView.getProjection().toPixels(pointToDraw, screenPts);
// add marker
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.marker);
// 24 is the height of image
canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 24, null);
return true;
}
}
}
Use reverse geocoding,feed the latitude and longitude and get the address.
Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);
try {
List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);
if(addresses != null) {
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder("Address:\n");
for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
}
myAddress.setText(strReturnedAddress.toString());
}
else{
myAddress.setText("No Address returned!");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
myAddress.setText("Canont get Address!");
}
The phrase you're looking for is "Reverse Geocoding". Another question on StackOverflow discusses the same topic- You can use that one's selected answer :)
This is only for Hint add your code !
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if (location != null) {
System.out.println("in onlocationchanged");
String locationString=location.convert(location.getLatitude(),1);
Toast.makeText(this,"locationString=="+locationString, Toast.LENGTH_LONG).show();
double lat = location.getLatitude();
double lng = location.getLongitude();
String currentLocation = "The location is changed to Lat: " + lat + " Lng: " + lng;
Toast.makeText(this,currentLocation, Toast.LENGTH_LONG).show();
use this two method
public double getLattitude() {
return lattitude;
}
}
public double getLongitude() {
return longitude;
public class MainActivity extends AppCompatActivity {
double latitude, longitude;
private TextView tvLocation;
private Button btnGetLocation;
private FusedLocationProviderClient locationProviderClient;
private Geocoder geocoder;
private List<Address> addresses;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestPermission();
locationProviderClient = LocationServices.getFusedLocationProviderClient(this);
tvLocation = findViewById(R.id.tv_location);
geocoder = new Geocoder(MainActivity.this, Locale.getDefault());
btnGetLocation = findViewById(R.id.btn_location);
btnGetLocation.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (ActivityCompat.checkSelfPermission(MainActivity.this, ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
locationProviderClient.getLastLocation().addOnSuccessListener(MainActivity.this, new OnSuccessListener<Location>() {
#Override
public void onSuccess(Location location) {
if (location != null) {
tvLocation.setText(location.toString());
latitude = location.getLatitude();
longitude = location.getLongitude();
try {
addresses = geocoder.getFromLocation(latitude, longitude, 1);
String addressLine1 = addresses.get(0).getAddressLine(0);
Log.e("line1", addressLine1);
String city = addresses.get(0).getLocality();
Log.e("city", city);
String state = addresses.get(0).getAdminArea();
Log.e("state", state);
String pinCode = addresses.get(0).getPostalCode();
Log.e("pinCode", pinCode);
String fullAddress = addressLine1 + ", " + city + ", " + state + ", " + pinCode;
tvLocation.setText(fullAddress);
} catch (IOException e) {
e.printStackTrace();
Log.e("MainActivity", e.getMessage());
}
}
}
});
}
});
}
private void requestPermission() {
ActivityCompat.requestPermissions(this, new String[]{ACCESS_FINE_LOCATION}, 1);
}
}
Here Is my code to get the current country Name.
private String getCountry() {
String country_name = null;
LocationManager lm = (LocationManager)getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
Geocoder geocoder = new Geocoder(getApplicationContext());
for(String provider: lm.getAllProviders()) {
#SuppressWarnings("ResourceType") Location location = lm.getLastKnownLocation(provider);
if(location!=null) {
try {
List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
if(addresses != null && addresses.size() > 0) {
country_name =addresses.get(0).getCountryName();
return country_name;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Toast.makeText(getApplicationContext(), country_name, Toast.LENGTH_LONG).show();
return null;
}
But don't forget to add this permission in the manifest file.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
I have an HTC desire and am trying to write an app to show my current location.
I have managed to write the app and to show where I am. But I am only able to see my location in satelite view. When I try and only get the streetView the map appears blank and there are no streets shown at all.
Here is the code that I am trying to use for getting the streetView to show.
public class FindMeInMap extends MapActivity {
#Override
protected boolean isRouteDisplayed() {
return false;
}
MapController mapController;
MyPositionOverlay positionOverlay;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView myMapView = (MapView)findViewById(R.id.theMapView);
myMapView.displayZoomControls(true);
myMapView.setBuiltInZoomControls(true);
mapController = myMapView.getController();
myMapView.setSatellite(false);
myMapView.setStreetView(true);
myMapView.displayZoomControls(true);
myMapView.setTraffic(true);
// Add the MyPositionOverlay
positionOverlay = new MyPositionOverlay();
List<Overlay> overlays = myMapView.getOverlays();
overlays.add(positionOverlay);
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(context);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
updateWithNewLocation(location);
locationManager.requestLocationUpdates(provider, 2000, 10,
locationListener);
}
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
public void onProviderDisabled(String provider){
updateWithNewLocation(null);
}
public void onProviderEnabled(String provider){ }
public void onStatusChanged(String provider, int status,
Bundle extras){ }
};
private void updateWithNewLocation(Location location) {
String latLongString;
TextView myLocationText;
myLocationText = (TextView)findViewById(R.id.myLocationText);
String addressString = "No address found";
if (location != null) {
// Update my location marker
positionOverlay.setLocation(location);
// Update the map location.
Double geoLat = location.getLatitude()*1E6;
Double geoLng = location.getLongitude()*1E6;
GeoPoint point = new GeoPoint(geoLat.intValue(),
geoLng.intValue());
mapController.animateTo(point);
mapController.setZoom(17);
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "Lat:" + lat + "\nLong:" + lng;
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Geocoder gc = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = gc.getFromLocation(latitude,
longitude, 1);
StringBuilder sb = new StringBuilder();
if (addresses.size() > 0) {
Address address = addresses.get(0);
for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
sb.append(address.getAddressLine(i)).append("\n");
sb.append(address.getLocality()).append("\n");
sb.append(address.getPostalCode()).append("\n");
sb.append(address.getCountryName());
}
addressString = sb.toString();
} catch (IOException e) {}
} else {
latLongString = "No location found";
}
myLocationText.setText("Your Current Position is:\n" +
latLongString + "\n" + addressString);
}
}
Can anyone spot my problem and tell me why I cannot see this working on my Desire? I tried the same code on the emulator and it works fine, just not on the phone ?!?
Thanks in Advance!
Make the following changes:
myMapView.setSatellite(false);
myMapView.setStreetView(true);
myMapView.setBuiltInZoomControls(true);
myMapView.postInvalidate();
i try to get the current zip code from the longitude and latitude which MyLocationOverlay delivers and set this to a EditView on my Activity.
The Activity crashs when i try to get the longitude and latitude from MyLocationOverlay.
Whats wrong with this code?
Regards,
float
LogCat output: http://codepaste.net/vs6itk
Line 59 is the following line:
double currentLatitude = myLocationOverlay.getMyLocation().getLatitudeE6();
Here is my Code:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.partner_suche);
final EditText tView = (EditText) findViewById(R.id.editTextPLZ);
final MapView mView = (MapView) findViewById(R.id.mapview);
mView.getController().setZoom(14);
List<Overlay> mapOverlays = mView.getOverlays();
myLocationOverlay = new MyCustomLocationOverlay(this, mView);
mapOverlays.add(myLocationOverlay);
myLocationOverlay.enableMyLocation();
myLocationOverlay.enableCompass();
myLocationOverlay.runOnFirstFix(new Runnable() {
public void run() {
mView.getController().animateTo(myLocationOverlay.getMyLocation());
}
});
Geocoder gc = new Geocoder(this, Locale.getDefault());
double currentLatitude = myLocationOverlay.getMyLocation().getLatitudeE6();
double currentLongitute = myLocationOverlay.getMyLocation().getLongitudeE6();
try
{
List<Address> addresses = gc.getFromLocation(currentLatitude, currentLongitute, 1);
if (addresses.size() > 0)
{
tView.setText(addresses.get(0).getLocality());
}
} catch (IOException e)
{
}
}
EDIT:
I created a LocationListener to get my current location. Now the part crashes where i try to run gc.getFromLocation(latitude, longitude, 1); I can't read the Exception? :/
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(context);
String provider = LocationManager.GPS_PROVIDER;
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
public void onProviderDisabled(String provider){
updateWithNewLocation(null);
}
public void onProviderEnabled(String provider){ }
public void onStatusChanged(String provider, int status,Bundle extras){ }
};
locationManager.requestLocationUpdates(provider, 0, 0, locationListener);
private void updateWithNewLocation(Location location) {
final EditText tView = (EditText) findViewById(R.id.editTextPLZ);
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Geocoder gc = new Geocoder(this, Locale.getDefault());
if (location != null) {
try
{
List<Address> addresses = gc.getFromLocation(latitude, longitude, 1);
if (addresses.size() > 0)
{
Address address = addresses.get(0);
for (int i = 0; i < address.getMaxAddressLineIndex(); i++){
tView.setText(address.getPostalCode());
}
}
} catch (Exception e)
{
}
}
}
If you are using an emulator API level 8 or 9, and getting the exception:
java.io.IOException: Service not Available
then it is a known bug, see service not available
It works OK on real devices and emulator level 7 though. ( You should probably put a trap on addresses being null too, though this won't make the geocoder work!)
Most likely the location being returned from
myLocationOverlay.getMyLocation()
or
Location location = locationManager.getLastKnownLocation(provider);
is NULL. Likely due to your application not yet having received a location fix, and having no previously saved locations.
Try moving the code block where you do the geocoding into your runnable that runs after receiving a fix. Like this:
myLocationOverlay.runOnFirstFix(new Runnable() {
public void run() {
Location loc = myLocationOverlay.getMyLocation();
if (loc != null) {
mView.getController().animateTo(loc);
Geocoder gc = new Geocoder(this, Locale.getDefault());
double currentLatitude = loc.getLatitudeE6();
double currentLongitute = loc.getLongitudeE6();
try
{
List<Address> addresses = gc.getFromLocation(currentLatitude, currentLongitute, 1);
if (addresses.size() > 0)
{
tView.setText(addresses.get(0).getLocality());
}
} catch (IOException e)
{
}
}
}
});