It looks like I'm having some kind of strange variable setting problem. I have a LocationListener that updates properly and sets the current location correctly. I try to set global variables containing the current lat/long and it kind of fails(more on that after I post some code).
I have a fragment
public class MyMap extends MapFragment {
private LocationManager mLocManager;
private LocationListener myLocationListener;
protected volatile double lat;//Place to hold variable
protected volatile double lon;//Place to hold variable
public void onActivityCreated(Bundled savedInstanceState) {
super.onActivityCreated(savedInstanceState);
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this.getActivity().getApplicationContext());
if (resultCode == ConnectionResult.SUCCESS){
if(gmap == null){
gmap = this.getMap();
if(gmap != null){
gmap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
gmap.setOnMarkerDragListener(this);
gmap.setMyLocationEnabled(true);
gmap.setOnMapLongClickListener(this);
}else{
return;
}
}
}else{
GooglePlayServicesUtil.getErrorDialog(resultCode, this.getActivity(), RQS_GooglePlayServices);
}
distance = 100;
myLocationListener = new MyLocationListener();
mLocManager = (LocationManager) this.getActivity().getSystemService(Context.LOCATION_SERVICE);
}
@Override
public void onResume() {
super.onResume();
mLocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500,10, myLocationListener);
mLocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 500, 10, myLocationListener);
mLocManager.getLastKnownLocation(Context.LOCATION_SERVICE);
}
@Override
public void onPause() {
super.onPause();
mLocManager.removeUpdates(myLocationListener);
}
//Listen for location changes and update the map with that information
private class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
if (Math.abs(location.getLatitude()) > 0.0 && Math.abs(location.getLongitude()) > 0.0) {
lat = location.getLatitude();
lon = location.getLongitude();
}
//This reports correctly
Log.v("Magic", "LOCATION CHANGED!" + "
Lat: " + Double.toString(lat) + "
Lon: " + Double.toString(lon));
if (!currentLocationSet) {
gmap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lon), 15));
currentLocationSet = true;
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
public void newPlace(final UpdatePlacesCallBack upc, Place p) {
//This gives me 0.0 for both variables.
Log.v("Magic", "Name: " + p.getName() + "
Lat: " + Double.toString(lat) + "
Lon:" + Double.toString(lon));
}
}
So, what happens is that it'll log the correct location when I see the log from within the LocationListener, but if I try to retrieve the values of double lat and double lon it returns 0.0 for both variables.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…