如何使用来自JSON的lat long放置多个标记

时间:2015-07-22 12:41:53

标签: android google-maps

在我的应用程序中,我想使用JSON中的纬度和经度显示多个标记,但是当我运行应用程序时,它只显示一个标记,其他标记未显示。请帮忙 - 这是我的代码:

class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {
        private ProgressDialog progressDialog; // class variable

        private void showProgressDialog(String title, String message)
        {
            progressDialog = new ProgressDialog(getActivity());

            progressDialog.setTitle(title); // set title 

            progressDialog.setMessage(message); // set message

            progressDialog.setCancelable(false);

            progressDialog.show();
        } 

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            showProgressDialog("Loading...", "Please wait for few seconds");
            //          dialog = new ProgressDialog(getActivity().this);
            //          dialog.setMessage("Loading, please wait");
            //          dialog.setTitle("Connecting server");
            //  dialog.show();
            //  dialog.setCancelable(false);
        }

        @Override
        protected Boolean doInBackground(String... urls) {
            try {


                //  Date a = new Date();
                //  a.setTime(System.currentTimeMillis()-(60*60*1000));
                //  Log.e("onehourback",""+a);*/
                //------------------>>
                HttpGet httppost = new HttpGet(urls[0]);
                HttpClient httpclient = new DefaultHttpClient();
                HttpResponse response = httpclient.execute(httppost);

                // StatusLine stat = response.getStatusLine();
                int status = response.getStatusLine().getStatusCode();

                if (status == 200) {
                    HttpEntity entity = response.getEntity();
                    String data = EntityUtils.toString(entity); 
                    JSONObject jsono = new JSONObject(data);
                    JSONArray jarray = jsono.getJSONArray("SingleIMEs");

                    for (int i = 0; i < jarray.length(); i++) {
                        JSONObject object = jarray.getJSONObject(i);
                        //  Log.e("object",""+object);
                        /*try {
                        array.add(jarray.getJSONObject(i));

                        Log.e("array",""+array);
                        } catch (JSONException e)
                        {
                            e.printStackTrace();
                        }*/

                        for(int j=0; j < 1;j++)
                        {
                            latvalue=object.getString("Latitude");
                            longvalue=object.getString("Longitude");
                            latt=Double.parseDouble(latvalue);
                            lng=Double.parseDouble(longvalue);
                            Log.e("lat",""+latt);
                            Log.e("lon",""+lng);




                        }
                    }
                }
                //}
                return true;


                //------------------>>

            } catch (ParseException e1) {
                e1.printStackTrace();
            }

            catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return false;
        }



        protected void onPostExecute(Boolean result) {
            //dialog.cancel();
            if(progressDialog != null && progressDialog.isShowing())
            {
                progressDialog.dismiss();
            }
            /*  adapter.notifyDataSetChanged();
            if(result == false)
                Toast.makeText(getActivity().getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
             */
        }

    }
    private void setUpMapIfNeeded(View inflatedView) { 
        mMap = ((MapView) inflatedView.findViewById(R.id.mapView)).getMap(); 
        mMap.clear();

        mMap.addMarker(new MarkerOptions().position(new LatLng(latt,lng)).title("eee")); 
        Log.e("lat",""+latt);
        Log.e("lon",""+lng);

        mMap.setMyLocationEnabled(true); 



        if (mMap != null) { 
            //setUpMap(); 
            mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() { 

                @Override 
                public void onMyLocationChange(Location arg0) { 
                    // TODO Auto-generated method stub 
                    LatLng latLng = new LatLng(arg0.getLatitude(), arg0.getLongitude()); 
                    mMap.addMarker(new MarkerOptions().position(new LatLng(arg0.getLatitude(), arg0.getLongitude())).title("WePOP")); 

                    //   mMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)).position( new LatLng(Double.parseDouble(datas.get("Lat")), Double.parseDouble(datas.get("Long"))))); 
                    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
                    //mMap.animateCamera(CameraUpdateFactory.zoomTo(15)); 


                } 
            }); 

        } 
    } 
    @Override 
    public void onResume() { 
        super.onResume(); 
        mMapView.onResume(); 
    } 

    @Override 
    public void onPause() { 
        super.onPause(); 
        mMapView.onPause(); 
    } 

    @Override 
    public void onDestroy() { 
        mMapView.onDestroy(); 
        super.onDestroy(); 
    } 

}

1 个答案:

答案 0 :(得分:0)

看起来您在此代码中多次覆盖了您的latt和lng变量:

for(int j=0; j < 1;j++)
{
    latvalue=object.getString("Latitude");
    longvalue=object.getString("Longitude");
    latt=Double.parseDouble(latvalue);
    lng=Double.parseDouble(longvalue);
    Log.e("lat",""+latt);
    Log.e("lon",""+lng);
}

我建议你创建一个List,它包含某种包含latt和lng信息的对象(比如列表位置)。 然后修改setUpMapIfNeeded中的代码以循环遍历所有LatLng。像这样:

for(LatLng latlng: positions) {
    mMap.addMarker(new MarkerOptions().position(new LatLng(latlng.getLat(),latlng.getLng())).title("eee")); 
    Log.e("Added marker at lat: " + latlng.getLat() + ", lng: " + latlng.getLng());
}

注意:自从我使用了Android和谷歌地图以来已经有一段时间了,所以我在这里写的代码可能并不完美 - 但这个想法应该是好的: - )