位置在 100 公里以内,使用 Google 地图在 Android 范围内

Location within 100 km using Google Maps within Android

我正在开发 Android 应用程序和 Google 地图,我正在使用以下代码在其中搜索特定地址。我想获取搜索地址的位置,如果它在我所在位置的 100 公里以内,如果超出该限制,它不会显示我。

我正在尝试获取我 100 公里半径内的搜索位置。

private class GeocoderTask extends AsyncTask<String, Void, List<Address>>{

                @Override
                protected List<Address> doInBackground(String... locationName) {
                    // Creating an instance of Geocoder class
                    Geocoder geocoder = new Geocoder(getBaseContext());
                    List<Address> addresses = null;

                    try {
                        //
                        //24.798406, 54.790448
                        //25.452403, 55.537519


                        // Getting a maximum of 3 Address that matches the input text
                        addresses = geocoder.getFromLocationName(locationName[0], 10,
                                24.861969, 54.857740,25.545368, 55.474347);//.getFromLocationName(locationName[0], 3);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    return addresses;

                }


                @Override
                protected void onPostExecute(List<Address> addresses) {         

                    if(addresses==null || addresses.size()==0){
                        Toast.makeText(getBaseContext(), "No Location found", Toast.LENGTH_SHORT).show();
                    }

                    // Clears all the existing markers on the map
                    mMap.clear();

                    // Adding Markers on Google Map for each matching address
                    for(int i=0;i<addresses.size();i++){                

                        Address address = (Address) addresses.get(i);

                        LatLng latLng;
                        // Creating an instance of GeoPoint, to display in Google Map
                        latLng = new LatLng(address.getLatitude(), address.getLongitude());

                        String addressText = String.format("%s, %s",
                                address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
                                address.getCountryName());

                        markerOptions = new MarkerOptions();
                        markerOptions.position(latLng);
                       // markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.flag));
                        markerOptions.title(addressText);

                        mMap.addMarker(markerOptions);

                        // Locate the first location
                        if(i==0)                        
                            mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));  
                    }           
                }       
            }

您可以使用此代码计算距离;在这里,距离以米为单位。您可以调用此函数并检查位置是否在范围内。

private boolean checkForArea(int rad, LatLng fromPosition, LatLng toPosition) {
        Location locationA = new Location("point A");
        locationA.setLatitude(fromPosition.latitude);
        locationA.setLongitude(fromPosition.longitude);
        Location locationB = new Location("point B");
        locationB.setLatitude(toPosition.latitude);
        locationB.setLongitude(toPosition.longitude);
        int distance = (int) locationA.distanceTo(locationB);
        if (distance / 1000 <= rad)
            return true;
        else
            return false;
    }