Android 定位的实现

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Yes_butter/article/details/79284762
  • 安卓定位可以通过使用GPS或者通过network获取地址,俩个都需要增加获取位置的权限!
  • 需要在AndroidManifest里面增加权限!
  • 分别介绍一下学习心得!
//ACCESS_FINE_LOCATION:允许APP访问精确地理位置。
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
//ACCESS_COARSE_LOCATION:允许APP访问大概地理位置
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

GPS具体实现:

//检查是否开启权限!
 if (ActivityCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(this, "权限不够", Toast.LENGTH_LONG).show();
            return;
        }

//获取一个地址管理者,获取的方法比较特殊,不是直接new出来的
        LocationManager locationManager = (LocationManager) getSystemService(mContext.LOCATION_SERVICE);

//使用GPS获取上一次的地址,这样获取到的信息需要多次,才能够显示出来,所以后面有动态的判断
        Location location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
//判断是否用户打开了GPS开关,这个和获取权限没关系
        GPSisopen(locationManager);
//显示信息,可以根据自己的传入对应的location!!!
        upLoadInfor(location);

//获取时时更新,第一个是Provider,第二个参数是更新时间1000ms,第三个参数是更新半径,第四个是监听器
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 8, new LocationListener() {

            @Override
            /*当地理位置发生改变的时候调用*/
            public void onLocationChanged(Location location) {

                upLoadInfor(location);//实时的显示信息

            }

            /* 当状态发生改变的时候调用*/
            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {
                Log.d("GPS_SERVICES", "状态信息发生改变");

            }

            /*当定位者启用的时候调用*/
            @Override
            public void onProviderEnabled(String s) {
                Log.d("TAG", "onProviderEnabled: ");

            }

            @Override
            public void onProviderDisabled(String s) {
                Log.d("TAG", "onProviderDisabled: ");
            }
        });
    }

//判断是否用户打开GPS开关,并作指导性操作!
   private void GPSisopen(LocationManager locationManager) {
        if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            Toast.makeText(this, "请打开GPS", Toast.LENGTH_SHORT);
            final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            dialog.setTitle("请打开GPS连接");
            dialog.setMessage("为了获取定位服务,请先打开GPS");
            dialog.setPositiveButton("设置", new android.content.DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    //界面跳转
                    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    startActivityForResult(intent, 0);
                }
            });
            dialog.setNegativeButton("取消", new android.content.DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.dismiss();
                }
            });
            //调用显示方法!
            dialog.show();
        }
    }
//同时获取到的只是location如果想根据location获取具体地址,可以通过Android提供的API获取具体的地点!

//传进来一个location返回一个Address列表,这个是耗时的操作所以需要在子线程中进行!!!
//传进来一个location返回一个Address列表,这个是耗时的操作所以需要在子线程中进行!!!
//传进来一个location返回一个Address列表,这个是耗时的操作所以需要在子线程中进行!!!
   private List<Address> getAddress(Location location) {
        List<Address> result = null;
        try {
            if (location != null) {
                Geocoder gc = new Geocoder(this, Locale.getDefault());
                result = gc.getFromLocation(location.getLatitude(),
                        location.getLongitude(), 1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

//采取直接用匿名类的方法,构造了一个线程,但是在子线程中不能直接修改主线程的内容,否则会报错,但是!!!,当我用Android8.0模拟器测试的时候没有崩,当用Android7.0测试的时候直接崩溃,所以还是老老实实通过handler来解决这个问题!
new Thread(new Runnable() {
            @Override
            public void run() {
                Log.e("Run", "A new Thread");
                try {
                    final Location location1 = finalLocation;
                    addresses = getAddress(location1
                    );
                    if (addresses != null) {
                        Log.e("run: ", addresses.toString());
                        Message message = new Message();
                        message.what = 1;//信息内容
                        handler.sendMessage(message);//发送信息
                    }
                } catch (Exception e) {
                    Log.e("Exception", "ERRPOR");
                }
            }

        }).start();

//主线程中处理函数
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    tvMapInfo.setText(tvMapInfo.getText() + "\n" + addresses.toString());
                    break;
                default:
                    break;
            }
        }
    };

基站定位:

  • 优点耗时短,缺点定位精度不如GPS
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

猜你喜欢

转载自blog.csdn.net/Yes_butter/article/details/79284762