Android谷歌地图圈可点击



我正在制作一个应用程序,通过在地图上围绕受影响的国家画一个圆圈来追踪地图上的冠状病毒。我做了那个圆圈,但我想要的是,当我点击任何一个圆圈时,它都会显示一些数据。所以我想让一个圆圈可以点击,这样当我点击任何一个圆圈时,它都会在文本框中显示该国家的一些细节
这是地图活动代码:

private GoogleMap mMap;
SearchView searchView;
private static final String TAG = MapsActivity.class.getSimpleName();
@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
searchView = findViewById(R.id.search_location);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
String location = searchView.getQuery().toString();
List<Address> addressList = null;
if (location != null || !location.equals("")){
Geocoder geocoder = new Geocoder(MapsActivity.this);
try {
addressList = geocoder.getFromLocationName(location, 1);
} catch (IOException e) {
e.printStackTrace();
}
Address address = addressList.get(0);
LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 6));
}
return false;
}
@Override
public boolean onQueryTextChange(String s) {
return false;
}
});
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
//set zoom control bar to zoom in and zoom out
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(true);
mMap.getUiSettings().setMapToolbarEnabled(true);
try{
// Customise the styling of the base map using a JSON object defined
// in a raw resource file.
boolean success = googleMap.setMapStyle(
MapStyleOptions.loadRawResourceStyle(
this, R.raw.style_json));
if (!success) {
Log.e(TAG, "Style parsing failed.");
}
} catch (Resources.NotFoundException e) {
Log.e(TAG, "Can't find style. Error: ", e);
}
fetchData();
}
private void fetchData() {
String url = "https://corona.lmao.ninja/v2/countries/";
StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray jsonArray = new JSONArray(response);
for (int i = 0; i < jsonArray.length(); i++) {
Double radius;
JSONObject jsonObject = jsonArray.getJSONObject(i);
//String countryName = jsonObject.getString("country");
Double population = jsonObject.getDouble("population");
Double cases = jsonObject.getDouble("cases");
JSONObject object = jsonObject.getJSONObject("countryInfo");
Double lat = object.getDouble("lat");
Double lng = object.getDouble("long");
Double zoomLevel = Double.valueOf(mMap.getCameraPosition().zoom);

radius = ((cases / population )* 100) * 500000;
if (radius <= 80000) {
radius = 150000d;
}
else if (radius > 80000 && radius < 600000){
radius = 300000d;
}
else radius = 600000d;
CircleOptions circleOptions= new CircleOptions().strokeWidth(3f).center(new LatLng(lat, lng)).radius(radius*(zoomLevel/9))
.strokeColor(Color.RED).fillColor(Color.argb(70, 150, 50, 50));;
Circle circle = mMap.addCircle(circleOptions);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MapsActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(request);
}

}

您可以为映射定义一个监听器

mMap.setOnCircleClickListener(...)

并使圆圈可点击

circleOptions.clickable(true)

最新更新