java - 从 android Java 中的 volley repose 向 google maps API 添加多个标记
问题描述
我是 Google Maps API for android 的新手,但我想在我的 android 应用程序中创建一个视图,以显示数据库中某些提供程序的位置。到目前为止,我已经完成了以下工作:
public void onResponse(String response) {
Log.d(TAG, "Response: " + response);
try {
JSONObject jsonObject;
JSONObject drinkObject = new JSONObject(response);
JSONArray jsonArray = drinkObject.getJSONArray("vendors");
for(int i=0; i<jsonArray.length();i++){
jsonObject = jsonArray.getJSONObject(i);
latlngs.add(new LatLng(jsonObject.getDouble("latitude"), jsonObject.getDouble("longitude")));
}
for (LatLng point : latlngs) {
options.position(point);
options.title("mama fua service provider");
options.snippet("someDesc");
googleMap.addMarker(options);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
有了这个,我已经能够添加标记,但它们都被组合成一个完整的世界页面地图,如此处所示。 这意味着用户要查看其他制造商,他必须手动放大。我怎样才能做到这一点? 我必须对我的代码进行哪些更改,或者有人可以指出我正确的方向吗?在遵循@Andy 建议的类似问题的答案后,我已将代码更新为
public void onMapReady(GoogleMap googleMap) {
final String TAG = ItemisedMapsActivity.class.getSimpleName();
StringRequest strReq = new StringRequest(Request.Method.POST, Link.URL_PROVIDER, response -> {
Log.d(TAG, "Response: " + response);
try {
JSONObject jsonObject;
JSONObject drinkObject = new JSONObject(response);
JSONArray jsonArray = drinkObject.getJSONArray("vendors");
for(int i=0; i<jsonArray.length();i++){
jsonObject = jsonArray.getJSONObject(i);
LatLng vnr = new LatLng(jsonObject.getDouble("latitude"), jsonObject.getDouble("longitude"));
MarkerOptions vnrMarker = new MarkerOptions();
vnrMarker.position(vnr);
vnrMarker.title(jsonObject.getString("name"));
vnrMarker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
markerList.add(vnrMarker);
}
showAllMarkers(googleMap);
} catch (JSONException e) {
e.printStackTrace();
}
}, error -> {
Log.e(TAG, "Request Error: " + error.getMessage());
Toast.makeText(this, " An error has occurred "+error.getMessage(), Toast.LENGTH_LONG).show();
}) {
@Override
protected Map<String, String> getParams() {
// Posting params to register url
Map<String, String> params = new HashMap<>();
params.put("town", town);
return params;
}
};
// Adding request to request queue
RequestQueue providerRequestQue = Volley.newRequestQueue(this);
providerRequestQue.add(strReq);
}
private void showAllMarkers(GoogleMap googleMap) {
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (MarkerOptions m : markerList) {
builder.include(m.getPosition());
}
LatLngBounds bounds = builder.build();
int width = getResources().getDisplayMetrics().widthPixels;
int height = getResources().getDisplayMetrics().heightPixels;
int padding = (int) (width * 0.30);
// Zoom and animate the google map to show all markers
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, width, height, padding);
googleMap.animateCamera(cu);
}
解决方案
经过多次搜索并遵循评论中的一些有用建议,我解决了这个问题,这是可以工作的代码,以防将来有人遇到同样的问题。地图准备好后,使用 volley 检索发球信息,然后遍历响应数组并将标记添加到标记选项数组列表
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
banPoint =BitmapDescriptorFactory.fromResource(R.drawable.pincode);
final String TAG = ItemisedMapsActivity.class.getSimpleName();
StringRequest strReq = new StringRequest(Request.Method.POST, Link.URL_PROVIDER, response -> {
try {
JSONObject jsonObject;
JSONObject drinkObject = new JSONObject(response);
JSONArray jsonArray = drinkObject.getJSONArray("vendors");
for(int i=0; i<jsonArray.length();i++){
jsonObject = jsonArray.getJSONObject(i);
LatLng vnr = new LatLng(jsonObject.getDouble("latitude"), jsonObject.getDouble("longitude"));
MarkerOptions vnrMarker = new MarkerOptions();
vnrMarker.position(vnr);
vnrMarker.title(jsonObject.getString("name"));
vnrMarker.snippet("Mama fua service provider");
vnrMarker.icon(banPoint);
markerList.add(vnrMarker);
}
showAllMarkers(mMap);
} catch (JSONException e) {
e.printStackTrace();
}
}, error -> {
Log.e(TAG, "Request Error: " + error.getMessage());
Toast.makeText(this, " An error has occurred "+error.getMessage(), Toast.LENGTH_LONG).show();
}) {
@Override
protected Map<String, String> getParams() {
// Posting params to register url
Map<String, String> params = new HashMap<>();
params.put("town", town);
return params;
}
};
// Adding request to request queue
RequestQueue providerRequestQue = Volley.newRequestQueue(this);
providerRequestQue.add(strReq);
}
然后创建一个方法来计算所有标记的位置并缩放到它们的点,在地图中显示所有标记
private void showAllMarkers(GoogleMap mMap) {
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (MarkerOptions m : markerList) {
builder.include(m.getPosition());
// add the parkers to the map
mMap.addMarker(m);
}
LatLngBounds bounds = builder.build();
int width = getResources().getDisplayMetrics().widthPixels;
int height = getResources().getDisplayMetrics().heightPixels;
int padding = (int) (width * 0.30);
// Zoom and animate the google map to show all markers
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, width, height, padding);
mMap.animateCamera(cu);
}
推荐阅读
- java - 如何使用 KafkaConsumer java SDK 使用来自 AWS MSK 集群的事件表单?
- django - 在 Django 模板中应用本地时区
- vue.js - 无法使用 'sudo npm uninstall @vue/cli -g' 来解除 vue.js
- azure-devops - Azure DevOps YAML 管道从传递的变量中删除引号和双反斜杠
- amazon-web-services - 如何通过 terraform 为 S3 存储桶添加公共权限
- r - 从 R 脚本渲染时如何将值传递给 Rmd 文件?
- arduino - 我可以让 2 个 L9637 iso9141 单线收发器与 ESP32 相互通信吗?
- sybase - SQL Anywhere 数据库 FLUSH() 来更新应用程序 UI?
- java - gradle shadowJar 任务找不到类 org.apache.tools.zip.ZipOutputStream
- javascript - 使用 asp.net 获取笔记本电脑/PC 的网络位置描述