首页 > 解决方案 > 从 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);
}

除了标记未显示在地图上之外,这确实会放大到标记的实际位置 新输出

标签: javaandroidgoogle-maps-api-3android-volley

解决方案


经过多次搜索并遵循评论中的一些有用建议,我解决了这个问题,这是可以工作的代码,以防将来有人遇到同样的问题。地图准备好后,使用 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);
}

推荐阅读