0

マップapiを使用するアプリを作っています。私はサーバー上にたくさんのマーカーを持っています。私がしたいのは、は、特定の半径のユーザの現在の位置にあるのマーカーのみを表示します(たとえば、現在地から半径10 kmのマーカーをすべて表示します)。Androidで特定の半径のマーカーを表示する

MapFragment:

if (myLocation != null) { 
    latitude = myLocation.getLatitude(); 
    longitude = myLocation.getLongitude(); 
} 

MarkerOptions markerOptions = new MarkerOptions().position(
     new LatLng(latitude, longitude)).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_me)); 

mMap.addMarker(markerOptions); 
CameraPosition cameraPosition = new CameraPosition.Builder() 
     .target(new LatLng(latitude, longitude)).zoom(12).build(); 
mMap.animateCamera(CameraUpdateFactory 
     .newCameraPosition(cameraPosition)); 

mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() { 

    @Override 
    public void onMapLongClick(final LatLng arg0) { 

     RequestQueue queue = Volley.newRequestQueue(getActivity()); 
     String url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + String.valueOf(arg0.latitude) + "," + String.valueOf(arg0.longitude) + "&key=myKey"; 

     // Request a string response from the provided URL. 
     StringRequest stringRequest = new StringRequest(Request.Method.GET, url, 
       new Response.Listener<String>() { 
        @Override 
        public void onResponse(String response) { 
         try { 
          JSONArray jObj = new JSONObject(response).getJSONArray("results").getJSONObject(0).getJSONArray("address_components"); 

          Intent intent = new Intent(getActivity(), SetRestaurantActivity.class); 

          for (int i = 0; i < jObj.length(); i++) { 
           String componentName = new JSONObject(jObj.getString(i)).getJSONArray("types").getString(0); 
           if (componentName.equals("postal_code") || componentName.equals("locality") || componentName.equals("street_number") || componentName.equals("route") 
             || componentName.equals("neighborhood") || componentName.equals("sublocality") || componentName.equals("administrative_area_level_2") 
             || componentName.equals("administrative_area_level_1") || componentName.equals("country")) { 
            intent.putExtra(componentName, new JSONObject(jObj.getString(i)).getString("short_name")); 
           } 
          } 

          intent.putExtra("latitude", arg0.latitude); 
          intent.putExtra("longitude", arg0.longitude); 

          startActivity(intent); 

         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 
        } 
       }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       int x = 1; 
      } 
     }); 
     queue.add(stringRequest); 

    } 
}); 

MyRestaurantsFragment:

public class RestaurantsFragment extends Fragment { 

private static final String TAG = RestaurantsFragment.class.getSimpleName(); 

// Restaurants json url 
private ProgressDialog pDialog; 
private List<Restaurant> restaurantList = new ArrayList<>(); 
private ListView listView; 
private CustomListAdapter adapter; 

@Override 
public View onCreateView(LayoutInflater inflater, 
        ViewGroup container, 
        Bundle savedInstanceState) { 
View view = inflater.inflate(R.layout.fragment_restaurants, container, false); 

listView = (ListView) view.findViewById(R.id.restaurants_list); 
adapter = new CustomListAdapter(getActivity(), restaurantList); 
listView.setAdapter(adapter); 

pDialog = new ProgressDialog(getActivity()); 

pDialog.setMessage("Loading..."); 
pDialog.show(); 

SQLiteHandler db = new SQLiteHandler(getActivity().getApplicationContext()); 
HashMap<String, String> user = db.getUserDetails(); 
final String userId = user.get("uid"); 

StringRequest restaurantReq = new StringRequest(Request.Method.POST, 
     AppConfig.URL_GET_RESTAURANT, new Response.Listener<String>() { 

    @Override 
    public void onResponse(String response) { 
     Log.d(TAG, "Login Response: " + response.toString()); 
     try { 
      JSONObject jObj = new JSONObject(response); 
      boolean error = jObj.getBoolean("error"); 

      if (!error) { 
       JSONArray restaurants = jObj.getJSONArray("restaurant"); 

       Log.d(TAG, response.toString()); 
       hidePDialog(); 

       // Parsing json 
       for (int i = 0; i < restaurants.length(); i++) { 
        try { 

         JSONObject obj = restaurants.getJSONObject(i); 
         Restaurant restaurant = new Restaurant(); 
         restaurant.setUserName(obj.getString("name")); 
         if (obj.getString("image") != null && !obj.getString("image").isEmpty()) { 
          restaurant.setThumbnailUrl(obj.getString("image")); 
         } 
         restaurant.setLat(obj.getString("latitude")); 
         restaurant.setLon(obj.getString("longitude")); 
         restaurant.setDate(obj.getString("restaurant_name")); 
         restaurant.setTime(obj.getString("restaurant_description")); 

         // adding restaurant to restaurant array 
         restaurantList.add(restaurant); 

        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 

       } 

       // notifying list adapter about data changes 
       // so that it renders the list view with updated data 
       adapter.notifyDataSetChanged(); 
      } else { 
       // Error. Get the error message 
       String errorMsg = jObj.getString("error_msg"); 
      } 
     } catch (JSONException e) { 
      // JSON error 
      e.printStackTrace(); 
     } 

    } 
}, 

答えて

1

使用google-maps-utilsからSphericalUtils方法computeDistanceBetween()すべてのレストランに自分の位置からの距離を計算し、計算した距離によってそのコレクションをフィルタリングする

... 
    restaurant.setDate(obj.getString("restaurant_name")); 
    restaurant.setTime(obj.getString("restaurant_description")); 
    restaurant.setLat(obj.getString("latitude")); 
    restaurant.setLon(obj.getString("longitude")); 
    // adding restaurant to restaurant array 
    if (SphericalUtil.computeDistanceBetween(new LatLng(restaurant.getLat(), restaurant.getLon()), userLatLng)<10) 
     restaurantList.add(restaurant); 
    } 
関連する問題