在获取当前位置在Android中的问题当前位置、问题、Android

2023-09-07 00:36:29 作者:weak 软弱

我想显示经纬度我目前的位置.. 对于这个,而不是搜索在谷歌,我搜查左右。

I want to display latitude and longitude my current location.. For this rather than searching in Google, i searched in SO.

我只是想显示我的当前位置的经度和纬度。

I just want to display my current location latitude and longitude.

请参阅我下面的code:

See my code below :

public class LocationGetter extends Activity {

@Override

public void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);

TextView tv = new TextView(this);

LocationManager mlocManager = 
                (LocationManager) getSystemService(Context.LOCATION_SERVICE);

LocationListener mlocListener = new LocationManagerHelper();

mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 100,mlocListener);

if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        tv.append("Latitude:- " + LocationManagerHelper.getLatitude()
                + '\n');
        tv.append("Longitude:- " + LocationManagerHelper.getLongitude()
                + '\n');

        Log.i("MyLocation1",Double.toString(LocationManagerHelper.getLatitude())+" "+Double.toString(LocationManagerHelper.getLongitude()));

} else {
    tv.setText("GPS is not turned on...");
}

/** set the content view to the TextView */
setContentView(tv);

}

/* Class My Location Listener */

public static class LocationManagerHelper implements LocationListener {

    private static double latitude;
    private static double longitude;

    @Override
    public void onLocationChanged(Location loc) {
        latitude = loc.getLatitude();
        longitude = loc.getLongitude();
        Log.i("MyLocation",Double.toString(latitude)+" "+Double.toString(longitude));
    }

    @Override
    public void onProviderDisabled(String provider) { }

    @Override
    public void onProviderEnabled(String provider) { }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

    public static double getLatitude() {
        return latitude;
    }

    public static double getLongitude() {
        return longitude;
    }

}

} 

我也清单文件添加权限:

I have also added permission in manifest file :

 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>

请参阅输出我得到:

我要去的地方错了?我不明白,它的一个简单的code和为什么它不工作?

Where i am going wrong ? I dont understand, its a simple code and why its not working ?

推荐答案

我刚刚验证了您的code,并与这些变化的作品,你只需要移动 tv.append()调用到 onLocationChanged()方法被调用每一次,如果你不使用的回调,那么你也会得到第一只有设置的值,它的唯一执行一次。

I just verified your code and it works with these changes, you just need to move the tv.append() calls to the onLocationChanged() method as that is called each time, if you don't use that for the CallBack, then u will get the first set values only and it's only executed once.

        public void onLocationChanged(Location loc) {
            latitude = loc.getLatitude();
            longitude = loc.getLongitude();
            Log.i("MyLocation",Double.toString(latitude)+" "+Double.toString(longitude));
            tv.append("Latitude:- " + LocationManagerHelper.getLatitude()
                    + '\n');
            tv.append("Longitude:- " + LocationManagerHelper.getLongitude()
                    + '\n');

            Log.i("MyLocation1",Double.toString(LocationManagerHelper.getLatitude())+" "+Double.toString(LocationManagerHelper.getLongitude()));

        }

和我在的Andr​​oidManifest.xml

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />    
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />    
<uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES" />    

您可以看看这些,并过滤掉那些你不感兴趣的,我已经使用从的这里

You can look at these and filter out the ones you won't be interested in. I have used a downloaded .gpx file from here

我已经在Android 2.2,虽然对其进行了测试,

I've tested it on Android 2.2 though,