如何在从json数组解析的谷歌地图上添加多个标记?

时间:2016-04-05 09:37:03

标签: android json google-maps

我写了一个程序,按钮点击我从json获取附近的atms。这是链接

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=19.052696,72.8713694&radius=1000&types=atm&sensor=true&key=AIzaSyA8szrI9Ue4EwyUwTgz7Nk0c39qMal0pN4

我想在谷歌地图上绘制atms,但问题是只有最后一个atm正在地图上显示

代码:获取atm名称,纬度,经度和附近的方法

 public void showAtm(){
    String getAtmUrl =   
    "https://maps.googleapis.com/maps/api/place/nearbysearch/json? 
    location="+lat+","+lng+"&radius=1000&types=atm&sensor=true
    &key=AIzaSyA8szrI9Ue4EwyUwTgz7Nk0c39qMal0pN4";
    try{
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(getAtmUrl).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
                Map_Activity.this.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        Toast.makeText(getBaseContext(), "Request to atm 
                        locations failed", Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws  
                IOException {
                Log.i("response ", "onResponse(): " + response);
                String result = response.body().string();
                Log.i("result",result);
                try{
                    JSONObject jsonObject = new JSONObject(result);
                    String resultData = jsonObject.getString("results");
                    JSONArray urlDetails = new JSONArray(resultData);
                    for (int i = 0 ; i < urlDetails.length(); i++){
                        JSONObject json = urlDetails.getJSONObject(i);
                        geometry = json.getString(GOEMETRY);
                        vicinity = json.getString(VICINITY);
                        JSONObject jsonGeometry = new JSONObject(geometry);
                        String geoLocation = 
                        jsonGeometry.getString(LOCATION);
                        JSONObject jsonLatLng = new JSONObject(geoLocation);
                        atmLat = jsonLatLng.getDouble(LATITUDE);
                        atmLong = jsonLatLng.getDouble(LONGITUDE);
                        atmName = json.getString(ATM_NAME);
                        Log.i("JsonArrayAtm", "" + atmName);
                        Log.i("JsonArrayGeometry",geometry);
                        Log.i("LatLong",""+atmLat+" , "+atmLong);
                        Log.i("Vicinity", vicinity);
                        runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            moveAtmMap(atmLat ,atmLong );
                        }
                    });
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
    }catch (Exception e){
        e.printStackTrace();
    }
}


 ///////////////////////////// atm locations map ///////////////////
private void moveAtmMap(Double amtLatitude,Double atmLongitude){
    fragment.getMap().clear();
    CameraPosition position = CameraPosition.builder()
            .target(new LatLng(amtLatitude, atmLongitude))
            .zoom(16f)
            .bearing(0.0f)
            .tilt(0.0f)
            .build();
    String msg = amtLatitude+ ", " + atmLongitude;
    LatLng latLng = new LatLng(amtLatitude, atmLongitude);
    fragment.getMap().addMarker(new MarkerOptions()
            .position(latLng));
    fragment.getMap().setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter()  
       {
        @Override
        public View getInfoWindow(Marker marker) {
            return null;
        }

        @Override
        public View getInfoContents(Marker marker) {
            View v = getLayoutInflater().inflate(R.layout.atm_custom_window, 
            null);
            TextView atmHeader = (TextView) v.findViewById(R.id.atmName);
            TextView atmLocation = (TextView) 
            v.findViewById(R.id.atmLocation);
            atmHeader.setText(atmName);
            atmLocation.setText(vicinity);
            return v;
        }
    });
    fragment.getMap().setMapType(GoogleMap.MAP_TYPE_NORMAL);
    fragment.getMap().setTrafficEnabled(true);
    fragment.getMap().setMyLocationEnabled(true);
    fragment.getMap().animateCamera(CameraUpdateFactory
            .newCameraPosition(position), null);
}

我如何实现上述目标,有人能建议我吗? 感谢

3 个答案:

答案 0 :(得分:1)

从方法moveAtmMap中删除此行:

fragment.getMap().clear();

第一次,在onResponse中循环写入for循环。

答案 1 :(得分:1)

您已将方法编写为,

private void moveAtmMap(Double amtLatitude,Double atmLongitude){
    fragment.getMap().clear();
    ...
}

因此,每次调用此方法时,它都会清除所有先前的标记,并且最终只会显示最后一个标记。

修改

for (int i = 0 ; i < urlDetails.length(); i++){
    JSONObject json = urlDetails.getJSONObject(i);
    String geometry = json.getString(GOEMETRY);
    String vicinity = json.getString(VICINITY);
    JSONObject jsonGeometry = new JSONObject(geometry);
    String geoLocation = jsonGeometry.getString(LOCATION);
    JSONObject jsonLatLng = new JSONObject(geoLocation);
    double atmLat = jsonLatLng.getDouble(LATITUDE);
    double atmLong = jsonLatLng.getDouble(LONGITUDE);
    String atmName = json.getString(ATM_NAME);
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            moveAtmMap(atmLat, atmLong, atmName, vicinity, geometry);
        }
    });
}

并改变方法,

private void moveAtmMap(Double amtLatitude,Double atmLongitude, String name, String vicinity, String geometry)

如果要清除之前网络服务点击中的标记,请在开始为新服务点击添加标记之前执行此操作,例如在for循环之前。

答案 2 :(得分:0)

试试这个简单的代码,

for(int i=0;i<jsonArray.length();i++){
            MarkerOptions markerOptions;
            markerOptions = new MarkerOptions().position(new LatLng(lattitude,
                           longitude)
            ).title("Title").snippet("This is snippet");

            markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_icon));
            marker = googleMap.addMarker(markerOptions);

        }

只需用您的值替换lattitude,经度即可。 如果你想拥有每个标记的实例,那么你可以放置每个标记&#34;使用key作为标记id将对象转换为hashmap。让我知道您的反馈意见。

注意:删除此行 - fragment.getMap()。clear();因为每次编译器进入循环时它都会清除映射,它只需要最后一个对象。这就是现在发生的事情。