通过安卓手机自带的定位功能定位

安卓手机定位,关键代码是获得lication这个变量
下边的两个获得location的方法经常出现location为null的情况

为null的情况:
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);  
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

或者

为null的情况:
if(providers.contains(LocationManager.GPS_PROVIDER)){  
            //如果是GPS  
            locationProvider = LocationManager.GPS_PROVIDER;  
        }else if(providers.contains(LocationManager.NETWORK_PROVIDER)){  
            //如果是Network  
            locationProvider = LocationManager.NETWORK_PROVIDER;  
        }else{  
            Toast.makeText(this, "没有可用的位置提供器", Toast.LENGTH_SHORT).show();  
            return ;  
        }  
        //获取Location  
        Location location = locationManager.getLastKnownLocation(locationProvider);

解决办法:

是用LocationListener,在回调中得到location。这里我自己加了一个接口myLocationListener
用于回调,将location传递给调用改工具方法的页面。在做相应处理。
代码如下

/**
 * Created by 张玉水 on 2016/7/19.
 */
public class GetLocationUtils {
    private Context mContext;
    private LocationManager locationManager;
    private String provider;
    private LocationListener llistener;
    private double latitude;
    private double longitude;

    public GetLocationUtils(Context context) {
        mContext = context;
    }

    public void getLocation(final myLocationListener listener) {
        latitude = 116.39722824;
        longitude = 39.90960456;
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        criteria.setPowerRequirement(Criteria.POWER_LOW);
        String serviceName = Context.LOCATION_SERVICE;
        locationManager = (LocationManager) mContext.getSystemService(serviceName);
        //locationManager.setTestProviderEnabled("gps", true);
        provider = locationManager.getBestProvider(criteria, true);
        llistener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {

                if (location != null) {
                    latitude = location.getLatitude(); //经度
                    longitude = location.getLongitude(); //纬度
                }
                double[] locations = new double[2];
                locations[0] = latitude;
                locations[1] = longitude;
                //通过回调,将存有经纬度的double[]传递出给调用改方法的页面
                listener.getdoublelocations(locations);

                locationManager.removeUpdates(this);
                locationManager.setTestProviderEnabled(provider, false);

            }

            @Override
            public void onProviderDisabled(String provider) {
                Log.i("onProviderDisabled", "come in");
            }

            @Override
            public void onProviderEnabled(String provider) {
                Log.i("onProviderEnabled", "come in");
            }

            @Override
            public void onStatusChanged(String provider, int status,
                                        Bundle extras) {
                Log.i("onStatusChanged", "come in");
            }
        };
        locationManager.requestLocationUpdates(provider, 1000, (float) 1000.0, llistener);

    }

    public interface myLocationListener{
        public void getdoublelocations(double[] locations);
    }

}

别忘了加上权限

<!-- 连接互联网Internet权限 -->
    <uses-permission android:name="android.permission.INTERNET" />
    <!-- GPS定位权限 -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

从上面可以看出,获取地理位置信息主要分如下步骤:
(1)获取LocationManager实例,通过getSystemService方法,传入Context.LOCATION_SERVICE参数。
(2)获取可用的位置提供器,有GPS_PROVIDER、NETWORK_PROVIDER、PASSIVE_PROVIDER三种,前两种比较常用。
(3)将(2)获取到的位置提供器传入LocationManager的方法getLastKnownLocation,即可获取Location信息。
如果移动设备地理位置不断发生变化,则实时更新需要进行如下步骤:
(4)调用LocationManager的requestLocationUpdates方法,第一个参数是位置提供器,第二个参数是监听位置变化的时间间隔(毫秒),第三个参数是监听位置变化的距 离间隔(米),第四个参数是LocationListener监听器
(5)当位置发生变化后,就会调用监听器的onLocationChanged方法。
(6)为了省电,节约资源,当程序关闭后,调用LocationManager的removeUpdates方法移除监听器。
另外下边的写法,有时候会获得地址,但有时候不准确,是上次保存的地址。如果没有加上边(4),就不会更新地址。所以只写下边的代码是不行的。

String serviceName = Context.LOCATION_SERVICE;
        locationManager = (LocationManager) mContext.getSystemService(serviceName);

        //另一种获得地址的反复,回调
        List<String> providers = locationManager.getProviders(true);
        String  locationProvider;
        if(providers.contains(LocationManager.GPS_PROVIDER)){
            //如果是GPS
            locationProvider = LocationManager.GPS_PROVIDER;
        }else if(providers.contains(LocationManager.NETWORK_PROVIDER)){
            //如果是Network
            locationProvider = LocationManager.NETWORK_PROVIDER;
        }else{
            locationProvider =null;
        }
        //获取Location
        Location location = locationManager.getLastKnownLocation(locationProvider);
        if(location != null){
            latitude = location.getLatitude(); //经度
            longitude = location.getLongitude(); //纬度
            double[] locations = new double[2];
            locations[0] = latitude;
            locations[1] = longitude;
            //通过回调,将存有经纬度的double[]传递出给调用改方法的页面
            listener.getdoublelocations(locations, location);
            //得到数据,直接返回
            return;

        }