获取当前经纬度 和 通过经纬度 得到大致位置的接口


转载  https://blog.csdn.net/qq_28946307/article/details/51175215

在Android中定位是属于危险权限,需要在添加Mainfest.xml中添加。

 <uses-permission android:name="android.permission.INTERNET">uses-permission>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION">uses-permission>
  • 1
  • 2

如果在Android6.0级以上系统,需要 动态的申请权限,也可以使用封装好 权限管理库。

  • network

    它是依靠信号塔或WiFi来定位的。对应的provider字段是LocationManager.NETWORK_PROVIDER,是一种低精度,低耗电的初略定位方式。

  • gps

    它是依靠GPS来定位的。对应的provider字段是LocationManager.GPS_PROVIDER,是高精度,高耗电的精准定位方式。

  • passive

    被动的获取定位信息,通过接受其他APP或service的定位信息。不过需要这个权限ACCESS_FINE_LOCATION。

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

    Google已经将这个定位方式hide了。

 安卓中有四种原生定位方式private LocationManager 
   locationManager; 
  privateLocationListener 
  locationListener; 
  
 
  




主函数代码//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 //首先我们通过获取系统的位置管理器的实例
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        //这一句使系统提示加上的,类似于判断啥的,我也不太懂,写下面代码的时候会自动提示你加上
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {return;}
        //然后我们通过位置管理器获取当前可用的位置提供器
        List prividers = locationManager.getProviders(true);
        Location location = null;
        String privider = null;
        if (prividers.contains(LocationManager.NETWORK_PROVIDER)) {
            privider = LocationManager.NETWORK_PROVIDER;
        } else if (prividers.contains(LocationManager.GPS_PROVIDER)) {
            privider = LocationManager.GPS_PROVIDER;
        } else {
            Toast.makeText(MainActivity.this, "当前没有可用的位置提供者", Toast.LENGTH_SHORT).show();
            return;
        }
        //根据获取的位置提供器获取当前的位置实例
        location = locationManager.getLastKnownLocation(privider);

        if (location != null) {
            showLocation(location);
        }
        //实例化位置提供器的监听对象,并实现其中的方法
        locationListener = new LocationListener() {
            //我们只需要注意这个方法,就是当位置发生变化时,我们的逻辑处理
            @Override
            public void onLocationChanged(Location location) {
                showLocation(location);
            }

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

            @Override
            public void onProviderEnabled(String provider) {
            }

            @Override
            public void onProviderDisabled(String provider) {
            }
        };
        //这个方法很重要,设置位置变化的监听事件,包含四个参数
        //第一个参数是位置提供器的类型
        //第二个参数是监听位置变化的时间间隔
        //第三个参数是监听位置变化的距离,以米为单位
        //第四个参数是我们实例化的监听器对象
        locationManager.requestLocationUpdates(privider, 5000, 1, locationListener);


得到经纬度的方法  和  销毁方法///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 private void showLocation(Location location) {
        String address = "经度:" + location.getLatitude() + "\n" + "纬度:" + location.getLongitude();
        //这个是把经纬度赋给TextView    注意  经纬度可能是反的   
       textView.setText(address);
        Toast.makeText(MainActivity.this, "更新啦", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
    //最后别忘了,在进程销毁的时候,将位置监听对象移除
        super.onDestroy();
        if (locationListener != null) {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {return;}
            locationManager.removeUpdates(locationListener);
        }
    }

  传经纬度得到大致位置的接口///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

转载   https://blog.csdn.net/u013790519/article/details/50417417


先传纬度再传经度


http://maps.google.cn/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true,language=zh-CN

你可能感兴趣的:(获取当前经纬度 和 通过经纬度 得到大致位置的接口)