title: Show markers near the device in Android
body: <p>I'm developing an android app which is having several kinds of markers. </p>
  
  <p>Here is my <code>MapsActivity.java</code> file.</p>
  
  <pre><code>public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleMap.OnMyLocationChangeListener {
  
  GoogleMap googleMap;
  List&lt;MapLocation&gt; restaurantList;
  List&lt;MapLocation&gt; hotelList;
  List&lt;Marker&gt; restaurantMarkers = new ArrayList&lt;&gt;();
  List&lt;Marker&gt; hotelMarkers = new ArrayList&lt;&gt;();
  
  
  MapLocation r1 = new MapLocation(6.9192,79.8950, "Mnhatten Fish Market" );
  MapLocation r2 = new MapLocation(6.9017,79.9192, "Dinemore" );
  MapLocation r3 = new MapLocation(6.9147,79.8778, "KFC" );
  MapLocation r4 = new MapLocation(6.9036,79.9547, "McDonalds" );
  MapLocation r5 = new MapLocation(6.8397,79.8758, "Dominos" );
  
  MapLocation h1 = new MapLocation(6.9006,79.8533, "Hilton" );
  MapLocation h2 = new MapLocation(6.8889,79.8567, "Galadari" );
  MapLocation h3 = new MapLocation(6.8756,79.8608, "Hotel Lagoon Dining" );
  MapLocation h4 = new MapLocation(6.7991,79.8767, "Aqua Pearl Lake Resort " );
  MapLocation h5 = new MapLocation(6.5833,79.1667, "KZ Resort" );
  
  //Buttons
  private final LatLng LOCATION_COLOMBO = new LatLng(6.9270786,79.861243);
  private final LatLng LOCATION_GALLE = new LatLng(6.0334009,80.218384);
  
  
  private GoogleMap mMap; // Might be null if Google Play services APK is not available.
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_maps);
      setUpMapIfNeeded();
  
      restaurantList = new ArrayList&lt;&gt;();
      hotelList =  new ArrayList&lt;&gt;();
  
      restaurantList.add(r1);
      restaurantList.add(r2);
      restaurantList.add(r3);
      restaurantList.add(r4);
      restaurantList.add(r5);
  
      hotelList.add(h1);
      hotelList.add(h2);
      hotelList.add(h3);
      hotelList.add(h4);
      hotelList.add(h5);
  
      // Give text to buttons
      Button buttonloc1 = (Button)findViewById(R.id.btnLoc1);
      buttonloc1.setText("Colombo");
  
      Button buttonloc2 = (Button)findViewById(R.id.btnLoc2);
      buttonloc2.setText("Galle");
  
      Button buttoncity = (Button)findViewById(R.id.btnCity);
      buttoncity.setText("My Location");
  
      Button buttonremove = (Button) findViewById(R.id.removeMarker);
      buttonremove.setText("Remove");
  
  
      CheckBox checkRestaurants = (CheckBox) findViewById(R.id.checkRestaurants);
      checkRestaurants.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
          @Override
          public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
              if (b) {
                  showRestaurants();
              } else {
                  hideRestaurants();
              }
          }
      });
      CheckBox checkHotels = (CheckBox) findViewById(R.id.checkHotels);
      checkHotels.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
          @Override
          public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
              if (b) {
                  showHotels();
              } else {
                  hideHotels();
              }
          }
      });
  
      // Marker to Dinemore
      mMap.addMarker(new MarkerOptions()
                      .position(LOCATION_COLOMBO)
                      .title("I'm in Colombo :D")
                      .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
      );
  
      // Marker to Barista
      mMap.addMarker(new MarkerOptions()
                      .position(LOCATION_GALLE)
                      .title("I'm in Galle :D")
                      .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
      );
  
  
  
      //When touch again on the map marker title will hide
      mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
          @Override
          public void onInfoWindowClick(Marker marker) {
  
          }
      });
  }
  
  @Override
  protected void onResume() {
      super.onResume();
      setUpMapIfNeeded();
  
      SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
              .findFragmentById(R.id.map);
  
      mapFragment.getMapAsync(this);
  }
  
  @Override
  public void onMyLocationChange(Location location) {
      Location target = new Location("target");
      for(LatLng point : new LatLng[]{}) {
          target.setLatitude(point.latitude);
          target.setLongitude(point.longitude);
          if(location.distanceTo(target) &lt;  100) {
              // bingo!
          }
      }
  }
  
  @Override
  public void onMapReady(GoogleMap map) {
  
      googleMap = map;
      setUpMap();
  }
  
  public void showRestaurants() {
  
      restaurantMarkers.clear();
      for (MapLocation loc : restaurantList){
          Marker marker = googleMap.addMarker(new MarkerOptions()
                  .position(new LatLng(loc.lat, loc.lon))
                  .title(loc.title)
                  .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));
  
          CameraPosition cameraPosition = new CameraPosition.Builder()
                  .target(new LatLng(loc.lat, loc.lon)).zoom(12).build();
          googleMap.animateCamera(CameraUpdateFactory
                  .newCameraPosition(cameraPosition));
  
          restaurantMarkers.add(marker);
      }
  }
  
  public void showHotels() {
  
      hotelMarkers.clear();
      for (MapLocation loc : hotelList){
          Marker marker = googleMap.addMarker(new MarkerOptions()
                  .position(new LatLng(loc.lat, loc.lon))
                  .title(loc.title)
                  .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
  
          CameraPosition cameraPosition = new CameraPosition.Builder()
                  .target(new LatLng(loc.lat, loc.lon)).zoom(12).build();
          googleMap.animateCamera(CameraUpdateFactory
                  .newCameraPosition(cameraPosition));
  
          hotelMarkers.add(marker);
      }
  }
  
  public void hideRestaurants(){
      for (Marker marker : restaurantMarkers){
          marker.remove();
      }
  }
  
  public void hideHotels(){
      for (Marker marker : hotelMarkers){
          marker.remove();
      }
  }
  
  public void onClick_City(View v){
      mMap.setMyLocationEnabled(true);
      mMap.setOnMyLocationChangeListener(this);
  }
  // When click on this button, Map shows the place of Dinemore
  public void onClick_Loc1(View v) {
      mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
      CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_COLOMBO,10);
      mMap.animateCamera(update);
  }
  
  // When click on this button, Map shows the place of Barista
  public void onClick_Loc2(View v) {
      mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
      CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_GALLE,10);
      mMap.animateCamera(update);
  }
  
  // To rmove All the Markers
  public void onClick_Remove(View v){
      mMap.clear();
  }
  
  private void setUpMapIfNeeded() {
      // Do a null check to confirm that we have not already instantiated the map.
      if (mMap == null) {
          // Try to obtain the map from the SupportMapFragment.
          mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                  .getMap();
          // Check if we were successful in obtaining the map.
          if (mMap != null) {
              setUpMap();
          }
      }
  }
  
  private void setUpMap() {
      mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
  }
  
  public class MapLocation {
      public MapLocation(double lt, double ln, String t){
          lat = lt;
          lon = ln;
          title = t;
      }
      public double lat;
      public double lon;
      public String title;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Now, what I need to do is, I need to show only markers which is near the device. </p>
  
  <p>Here are the steps what I need to do.</p>
  
  <ol>
  <li><p>Identify a circle round of the device area (500m)</p></li>
  <li><p>Identify the Markers inside this circle</p></li>
  <li><p>Show the identified Markers</p></li>
  <li><p>Re-identify the circle when the device is moving</p></li>
  <li><p>Need to change the circle and markers when the device is traveling.</p></li>
  </ol>
  
  <p>Can I do this in Android? If can please help me to do this or post articles that might help me.</p>
  
  <p>Thanks in advance.</p>