1

我有一個已保存的地理位置(緯度,經度)。當用戶在應用中按下動作按鈕時,會記錄當前位置。檢查圓形地理圍欄內的地理位置(緯度,經度)

我想檢查當前的緯度,經度(我有)在圓形地理圍欄(誰是我的中心地理點)下降了 。

我不想在Google地圖上創建地理圍欄。我只想檢查地理位置是否落入地理圍欄。

代碼我用它來獲取當前地理位置時精度小於20:

public class CreatePlaceActivity extends Fragment implements 
     OnClickListener, 
     GoogleApiClient.ConnectionCallbacks, 
     GoogleApiClient.OnConnectionFailedListener, LocationListener { 
    private Button cancelBtn, submitBtn; 

    public boolean geoLocationCheck = false; 
    private String TAG = "CreatePlaceActivity"; 

    public CreatePlaceActivity() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     View rootView = inflater.inflate(R.layout.create_place_screen, 
       container, false); 
     submitBtn = (Button) rootView.findViewById(R.id.submit_btn_id); 
     submitBtn.setTypeface(typeface); 
     submitBtn.setOnClickListener(this); 
     cancelBtn = (Button) rootView.findViewById(R.id.cancel_btn_id); 
     cancelBtn.setOnClickListener(this); 

     setlocationClient(); 
     return rootView; 
    } 

    private void setlocationClient() { 
     int resp = GooglePlayServicesUtil 
       .isGooglePlayServicesAvailable(getActivity()); 
     if (resp == ConnectionResult.SUCCESS) { 

      locationclient = new GoogleApiClient.Builder(getActivity()) 
        .addApi(LocationServices.API).addConnectionCallbacks(this) 
        .addOnConnectionFailedListener(this) 
        .build(); 
      locationclient.connect(); 
     } else { 
      GooglePalyErrorDialogIsShowing = true; 

      Toast.makeText(getActivity(),// CreatePlaceActivity.this, 
        "Google Play Service Error " + resp, Toast.LENGTH_LONG) 
        .show(); 

      if (alert == null) { 
       onLocationChangedbuilder = new AlertDialog.Builder(
         getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT); 
      } else if (alert.isShowing()) { 
       alert.dismiss(); 

       onLocationChangedbuilder = new AlertDialog.Builder(
         getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT); 

      } 
      onLocationChangedbuilder 
        .setTitle("Alert!") 
        .setMessage("Google Play Service Error " + resp) 
        .setPositiveButton("OK", 
          new DialogInterface.OnClickListener() { 

           @Override 
           public void onClick(DialogInterface dialog, 
             int which) { 

            locationLockDialogIsShowing = false; 
            GooglePalyErrorDialogIsShowing = false; 

            ((MenuActivity) getActivity()) 
              .removeFragment(CreatePlaceActivity.this); 

           } 
          }); 

      alert = onLocationChangedbuilder.create(); 
      alert.setCancelable(false); 
      try { 
       alert.show(); 
      } catch (Exception e) { 

      } 

     } 

    } 

    @Override 
    public void onClick(View v) { 
     switch (v.getId()) { 
     case R.id.submit_btn_id: 
      Log.i(TAG, "lat=" + currentGeoLocation.getLatitude() + "lon=" 
        + currentGeoLocation.getLongitude() + "acc=" 
        + currentGeoLocation.getAccuracy() + "alt=" 
        + currentGeoLocation.getAltitude()); 
      validatePlace(); 

      break; 
     case R.id.cancel_btn_id: 
      // finish(); 
      ((MenuActivity) getActivity()) 
        .removeFragment(CreatePlaceActivity.this); 

      break; 

     default: 
      break; 
     } 
    } 


    @Override 
    public void onLocationChanged(Location location) { 

     float accuracy = 50; 

     // DateTime dt1 = new DateTime(); 
     // DateTimeFormatter fmt1 = ISODateTimeFormat.dateTime(); 

     Time time = new Time(); 
     time.setToNow(); 
     String strdt = String.valueOf(time.toMillis(false)); 

     if (location != null) { 

      accuracy = location.getAccuracy(); 

      locationLockDialogIsShowing = true; 

      if (globalAccuracy - location.getAccuracy() >= 20 
        || globalAccuracy - location.getAccuracy() >= -20) { 

       globalAccuracy = location.getAccuracy(); 
      } 
     } 



     if (alert == null) { 
      onLocationChangedbuilder = new AlertDialog.Builder(getActivity(), 
        AlertDialog.THEME_DEVICE_DEFAULT_LIGHT); 
     } else if (alert.isShowing()) { 
      alert.dismiss(); 

      onLocationChangedbuilder = new AlertDialog.Builder(getActivity(), 
        AlertDialog.THEME_DEVICE_DEFAULT_LIGHT); 

     } 
     final float accr = accuracy; 
     onLocationChangedbuilder 
       .setTitle("Alert!") 
       .setMessage(
         "Getting the best possible location..Please wait.\nCurrent Accuracy is " 
           + accuracy) 
       .setPositiveButton("Cancel", 
         new DialogInterface.OnClickListener() { 

          @Override 
          public void onClick(DialogInterface dialog, 
            int which) { 
           // TODO Auto-generated method stub 

           locationLockDialogIsShowing = false; 

           if (locationclient != null) { 

            locationclient.disconnect(); 
           } 

           Time time = new Time(); 
           time.setToNow(); 
           String strdt = String.valueOf(time 
             .toMillis(false)); 



           if (alert == null) { 

           } else if (alert.isShowing()) { 
            alert.dismiss(); 
           } 
           if (locationLockAlert == null) { 

           } else if (locationLockAlert.isShowing()) { 
            locationLockAlert.dismiss(); 
           } 
           // setResult(RESULT_OK); 
           // finish(); 
           ((MenuActivity) getActivity()) 
             .removeFragment(CreatePlaceActivity.this); 
          } 
         }); 

     alert = onLocationChangedbuilder.create(); 
     alert.setCancelable(false); 
     try { 
      alert.show(); 
     } catch (Exception e) { 

     } 
     if (location.getAccuracy() <= 16) { 

      DateTime dt = new DateTime(); 
      DateTimeFormatter fmt = ISODateTimeFormat.dateTime(); 
      String str1 = fmt.print(dt); 

      locationLockDialogIsShowing = false; 

      Time timeNow = new Time(); 
      timeNow.setToNow(); 

      String str = String.valueOf(timeNow.toMillis(false)); 

      // locationLockTimestamp = str1; 
      // TODO 
      currentGeoLocation = location; 

      if (locationclient != null) { 
       if (locationclient.isConnected()) { 

        LocationServices.FusedLocationApi.removeLocationUpdates(
          locationclient, this); 
        locationclient.disconnect(); 
       } 
      } 

      if (alert.isShowing()) { 
       alert.dismiss(); 
      } 
      if (locationLockAlert != null) { 

       if (locationLockAlert.isShowing()) { 
        locationLockAlert.dismiss(); 
       } 
      } 
      showDialog("Alert!", 
        "GPS Location locked.. You can now continue to create place"); 

     } 


    } 

    @Override 
    public void onConnectionFailed(ConnectionResult result) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onConnected(Bundle connectionHint) { 
     // TODO Auto-generated method stub 

     final float accuracy = 250; 

     if (locationclient != null && locationclient.isConnected()) { 

      if (geoLocationCheck == false) { 

       locationrequest = LocationRequest.create(); 
       locationrequest 
         .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 

       locationrequest.setInterval(100); 
       // locationclient.requestLocationUpdates(locationrequest, this); 
       LocationServices.FusedLocationApi.requestLocationUpdates(
         locationclient, locationrequest, this); 

       locationLockDialogIsShowing = true; 

       if (alert == null) { 
        onLocationChangedbuilder = new AlertDialog.Builder(
          getActivity(), 
          AlertDialog.THEME_DEVICE_DEFAULT_LIGHT); 
       } else if (alert.isShowing()) { 
        alert.dismiss(); 

        onLocationChangedbuilder = new AlertDialog.Builder(
          getActivity(), 
          AlertDialog.THEME_DEVICE_DEFAULT_LIGHT); 

       } 
       onLocationChangedbuilder 
         .setTitle("Alert!") 
         .setMessage(
           "Getting the best possible location..Please wait.\nCurrent Accuracy is " 
             + accuracy) 
         .setPositiveButton("Cancel", 
           new DialogInterface.OnClickListener() { 

            @Override 
            public void onClick(DialogInterface dialog, 
              int which) { 
             // TODO Auto-generated method stub 

             locationLockDialogIsShowing = false; 

             if (locationclient != null) { 
              locationclient.disconnect(); 
             } 

             Time time = new Time(); 
             time.setToNow(); 
             String strdt = String.valueOf(time 
               .toMillis(false)); 


             if (alert == null) { 
             } else if (alert.isShowing()) { 
              alert.dismiss(); 
             } 
             if (locationLockAlert == null) { 

             } else if (locationLockAlert 
               .isShowing()) { 
              locationLockAlert.dismiss(); 
             } 
             // setResult(RESULT_OK); 
             // finish(); 
             ((MenuActivity) getActivity()) 
               .removeFragment(CreatePlaceActivity.this); 
            } 
           }); 

       alert = onLocationChangedbuilder.create(); 
       alert.setCancelable(false); 
       try { 
        alert.show(); 
       } catch (Exception e) { 

       } 

      } 
     } 

    } 


    @Override 
    public void onConnectionSuspended(int arg0) { 
     Log.i("fused", "loc client onConnectionSuspended"); 
    } 

    public Dialog showDialog(String title, String msg) { 

     locationLockAlert = new AlertDialog.Builder(getActivity(), 
       AlertDialog.THEME_DEVICE_DEFAULT_LIGHT).create(); 
     locationLockAlert.setTitle(title); 
     locationLockAlert.setMessage(msg); 
     locationLockAlert.setButton("OK", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         locationLockAlert.dismiss(); 

        } 
       }); 
     try { 
      locationLockAlert.show(); 
     } catch (Exception e) { 

     } 
     return locationLockAlert; 

    } 

    @Override 
    public void onNothingSelected(AdapterView<?> parent) { 
     // TODO Auto-generated method stub 

    } 
} 

鏈接我剛纔提到是Android Geofencing (Polygon)

謝謝!

+0

http://en.wikipedia.org/wiki/Pythagorean_theorem僅適用於noobs?或簡單的'location.distanceTo()' – injecteer

回答

2

如果您知道圓的半徑和它的中心點,你知道,如果任何其它點是圓圈內通過使用下面的公式:

enter image description here 這是距離公式。

另外,如果兩個點都是Location對象。你可以使用Location.distanceTo

相關問題