你好,我需要獲取當前座標,當我開始片段(在onResume())。我嘗試下一步:如何獲取當前位置(經度和緯度)
public class FragmentNear extends Fragment implements LocationListener{
View v;
protected LocationManager locationManager;
protected LocationListener locationListener;
protected Context context;
TextView tvLess1;
String lat;
String provider;
protected String latitude,longitude;
protected boolean gps_enabled,network_enabled;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v=inflater.inflate(R.layout.fragment_near, null);
tvLess1=(TextView)v.findViewById(R.id.tv_less_1);
return v;
}
@Override
public void onResume() {
locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
super.onResume();
}
@Override
public void onLocationChanged(Location location) {
Log.d("myLogs", "onChangeLocation");
//tvLess1.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude());
//Log.d("myLogs","Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude());
}
@Override
public void onProviderDisabled(String provider) {
Log.d("myLogs","disable");
}
@Override
public void onProviderEnabled(String provider) {
Log.d("myLogs","enable");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("myLogs","status");
}
}
它爲我獲取座標約一分鐘,爲什麼需要這麼多時間?然後onChangeLocation一直在調用,但我只想在onResume調用時更新座標。我該怎麼做?
的解決方案是未來的使用方法:
LocationManager service = (LocationManager) getActivity().getSystemService(getActivity().LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = service.getBestProvider(criteria, false);
Location location = service.getLastKnownLocation(provider);
LatLng userLocation = new LatLng(location.getLatitude(),location.getLongitude());
你在建築物內嗎? –
不明白 –