在Android中查找当前位置的经度和纬度 [英] find current location latitude and longitude in Android

查看:69
本文介绍了在Android中查找当前位置的经度和纬度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在android应用程序中查找当前位置的纬度和经度.只要手机稳定,我就需要它.

I want to find latitude and longitude of my current location in an android application. I just need it when I am stable with my phone.

我的代码是:

LocationManager locationManager;
    locationManager = (LocationManager)getSystemService
    (Context.LOCATION_SERVICE);
    Location location = locationManager.getLastKnownLocation
    (LocationManager.GPS_PROVIDER);


    locationManager.requestLocationUpdates(                
             LocationManager.GPS_PROVIDER,0,0,(LocationListener) this);
    updateWithNewLocation(location);
    }

    private void updateWithNewLocation(Location location) {
    TextView myLocationText = (TextView)findViewById(R.id.myLocationText);

    String latLongString;

    if (location != null) {
    double lat = location.getLatitude();
    double lng = location.getLongitude();
    latLongString = "Lat:" + lat + "\nLong:" + lng;
    } else {
    latLongString = "No location found";
    }

    myLocationText.setText("Your Current Position is:\n" +
    latLongString);
    }

在模拟器上运行它后,我总是会发现找不到位置".为什么会这样呢?

After running it on emulator i always find "no location found". Why this is so?

推荐答案

public class GPSmap extends Activity {

    GeoPoint geoPoint;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LocationManager locManager;
        setContentView(R.layout.main);
        locManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
        locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L,
            500.0f, locationListener);
        Location location = locManager
                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (location != null) {
            double latitude = location.getLatitude();
            double longitude = location.getLongitude();
        }
    }

    private void updateWithNewLocation(Location location) {
        TextView myLocationText = (TextView) findViewById(R.id.text);
        String latLongString = "";
        if (location != null) {
            double lat = location.getLatitude();
            double lng = location.getLongitude();
            latLongString = "Lat:" + lat + "\nLong:" + lng;
        } else {
            latLongString = "No location found";
        }
        myLocationText.setText("Your Current Position is:\n" + latLongString);
    }

    private final LocationListener locationListener = new LocationListener() {

        public void onLocationChanged(Location location) {
            updateWithNewLocation(location);
        }

        public void onProviderDisabled(String provider) {
            updateWithNewLocation(null);
        }

        public void onProviderEnabled(String provider) {}

        public void onStatusChanged(String provider,int status,Bundle extras){}
    };
}

如果您将使用此代码,则会得到答案.

If you will use this code, you will get answer.

这篇关于在Android中查找当前位置的经度和纬度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆