2015-03-17 6 views
0

Я работаю над приложением Android и Google Maps, в котором я ищу конкретный адрес, используя следующий код. Я хочу получить местоположение искомого адреса, если он находится в 100 км в моем местоположении, и если он выходит за пределы этого предела, он не покажет мне.Расположение в пределах 100 км с помощью Google Maps в Android

Я пытаюсь найти искомое место в радиусе 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)); 
        }   
       }  
      } 
+0

действительно хотите знать, как найти диапазон 100 км ?? Я запутался –

+0

Фактически из моего текущего местоположения я ищу любое предпочтительное местоположение, если я получил искомый адрес и его расстояние больше 100 км, тогда он не будет искать местоположение. –

+0

do u хотите рассчитать расстояние между двумя геотомами, т.е. от текущего местоположения до того места, что вы ищете? –

ответ

3

Вы можете рассчитать расстояние с помощью этого кода; здесь, расстояние в метрах. Вы можете вызвать эту функцию и проверить, где находится место в диапазоне или нет.

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