Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在从json数组解析的google地图上添加多个标记?_Android_Json_Google Maps - Fatal编程技术网

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

Android 如何在从json数组解析的google地图上添加多个标记?,android,json,google-maps,Android,Json,Google Maps,我已经编写了一个程序,在这个程序中,点击按钮我就可以从json获得附近的ATM机。这是链接 我想在谷歌地图上标出自动取款机,但问题是地图上只显示了最后一台自动取款机 代码:获取atm名称、纬度、经度和邻近区域的方法 public void showAtm(){ String getAtmUrl = "https://maps.googleapis.com/maps/api/place/nearbysearch/json? location="+lat+","+

我已经编写了一个程序,在这个程序中,点击按钮我就可以从json获得附近的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);
}
public void showtam(){
字符串getAtmUrl=
"https://maps.googleapis.com/maps/api/place/nearbysearch/json? 
位置=“+lat+”,“+lng+”&半径=1000&类型=atm&传感器=true
&key=AIzaSyA8szrI9Ue4EwyUwTgz7Nk0c39qMal0pN4”;
试一试{
OkHttpClient OkHttpClient=新的OkHttpClient();
Request Request=newrequest.Builder().url(getAtmUrl.build();
Call Call=okHttpClient.newCall(请求);
call.enqueue(新回调(){
@凌驾
公共void onFailure(调用调用,IOE异常){
e、 printStackTrace();
映射\u活动。this.runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
Toast.makeText(getBaseContext(),“对atm的请求
位置失败”,Toast.LENGTH_SHORT).show();
}
});
}
@凌驾
公共void onResponse(调用、响应)抛出
IOException{
Log.i(“响应”,“onResponse():”+响应);
字符串结果=response.body().String();
Log.i(“结果”,结果);
试一试{
JSONObject JSONObject=新JSONObject(结果);
String resultData=jsonObject.getString(“结果”);
JSONArray urlDetails=新的JSONArray(resultData);
对于(int i=0;i
我如何实现上述目标,有人能建议我吗?
谢谢

从方法moveAtmMap中删除此行:

fragment.getMap().clear();

这是第一次,在onResponse中为循环写入它。

从方法moveAtmMap中删除这一行:

fragment.getMap().clear();

这是第一次在onResponse中为循环编写它。

您已经将该方法编写为

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);
        }
    });
}

如果要从以前的web服务命中中清除标记,请在开始为新服务命中添加标记之前执行此操作,就像在
for
循环之前一样。

您已将此方法编写为

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);
        }
    });
}
如果要从以前的web服务命中中清除标记,请在开始为新服务命中添加标记之前执行此操作,就像在
for
循环之前一样。

尝试此简单代码

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);

        }
for(inti=0;i试试这个简单的代码

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);

        }

for(int i=0;iWhy您建议我在for loop之前这样做??第一次,您必须删除所有以前添加的标记,然后添加新的标记b