检查圆形地理围栏内的地理位置(纬度,经度)

问题描述 投票:3回答:1

我有一个保存的地理位置(纬度,经度)。当用户在应用中按下操作按钮时,将记录当前位置。

我想检查当前的纬度,经度是否下降在圆形地理围栏(我有中心地理点)中。

我不想在Google地图上创建地理围栏。我只想检查地理位置是否属于地理围栏。

<< [code我用来获得当前地理位置的精度小于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)

谢谢!

android google-api-client locationlistener android-geofence
1个回答
3
投票
如果知道圆的半径及其中心点,则可以使用以下公式来知道圆内是否还有其他点:

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS8wU2hOYi5wbmcifQ==” alt =“在此处输入图像描述”>这是距离公式。

同样,如果两个点都是Location对象。您可以使用Location.distanceTo

© www.soinside.com 2019 - 2024. All rights reserved.