Android设备上的地图只显示来自网址的一个标记,但在我使用Toast时显示所有值

时间:2016-09-10 09:03:46

标签: android json google-maps google-maps-markers

我在谷歌上尝试了很多变化但我的问题没有得到解决。我有一个包含以下数据的URL。在地图上它应显示多个标记,因为我有9个坐标。但它显示一个标记,其坐标为最后的响应值。任何帮助都可以得到赞赏。

package com.example.charan.markermap;

import android.app.ProgressDialog;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;

public class Maps3 extends FragmentActivity {

    private GoogleMap mMap;

    ArrayList<HashMap<String, Double>> locationList = new ArrayList<HashMap<String, Double>>();
    JSONArray locations = new JSONArray();
    int json1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        new TaskRead().execute();
    }


    public class TaskRead extends AsyncTask<Void, Void, Void> implements OnMapReadyCallback {

        ProgressDialog pg;
        Double latitude, longitude;

        protected void onPreExecute() {
            SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map);
            mapFragment.getMapAsync(this);
            pg = ProgressDialog.show(Maps3.this, "Please Wait", "Conecting to Server");
            pg.setCancelable(true);
        }


        protected Void doInBackground(Void... params) {
            try {

                String url = "http://192.168.1.33:8282/DocFinderServices/doctorService/doctorsList";

                HttpClient client = new DefaultHttpClient();
                HttpGet httpget = new HttpGet(url);
                HttpResponse response = client.execute(httpget);
                HttpEntity entity = response.getEntity();
                InputStream inputStreamResponse = entity.getContent();

                String str = convertStreamToString(inputStreamResponse);

                if (str != null) {
                    try {
                        JSONObject jsonObj = new JSONObject(str);

                        // Getting JSON Array node
                        locations = jsonObj.getJSONArray("response");
                        json1 = jsonObj.getInt("status");

                        // looping through All records
                        for (int i = 0; i < locations.length(); i++) {
                            JSONObject c = locations.getJSONObject(i);

                            latitude = c.getDouble("lat");
                            longitude = c.getDouble("log");

                            // tmp hashmap for single contact
                            HashMap<String, Double> location = new HashMap<>();

                            // adding each child node to HashMap key => value
                            location.put("latitude", latitude);
                            location.put("longitude", longitude);
                            locationList.add(location);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        protected void onPostExecute(Void gmap) {
            mMap.clear();
            Marker[] allMarkers = new Marker[locationList.size()];
            Toast.makeText(Maps3.this, "The list size is " + locationList.size(), Toast.LENGTH_SHORT).show();

            for (int i = 0; i < locationList.size(); i++) {

                if (mMap != null && json1 == 200) {
                    for (int j = 0; j < locationList.size(); j++) {
                        LatLng latLng = new LatLng(latitude, longitude);
                        allMarkers[i] = mMap.addMarker(new MarkerOptions()
                                .position(latLng)
                                .icon(BitmapDescriptorFactory.fromResource(R.drawable.ok)));
                        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 14.0f));
                        mMap.getUiSettings().isZoomGesturesEnabled();
                    }

                } else {
                    Toast.makeText(Maps3.this, "Oops..their is no map........", Toast.LENGTH_SHORT).show();
                }
            }
            pg.dismiss();

        }

        public String convertStreamToString(InputStream is) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();
            String line = null;
            try {
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            return sb.toString();
        }

        @Override
        public void onMapReady(GoogleMap googleMap) {
            mMap = googleMap;

        }
    }
}

{{1}}

2 个答案:

答案 0 :(得分:2)

在回调SELECT SUBSTRING_INDEX(GROUP_CONCAT(content ORDER BY IF(language='nl',1,IF(language='en',2,3))),',',1) FROM block GROUP BY title; 之后尝试执行TaskRead

onMapReady()

@Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; // Set Zoom Controls and Gestures on map mMap.getUiSettings().setZoomControlsEnabled(true) mMap.getUiSettings().setZoomGesturesEnabled(true) new TaskRead().execute(); } 内实施OnMapReadyCallback并在FragmentActivity方法中初始化您的SupportMapFragment,如下所示。

onCreate

尝试在地图上设置缩放控件和手势并尝试放大或缩小并查看,是否在地图上添加了其他标记?

检查我更新的public class Maps3 extends FragmentActivity implements OnMapReadyCallback { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_maps); SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); } 方法。

答案 1 :(得分:0)

我刚刚将LatLng latLng = new LatLng(latitude, longitude);更改为获取密钥对值的新值  LatLng latLng = new LatLng(Double.valueOf(locationList.get(j).get("latitude")), Double.valueOf(locationList.get(j).get("longitude")));。现在它正在发挥作用。