user2757842
user2757842

Reputation: 639

google maps android, all markers being loaded within circle

I am using a circle and trying to use its latitude and longitude values to load markers from a database that are within distance, the problem is, it is loading all the markers, rather than just the ones within the circle.

Can anyone point out where I'm going wrong? Code:

float[] distance = new float[10];
for(int i=0; i<jArray.length(); i++)
{
    JSONObject jsonData = jArray.getJSONObject(i);

    databaseMarkerData.add(new BasicNameValuePair("name", jsonData.getString("name")));
    databaseMarkerData.add(new BasicNameValuePair("address", jsonData.getString("address")));
    databaseMarkerData.add(new BasicNameValuePair("description", jsonData.getString("description")));
    databaseMarkerData.add(new BasicNameValuePair("rating", jsonData.getString("rating")));
    databaseMarkerData.add(new BasicNameValuePair("lat", jsonData.getString("lat")));
    databaseMarkerData.add(new BasicNameValuePair("lng", jsonData.getString("lng")));
    databaseMarkerData.add(new BasicNameValuePair("estType", jsonData.getString("estType")));
    databaseMarkerData.add(new BasicNameValuePair("reliability", jsonData.getString("reliability")));

    String latText = jsonData.getString("lat");
    Double lat = Double.parseDouble(latText);

    String lngText = jsonData.getString("lng");
    Double lng = Double.parseDouble(lngText);

    getMarkerDataArray.add(new MarkerData(jsonData.getString("name"), jsonData.getString("address"), jsonData.getString("description"),
            jsonData.getString("rating"), jsonData.getString("reliability"), lat, lng, jsonData.getString("estType")));



    Location.distanceBetween( lat, lng, circle.getCenter().latitude, circle.getCenter().longitude, distance);

    if( distance[i] < circle.getRadius())
    {
        plotMarkers(getMarkerDataArray);
    }
}

this is my plotMarkers function just in case it is needed:

plotMarkers()

private void plotMarkers(ArrayList<MarkerData> markers)
{
    if(markers.size() > 0)
    {
        for (MarkerData markerData : markers)
        {
            // Create user marker with custom icon and other options
            MarkerOptions markerOption = new MarkerOptions().position(new LatLng(markerData.getLat(), markerData.getLng()));

            Marker currentMarker = googleMap.addMarker(markerOption);
            markerDataHashmap.put(currentMarker, markerData);

            googleMap.setInfoWindowAdapter(new MarkerInfoWindowAdapter());
        }
    }

Upvotes: 3

Views: 273

Answers (1)

inmyth
inmyth

Reputation: 9050

There are more than one problem.

One is the first element of distance is what you should use:

    if( distance[0] < circle.getRadius()){
         plotMarkers(getMarkerDataArray);
    }

And you just need one element array float[] distance = new float[1];

Two is this

   if( distance[i] < circle.getRadius())
    {
    plotMarkers(getMarkerDataArray);
    }

that getMarkerDataArray is a list that is filled regardless whether the distance condition is met or not and used in plotMarkers for every time it is. I think what you want is

   for (...){
      if( distance[0] < circle.getRadius()){
         getMarkerDataArray.add(new MarkerData(...));
      }
   } 
   plotMarkers(getMarkerDataArray);

Upvotes: 2

Related Questions