android - How do I retrieve the JSON response for each click on Google Map Marker?
问题描述
I was able to make multiple marks on the google map on android by getting data from JSON. So NOW each of those markers has their own corresponding bukken_name
, latitute
and longitude
.
Each time I click a specific marker on the map. I want it to display in console their retrieved JSON data.
In my current code, onMarkerClick
gets the values and then displays it in System.out.println
. But whenever I do so click the marker it only keeps on display the exact same value of the last JSON data.
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try{
JSONObject jsonObject = new JSONObject(response);
JSONObject data = jsonObject.getJSONObject("data");
JSONArray bukken_list = data.getJSONArray("bukken_list");
for (int i = 0; i < bukken_list.length(); i++) {
JSONObject returnBukken = bukken_list.getJSONObject(i);
String latitude = returnBukken.getString("ido_hokui"); //latitude
String longitude = returnBukken.getString("keido_tokei"); //longitude
final String bukken_name = returnBukken.getString("bukken_name"); //longitude
final Double x = Double.parseDouble(latitude);
final Double y = Double.parseDouble(longitude);
System.out.println("COORDINATES: " + i);
System.out.println("LATITUDE: " + latitude);
System.out.println("LONGITUDE: " + longitude);
System.out.println("BUKKEN NAME: " + longitude);
LatLng japan = new LatLng(x, y);
MarkerOptions client_marker = new MarkerOptions().position(new LatLng(x, y));
client_marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.mapicon_rent));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(japan, 10f));
mMap.addMarker(client_marker);
mMap.moveCamera(CameraUpdateFactory.newLatLng(japan));
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker arg0) {
//FAULT: does not get value of each marker
System.out.println("MARKER " + x + " " + y + " " + bukken_name);
return true;
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
int x = 1;
// Anything you want
}
});
解决方案
Set bukken_name as marker's tag:
Marker marker = mMap.addMarker(client_marker);
marker.setTag(bukken_name);
On marker click:
public boolean onMarkerClick(Marker clickedMarker) {
LatLng location = clickedMarker.getPosition();
System.out.println("MARKER " + location.latitude + " " + location.longitude + " " + clickedMarker.getTag());
return true;
}
推荐阅读
- r - 如何在根据 R 中的另一组定位它们的同时填充不同颜色的点?
- python - 将用户数据传递给 Django 中的管理操作
- html - 卡片列嵌入 Instragram 帖子:Chrome 和其他浏览器之间的区别
- python - 尝试同步 discord.py 命令时无法获取锁
- javascript - 如何在侧边栏地图上标记 Google 表格中的坐标?-- 地图接口
- android - 我无法让 Xamarin 调试我的 android 应用程序 - 缺少 android.jar?
- java - 如何在不关闭连接的情况下使用 SELECT LAST_INSERT_ID()
- ios - Firebase 推送通知不适用于某些 iOS 用户
- javascript - 如何使用 JS API 解决 YouTrack 中的问题
- tensorflow - 如何在定制的 Tensorflow 模型中打破循环(使用 Keras)