Androidの位置づけの実現

10376 ワード

  • アンドロイド測位は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
    getAddress(Location location) { List
    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() + "
    "
    + addresses.toString()); break; default: break; } } };

    基地局の位置付け:
  • の利点は時間が短く、欠点の位置決め精度はGPS
  • に及ばない.
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);