Usman Khan
Usman Khan

Reputation: 3973

Location within 100 km using Google Maps within Android

I am working on an Android application and Google Maps in which I am searching for the particular address by using the following code. I want to get the location of the searched address if it is in 100km within my location, and if it outside that limit, it will not show me.

I am trying to get the searched location within my 100km radius.

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));  
                    }           
                }       
            }

Upvotes: 0

Views: 3104

Answers (1)

Setu Kumar Basak
Setu Kumar Basak

Reputation: 12022

You can calculate distance using this code; here, distance is in meters. You can call this function and check where the location is in the range or not.

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;
    }

Upvotes: 3

Related Questions