我坚持使用lat和long:|
我的所作所为:我正在使用LocationManager来获取当前位置(我制作了对话框......显示我在加载时加载屏幕
public void onLocationChanged(Location loc)
但是..我设置了一个日志..或者只是一个简单的消息..我从来没有得到过这个函数的请求。
但如果我正在使用googlemap片段..那么我按下右侧顶部的按钮,它会立即显示我当前的位置..如何在这种情况下正确?如何获得cur位置(lat和long)?
我只是被困住了:(
public class getLocation implements LocationListener
{
public getLocation()
{
dialog = new ProgressDialog(MainActivity.this);
dialog.setMessage("Loading GPS Information");
dialog.setCancelable(true);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.show();
lm = (LocationManager) getSystemService(LOCATION_SERVICE);
new Thread() {
public void run() {
runOnUiThread(new Runnable() {
public void run() {
boolean enabled = lm
.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!enabled) {
Toast.makeText(MainActivity.this, "GPS NOT ENABLED" , Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
} else{
Toast.makeText(MainActivity.this, "GPS ENABLED" , Toast.LENGTH_SHORT).show();
if(lm.getLastKnownLocation(LocationManager.GPS_PROVIDER) != null){
Toast.makeText(MainActivity.this, "LAST LOCATION " , Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(MainActivity.this, "Last location unknown ", Toast.LENGTH_SHORT).show();
}
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, getLocation.this);
}
}
});
}
}.start();
handler = new Handler() {
public void handleMessage(android.os.Message msg) {
dialog.dismiss();
};
};
}
@Override
public void onLocationChanged(Location loc) {
// TODO Auto-generated method stub =
if(loc!=null){
if(apress){
at.setText("Lat = "+loc.getLatitude()+" | Long = "+loc.getLongitude());
apress=false;
Toast.makeText(MainActivity.this, "OnLocationChanged GPS" , Toast.LENGTH_SHORT).show();
latG=loc.getLatitude();
longG=loc.getLongitude();
showA.setEnabled(true);
handler.sendEmptyMessage(0);
}
}else{
Toast.makeText(MainActivity.this, "OnLocationChanged GPS Location - null" , Toast.LENGTH_SHORT).show();
}
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "ON PROVIDER DISABLED "+arg0 , Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "ON PROVIDER ENABLED "+arg0 , Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
Toast.makeText(MainActivity.this, "ON STATUS CHANGED "+arg0 , Toast.LENGTH_SHORT).show();
}
}
并在此功能中
public void onLocationChanged(Location loc) {
我从来没有接到过正确的电话..如果我在这里添加Log.e(或者其他东西)而不是它从未显示过来的意思是:(
答案 0 :(得分:0)
我建议你使用这个子类;
private class GPSLocationListener implements LocationListener,
GpsStatus.Listener {
boolean isGPSFix;
public void onGpsStatusChanged(int event) {
switch (event) {
case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
if (mLastLocation != null)
isGPSFix = (SystemClock.elapsedRealtime() - lastGPStime) < 3000;
if (isGPSFix) { // A fix has been acquired.
Toast toast = Toast.makeText(GPSLocationService.this, "GPS has a fix.", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
} else { // The fix has been lost.
Toast toast = Toast.makeText(GPSLocationService.this, "GPS DOES NOT have a fix.", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
break;
case GpsStatus.GPS_EVENT_FIRST_FIX:
Toast toast = Toast.makeText(GPSLocationService.this, "GPS got first fix.", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
isGPSFix = true;
break;
}
}
@Override
public void onLocationChanged(Location location) {
if(apress){
try {
latG = location.getLatitude();
longG = location.getLongitude();
at.setText("Lat = "+latG +" | Long = "+longG);
apress=false;
showA.setEnabled(true);
} catch (Exception e) {
latG = 0.0;
longG = 0.0;
}
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
String statusDescription = "unknown";
switch (status) {
case LocationProvider.OUT_OF_SERVICE:
statusDescription = "OUT_OF_SERVICE";
break;
case LocationProvider.AVAILABLE:
statusDescription = "AVAILABLE";
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
statusDescription = "TEMPORARILY_UNAVAILABLE";
break;
}
}
@Override
public void onProviderEnabled(String provider) {
Toast toast = Toast.makeText(GPSLocationService.this, "Your GPS is opened", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
@Override
public void onProviderDisabled(String provider) {
Toast toast = Toast.makeText(GPSLocationService.this, "Your GPS is closed", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
}
然后使用代码从MainActivity中调用此类;
GPSLocationListener gpsLocationListener = new GPSLocationListener();
// get the system manager
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, gpsLocationListener);
不要忘记在清单上添加<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
。
答案 1 :(得分:0)
嗯......代码工作正常。只需要留在天空下正确的工作..我在家里。这就是为什么我没有得到coords(lat和long。感谢所有...