Google maps android, all markers are loaded in a circle
I am using a circle and am trying to use its latitude and longitude values ββto load markers from a database in a distance, the problem is that it loads all markers, not just the ones inside the circle.
Can anyone point out where I am 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 if 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());
}
}
source to share
There are several problems.
One of the first element distance
is what you should be using:
if( distance[0] < circle.getRadius()){
plotMarkers(getMarkerDataArray);
}
And you only need one array of elements float[] distance = new float[1];
Two is
if( distance[i] < circle.getRadius())
{
plotMarkers(getMarkerDataArray);
}
which getMarkerDataArray
is a list that gets populated regardless of whether the distance condition is met or not and is used at plotMarkers
a time. I think what you want
for (...){
if( distance[0] < circle.getRadius()){
getMarkerDataArray.add(new MarkerData(...));
}
}
plotMarkers(getMarkerDataArray);
source to share