从 android Java 中的凌空休息向 google 地图 API 添加多个标记
Adding multiple markers to google maps API from volley repose in android Java
我是 Google 地图 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);
}
我是 Google 地图 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();
}
}
有了这个,我已经能够添加标记,但它们都被组合在一起,形成了一个完整的世界页面地图,如此处所示
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);
}