Search code examples
androidmarkers

Android GoogleMap V2 how to add markers with user screen size?


I am working on google map v2 and i got a question, i can now add a single marker for my current location and i know the concept about adding markers, but now i want to add more markers which near my current location, and there are thousands of markers can add into the map due to the database

so how can i add markers within a range of area which is surround my current location, and the range will be the user's screen size. Thus more markers when zoom out and less markers when zoom in.

or maybe radius set as 5km of if current location

Thanks guys

marker = map.addMarker(new MarkerOptions().position(
             new LatLng(location.getLatitude(), location.getLongitude()))
             .title("my position").icon(BitmapDescriptorFactory
        .defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));

Solution

  • use this approach:

    var lat=new_marker.getPosition().lat();
    var lng=new_marker.getPosition().lng();
    
    var target = new GLatLng(lat,lng);
    
    var center= new GLatLng(yourLat, yourLng);//your gps position
    
    var distance = center.distanceFrom(target) / 1000;//meters to km
    
    if(distance < radius){//where radius = 5Km
        new_marker.setMap(map);//add to map
    }