Reputation: 63
I have two classes.. First.java and Second.java,
In the First class.. latitude, longitude, and address show correctly in TextView. But in Second class, latitude and longitude become 0.0 , address becomes null.
Can anyone help me why?
Here's the code for First.java
public class First extends Activity {
private Context context=null;
AppLocationService appLocationService;
String address;
double latitude;
double longitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_location);
context=this;
appLocationService = new AppLocationService(First.this);
btngetLocation=(Button)findViewById(R.id.button_getLocation);
btngetLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// getting GPS status
boolean isGPSEnabled = appLocationService
.getStatus(LocationManager.GPS_PROVIDER);
// getting network status
boolean isNetworkEnabled = appLocationService
.getStatus(LocationManager.NETWORK_PROVIDER);
if( isGPSEnabled==false && isNetworkEnabled==false){
showSettingsAlert("Location Service");
}
else {
Toast.makeText(
getApplicationContext(),
"Attempt to get location...Please Wait.. ",
Toast.LENGTH_LONG).show();
if( isNetworkEnabled ==true ){
Location nwLocation = appLocationService
.getLocation(LocationManager.NETWORK_PROVIDER);
if (nwLocation == null){
Toast.makeText(
getApplicationContext(),
"Network bermasalah.. Pastikan Anda terkoneksi Internet, dan coba get Location lagi",
Toast.LENGTH_LONG).show();
}
else {
latitude = nwLocation.getLatitude();
longitude = nwLocation.getLongitude();
Toast.makeText(
getApplicationContext(),
"Mobile Location (NW): \nLatitude: " + latitude
+ "\nLongitude: " + longitude,
Toast.LENGTH_LONG).show();
}
}
else {
Location gpsLocation = appLocationService
.getLocation(LocationManager.GPS_PROVIDER);
if (gpsLocation == null){
Toast.makeText(
getApplicationContext(),
"GPS bermasalah.. Silahkan di ruangan terbuka atau aktifkan network provider Anda, dan coba get Location lagi",
Toast.LENGTH_LONG).show();
}
else {
latitude = gpsLocation.getLatitude();
longitude = gpsLocation.getLongitude();
Toast.makeText(
getApplicationContext(),
"Mobile Location (GPS): \nLatitude: " + latitude
+ "\nLongitude: " + longitude,
Toast.LENGTH_LONG).show();
}
}
}
}
});
}
public class AppLocationService extends Service implements LocationListener{
protected LocationManager locationManager;
Location location;
private static final long MIN_DISTANCE_FOR_UPDATE = 10; //10 meter
private static final long MIN_TIME_FOR_UPDATE = 1000 * 60 * 3; //3 menit
public AppLocationService(Context context) {
locationManager = (LocationManager) context
.getSystemService(LOCATION_SERVICE);
}
public boolean getStatus(String provider){
return locationManager.isProviderEnabled(provider); /
}
public Location getLocation(String provider) {
MIN_TIME_FOR_UPDATE, MIN_DISTANCE_FOR_UPDATE, this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(provider);
locationManager.removeUpdates(this);
return location;
}
return null;
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
public String getAddress(Context ctx, double latitude, double longitude) {
// StringBuilder result = new StringBuilder();
String alamat=null;
try {
Geocoder geocoder = new Geocoder(ctx, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
if (addresses.size() > 0) {
Address address = addresses.get(0);
String jalan = (address.getAddressLine(0));
String locality=address.getLocality();
String country=address.getCountryName();
String country_code=address.getCountryCode();
String zipcode=address.getPostalCode();
alamat = jalan +", "+ locality +", "+ country+", " + country_code+", " + zipcode;
}
} catch (IOException e) {
Log.e("tag", e.getMessage());
}
return alamat;
}
public double getLatitude(){
return latitude;
}
public double getLongitude(){
return longitude;
}
public String getAlamat(){
return address;
}
Here's Second. java
public class Second extends Activity{
AddLocation data_dr_location;
Double latitude;
Double longitude;
String address;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_description);
data_dr_location = new AddLocation();
address= data_dr_location.getAlamat();
latitude = data_dr_location.getLatitude();
longitude = data_dr_location.getLongitude(); }
Upvotes: 0
Views: 504
Reputation: 1141
your are only getting value.means you have written getter mehtod. you have not set value using setter method. please try that and check.
public void setLatitude(double latitude){
this.latitude = latitude;
}
public void setLongitude(double longitude){
this.longitude=longitude;
}
public setAlamat(String address){
this.address=address;
}
Upvotes: 1
Reputation: 9884
In your first java, there is only getter method and no setter method!
private double latitude;
private double longitude;
private String address;
public double getLatitude(){
return latitude;
}
public double getLongitude(){
return longitude;
}
public String getAlamat(){
return address;
}
/* THE BELOW SETTER METHODS ARE MISSING IN YOUR CODE.*/
public void setLatitude(double latitude){
this.latitude = latitude;
}
public void setLongitude(double longitude){
this.longitude=longitude;
}
public setAlamat(String address){
this.address=address;
}
At appropriate location (say whereever you get lat/long/address), add these lines.
latitude = nwLocation.getLatitude();
setLatitude (latitude);
longitude = nwLocation.getLongitude();
setLongitude (longitude);
Upvotes: 2