I am trying to change marker colours depends on category id in google map Is there any cleaner way to make condition?
for (int i=0; i < response.body().size(); i++) {
LatLng place = new LatLng(response.body().get(i).getLatitude(), response.body().get(i).getLongitude());
if(response.body().get(i).getCategory().equals("1")){
mMap.addMarker(new MarkerOptions()
.position(place)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
.title(response.body().get(i).getName())
.snippet(response.body().get(i).getCategory()))
.setTag(response.body().get(i).getSlug());
}
else if(response.body().get(i).getCategory().equals("2")){
mMap.addMarker(new MarkerOptions()
.position(place)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW))
.title(response.body().get(i).getName())
.snippet(response.body().get(i).getCategory()))
.setTag(response.body().get(i).getSlug());
} else {
mMap.addMarker(new MarkerOptions()
.position(place)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
.title(response.body().get(i).getName())
.snippet(response.body().get(i).getCategory()))
.setTag(response.body().get(i).getSlug());
}
Something like this?
.position(place)
if(condition) {
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
}
...
Well you should look up for following to optimize your conditions:
So, if I follow above in your code below always remains same:
.title(response.body().get(i).getName())
.snippet(response.body().get(i).getCategory()))
.setTag(response.body().get(i).getSlug());
Also, in your condition you always repeat response.body().get(i).getCategory()
. Hence, if both could be extracted out your code will be way more readable and concise.
See below, how
String category = response.body().get(i).getCategory();
BitmapDescriptorFactory bitmapColor = null;//or your default color
if("1".equals(category )){
bitmapColor = BitmapDescriptorFactory.HUE_AZURE;
}
else if("2".equals(category )){
bitmapColor = BitmapDescriptorFactory.HUE_YELLOW;
} else {
bitmapColor = BitmapDescriptorFactory.HUE_BLUE;
}
mMap.addMarker(new MarkerOptions()
.position(place) .icon(BitmapDescriptorFactory.defaultMarker(bitmapColor))
.title(response.body().get(i).getName())
.snippet(response.body().get(i).getCategory()))
.setTag(response.body().get(i).getSlug());
Create a Map<String, Float>
that maps your categories to hues. Then retrieve values from it or return a default value.
Map<String, Float> hueMapping = new HashMap<>();
hueMapping.put("1", BitmapDescriptorFactory.HUE_AZURE);
hueMapping.put("2", BitmapDescriptorFactory.HUE_YELLOW);
Then instead of the whole if-else
construct, you can just write:
for (int i=0; i < response.body().size(); i++) {
YourObject obj = response.body().get(i); // Replace YourObject with the type you're getting here
LatLng place = new LatLng(obj.getLatitude(), obj.getLongitude());
float hue = hueMapping.getOrDefault(obj.getCategory(), BitmapDescriptorFactory.HUE_BLUE);
mMap.addMarker(new MarkerOptions()
.position(place)
.icon(BitmapDescriptorFactory.defaultMarker(hue))
.title(obj.getName())
.snippet(obj.getCategory()))
.setTag(obj.getSlug()
);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.