简体   繁体   English

在Android中使用gps获取当前经度和纬度

[英]get current longitude and latitude using gps in android

I am trying to get current location using LocationManager class, but I am not getting any result in onLocationChanged method. 我正在尝试使用LocationManager类获取当前位置,但是在onLocationChanged方法中没有任何结果。

I am doing this in a fragment 我正在做一个片段

 LocationManager locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
 LocationListener locationListener = new myLocationListener();
 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, 0, locationListener);



private class myLocationListener implements LocationListener {
    @Override
    public void onLocationChanged(Location location) {

        Log.d(TAG, "Location Changed");
        Toast.makeText(getActivity(), location.getLatitude() + " " + location.getLongitude(), Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
}

create a new class in your project, name it GPSTracker.java and copy content below in this: 在您的项目中创建一个新类,将其命名为GPSTracker.java并在此复制以下内容:

Sorry unable to put its content here, so you have to search this class, you will get it from here then where you want current latitude and longitude, just use this code: 抱歉,无法将其内容放在这里,因此您必须搜索此类,您将在此处获取当前的纬度和经度,只需使用以下代码即可:

GPSTracker mTracker = new GPSTracker(MyActivity.this);

double lat=mTracker.getLatitude(); double lat = mTracker.getLatitude(); double lng=mTracker.getLongitude(); double lng = mTracker.getLongitude();

First of all you have to add the android.permission.ACCESS_FINE_LOCATION permission in your AndroidManifest.xml file. 首先,您必须在AndroidManifest.xml文件中添加android.permission.ACCESS_FINE_LOCATION权限。

After that you can get last known location using next code: 之后,您可以使用下一个代码获取最后的已知位置:

Criteria criteria = new Criteria();
String provider = mLocationManager.getBestProvider(criteria, true);
Location location = mLocationManager.getLastKnownLocation(provider);

You can setup power and accuracy requirements in the Criteria. 您可以在条件中设置功率和精度要求。 eg: 例如:

criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAccuracy(Criteria.NO_REQUIREMENT);

If last known location was not enabled, or if you need continues updates of location you should setup location listener. 如果未启用最后一个已知位置,或者您需要继续更新位置,则应设置位置侦听器。

But code that you show in question should setup it correctly. 但是您显示的代码应正确设置。 So, probably you do not turn location on on the device, or you don't have permissions. 因此,可能您没有在设备上打开位置信息,或者您没有权限。

It is better to use instead of LocationManager.GPS_PROVIDER, Criteria with requirements, because sometimes GPS provider is disables, but you can obtain location using network. 最好使用带有要求的条件代替LocationManager.GPS_PROVIDER,因为有时GPS提供程序已禁用,但是您可以使用网络获取位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM