-1

In my application am plotting the markers from the latitude and longitude from Json when the latitude and longitude is updated in JSON it working fine and the markers get updated.But at sometime the all markers getting disable for a second and enabling.I don't want all the markers to get disable at any time please help me to fix it my code is given below.

@Override 

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
            Bundle savedInstanceState) { 
        rootView = inflater.inflate(R.layout.fragment_layout_one, container, false); 


        MapsInitializer.initialize(getActivity()); 

        mMapView = (MapView)rootView.findViewById(R.id.mapView); 
        mMapView.onCreate(mBundle); 


        MapsInitializer.initialize(getActivity()); 

        if (android.os.Build.VERSION.SDK_INT > 9) { 
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() 
            .permitAll().build(); 
            StrictMode.setThreadPolicy(policy); 
        } 


        /* handler = new Handler();
        handler.postDelayed(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                 new DownloadJSON().execute(); 
                    setUpMapIfNeeded(rootView); 

                 Toast.makeText(getActivity(), "Data Updated!!!! ", Toast.LENGTH_SHORT).show();
                 Log.e("Data in Log", "");
            }
        }, 1000);
        */
         final Handler handler = new Handler();
            timer = new Timer();
            TimerTask doAsynchronousTask = new TimerTask() {
                @Override
                public void run() {
                    handler.post(new Runnable() {
                        public void run() {
                            //mMap.clear();
                         Toast.makeText(getActivity(), "Data Updated!!!! ", Toast.LENGTH_SHORT).show();
                         new DownloadJSON().execute(); 
                            setUpMapIfNeeded(rootView); 

                        }
                    });
                }
            };
            timer.schedule(doAsynchronousTask, 20000, 20000);


        new DownloadJSON().execute(); 
        setUpMapIfNeeded(rootView);

        /*LocationManager locman = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
        //locman.requestLocationUpdates(minTime, minDistance, criteria, intent);
        locman.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, this);*/




        return rootView; 
    } 

    private void setUpMapIfNeeded(View inflatedView) { 

        if (mMap == null) { 
            mMap = ((MapView) inflatedView.findViewById(R.id.mapView)).getMap(); 
            mMap.setMyLocationEnabled(true); 

            Location myLocation = mMap.getMyLocation(); 
            if (mMap != null) {  
                //mMap.clear();

                // setUpMap(); 
                mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() { 

                    @Override 
                    public void onMyLocationChange(Location arg0) { 

                        // TODO Auto-generated method stub 
                        final LatLngBounds.Builder builder = new LatLngBounds.Builder(); 
                    mMap.clear();

                        for (int i = 0; i < arraylist1.size(); i++) { 
                            final LatLng position = new LatLng(Double 
                                    .parseDouble(arraylist1.get(i).get("Latitude")), 
                                    Double.parseDouble(arraylist1.get(i).get( 
                                            "Longitude"))); 
                            String ime1 = arraylist1.get(i).get("IME");
                            final MarkerOptions options = new MarkerOptions() 
                            .position(position); 
                            //mMap.addMarker(options); 
                            //mMap.addMarker(options.icon(BitmapDescriptorFactory .fromResource(R.drawable.buspng)).title(ime1));
                              marker=mMap.addMarker(new MarkerOptions().position(position).icon(BitmapDescriptorFactory .fromResource(R.drawable.buspng)).title(ime1));


                            //options.title(ime1);
                            builder.include(position); 

                        } 
                        LatLng latLng = new LatLng(arg0.getLatitude(), arg0 
                                .getLongitude()); 
                        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                        mMap.setMyLocationEnabled(true); 

                        mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
                        // mMap.setOnMapClickListener(null); 
                        mMap.setOnMarkerClickListener(null); 

                        mMap.animateCamera(CameraUpdateFactory.zoomTo(9)); 
                    } 
                }); 

            } 
        } 
    } 

    /*  protected void retrieveAndAddCities() throws IOException { 
        HttpURLConnection conn = null; 
        final StringBuilder json = new StringBuilder(); 
        try { 

            URL url = new URL(SERVICE_URL); 
            conn = (HttpURLConnection) url.openConnection(); 
            InputStreamReader in = new InputStreamReader(conn.getInputStream()); 

            int read; 
            char[] buff = new char[1024]; 
            while ((read = in.read(buff)) != -1) { 
                json.append(buff, 0, read); 
            } 
        } catch (IOException e) { 
            Log.e(LOG_TAG, "Error connecting to service", e); 
            throw new IOException("Error connecting to service", e); 
        } finally { 
            if (conn != null) { 
                conn.disconnect(); 
            } 
        } 
        new DownloadJSON().execute(); 

    } */

    private class DownloadJSON extends AsyncTask<Void, Void, Void> { 
        String result=""; 
        @Override 
        protected void onPreExecute() { 
            super.onPreExecute(); 
        }

        @Override 
        protected Void doInBackground(Void... params) { 
            try { 
                arraylist1 = new ArrayList<HashMap<String, String>>(); 

                JSONParser jParser = new JSONParser(); 
                String result = ""; 

                json = jParser.getJSONFromUrl(SERVICE_URL); 

                try { 
                    //arraylist1.clear(); 

                    jsonarray = json.getJSONArray("SingleIMEs"); 
                    Log.d("Haaaaaaaaaaaa", "" + json); 

                    for (int i = 0; i < jsonarray.length(); i++) { 
                        Log.d("H11111111111111111111111111", 
                                "" + jsonarray.length()); 
                        map = new HashMap<String, String>(); 
                        json = jsonarray.getJSONObject(i); 

                        latitude = json.getDouble("Latitude"); 
                        longitude = json.getDouble("Longitude"); 
                        ime = json.getString("IME");

                        map.put("Latitude", json.getString("Latitude")); 
                        Log.e("CHECKLAT",""+json.getString("Latitude") );
                        map.put("Longitude", json.getString("Longitude")); 
                        Log.e("CHECKLONG",""+json.getString("Longitude") );

                        map.put("IME", json.getString("IME"));
                        arraylist1.add(map); 
                    } 

                } catch (JSONException e) { 
                    Log.e("log_tag", "Error parsing data " + e.toString()); 
                    result="Error"; 
                    e.printStackTrace(); 
                } 
            }catch(Exception e){ 
                result="Error"; 
            } 

            return null; 
        } 

        protected void onPostExecute(Void args) { 

            // mProgressDialog.dismiss(); 

        } 
    } 

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

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

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

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub

   /* Toast.makeText(getActivity(), "onLocationUpdated!!!", Toast.LENGTH_SHORT).show();
        Log.d("onLocationUpdated!!!","");
        new DownloadJSON().execute(); 
        setUpMapIfNeeded(rootView);*/

    }


    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderDisabled(String provider) {

        // TODO Auto-generated method stub

    } 


}
user3631500
  • 39
  • 1
  • 8

2 Answers2

0

I think it is some memory issue...

Try to override this method...

@Override
public void onLowMemory() {
    super.onLowMemory();
    mMapView.onLowMemory();
}
Rishad Appat
  • 1,786
  • 1
  • 15
  • 30
0

In this for loo you are refrencing to same marker variable:

marker=mMap.addMarker(new MarkerOptions().position(position).icon(BitmapDescriptorFactory .fromResource(R.drawable.buspng)).title(ime1));

for (int i = 0; i < arraylist1.size(); i++) { 
                            final LatLng position = new LatLng(Double 
                                    .parseDouble(arraylist1.get(i).get("Latitude")), 
                                    Double.parseDouble(arraylist1.get(i).get( 
                                            "Longitude"))); 
                            String ime1 = arraylist1.get(i).get("IME");
                            final MarkerOptions options = new MarkerOptions() 
                            .position(position); 
                            //mMap.addMarker(options); 
                            //mMap.addMarker(options.icon(BitmapDescriptorFactory .fromResource(R.drawable.buspng)).title(ime1));
                              marker=mMap.addMarker(new MarkerOptions().position(position).icon(BitmapDescriptorFactory .fromResource(R.drawable.buspng)).title(ime1));


                            //options.title(ime1);
                            builder.include(position); 

                        } 

So just remove marker = and make it

>     mMap.addMarker(new MarkerOptions().position(position).icon(BitmapDescriptorFactory
> .fromResource(R.drawable.buspng)).title(ime1));

Hope this will work

Gaurav
  • 3,615
  • 2
  • 27
  • 50
  • Gaurav Polekar:If I use marker.remove() the app getting crashed how to remove the marker any sample code? – user3631500 Jul 29 '15 at 07:10
  • no I suggest you to dont write marker = mMap.addMarker(); just use mMap.addMarker(); and if you want to use marker.remove(); then use in like if(marker != null) marker.remove(); – Gaurav Jul 29 '15 at 07:46
  • Gaurav Polekar:Yes i used as mMap.addMarker(new MarkerOptions().position(position).icon(BitmapDescriptorFactory .fromResource(R.drawable.buspng)).title(ime1)); -- but still i get the issue the markers are disabling and enabling at some time – user3631500 Jul 29 '15 at 07:50
  • I tried similar code it was working fine for me, try to debug it and check at what point markers get disable – Gaurav Jul 29 '15 at 07:59