在LocationListener中设置双精度值
问题描述:
看起来我有一些奇怪的变量设置问题。我有一个LocationListener可以正确更新并正确设置当前位置。我尝试设置包含当前纬度/经度的全局变量,并且它失败了(在我发布一些代码之后更多)。在LocationListener中设置双精度值
我有一个片段
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!" + "\nLat: " + Double.toString(lat) + "\nLon: " + 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() + "\nLat: " + Double.toString(lat) + "\nLon:" + Double.toString(lon));
}
}
那么,什么情况是,它会记录正确的位置,当我看到从LocationListener的内日志,但如果我尝试找回双纬度的价值观和double lon它会为这两个变量返回0.0。
答
// try this
public static LocationManager mlocManager;
public static LocationListner mListner;
private static String latitude;
public static String longitude;
private Location currentLocation;
public Location getCurrentLocation() {
return currentLocation;
}
public void setCurrentLocation(Location currentLocation) {
this.currentLocation = currentLocation;
}
try {
mlocManager = (LocationManager) getSystemService(Activity.LOCATION_SERVICE);
mListner = new LocationListner();
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
try {
mlocManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 0, 0, mListner);
} catch (Throwable e) {
e.printStackTrace();
}
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mListner);
} catch (Throwable e) {
e.printStackTrace();
}
try {
mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mListner);
} catch (Throwable e) {
e.printStackTrace();
}
}
});
} catch (Throwable e) {
e.printStackTrace();
}
public String getLatitude() {
if (latitude != null) {
return latitude;
}
Location loc = mlocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (loc == null) {
loc = mlocManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (loc == null) {
loc = mlocManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
}
if (loc != null) {
return "" + loc.getLatitude();
}
} else {
return "" + loc.getLatitude();
}
return "0.0";
}
public void setLatitude(String latitide) {
latitude = latitide;
}
public String getLongitude() {
if (longitude != null) {
return longitude;
}
Location loc = mlocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (loc == null) {
loc = mlocManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (loc == null) {
loc = mlocManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
}
if (loc != null) {
return "" + loc.getLongitude();
}
} else {
return "" + loc.getLongitude();
}
return "0.0";
}
public void setLongitude(String longitude) {
SmartActivity.longitude = longitude;
}
class LocationListner implements LocationListener {
@Override
public void onLocationChanged(Location location) {
setLatitude("" + location.getLatitude());
setLongitude("" + location.getLongitude());
setCurrentLocation(location);
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
+0
嗨,你可以plz upvote ans如果它对你有用谢谢... –
你在哪里调用newPlace()方法?你的MyMap类内部或外部? – user2652394
我从创建MapFragment的Activity中调用它。有一个带有OnClickListener的抽屉,当你按下它时,它弹出一个询问信息的对话框,创建Place对象,然后将它传递给MapFragment。 – gochuck