在Android中查找当前位置的纬度和经度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2250597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:09:42  来源:igfitidea点击:

find current location latitude and longitude in Android

android

提问by rashmi

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.

我想在 android 应用程序中找到我当前位置的纬度和经度。当我的手机稳定时,我才需要它。

My code is:

我的代码是:

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?

在模拟器上运行后,我总是发现“找不到位置”。为什么会这样?

回答by JohnNick

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.

如果您将使用此代码,您将得到答案。

回答by CommonsWare

The GPS radio is not kept on all of the time, as it seriously drains the battery. At the point when you are calling getLastKnownLocation(), the radio is probably off.

GPS 无线电并非一直保持开启状态,因为它会严重耗尽电池电量。在您拨打电话时getLastKnownLocation(),收音机可能已关闭。

You need to do your requestLocationUpdates()first, then wait for a fix to be supplied to your LocationListener. Any time after that (until you removeUpdates()), getLastKnownLocation()should return a non-nullvalue.

您需要先做您的工作requestLocationUpdates(),然后等待修复程序提供给您的LocationListener. 之后的任何时间(直到你removeUpdates()),getLastKnownLocation()都应该返回一个非null值。

回答by Pratik Butani

I have search lots of tutorial but found this best one:

我搜索了很多教程,但找到了最好的一个:

Using Criteriaand BestProvider

使用CriteriaBestProvider

    /** PROCESS for Get Longitude and Latitude **/
locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);

// getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
Log.d("msg", "GPS:"+isGPSEnabled);

// check if GPS enabled     
if(isGPSEnabled){
    /*
    longitude = 70.80079728674089;
    latitude =  22.29090332494221;
     */
     Criteria criteria = new Criteria();
     String provider = locationManager.getBestProvider(criteria, false);
     Location location = locationManager.getLastKnownLocation(provider);

    //new LoadPlaces().execute();
    //Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if(location != null)
    {
        longitude = location.getLongitude();
        latitude = location.getLatitude();
        Log.d("msg", "first lat long : "+latitude +" "+ longitude);
        //new LoadPlaces().execute();
    }else
    {
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() {

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

            }

            @Override
            public void onProviderEnabled(String provider) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProviderDisabled(String provider) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onLocationChanged(Location location) {
                // TODO Auto-generated method stub
                longitude = location.getLongitude();
                latitude = location.getLatitude();
                Log.d("msg", "changed lat long : "+latitude +" "+ longitude);
            }
        });
    }

}
else
{
    showAlertforGPS();
}

回答by nithinreddy

The emulator by default, may not have any location associated. So it is not giving any.

默认情况下,模拟器可能没有任何关联的位置。所以它没有给予任何。

To send a location, go to DDMS perspective in Eclipse, and enter the latitude and longitude and click Send. Then you will be able to see that in your application.

要发送位置,请转到 Eclipse 中的 DDMS 透视图,输入纬度和经度,然后单击发送。然后您将能够在您的应用程序中看到它。

Let me know if you still have any issues.

如果您还有任何问题,请告诉我。