【问题标题】:Markers getting disabled and getting enable when the latitude nad longitude is updated更新纬度和经度时标记被禁用和启用
【发布时间】:2015-07-29 06:03:46
【问题描述】:

在我的应用程序中,当纬度和经度在 JSON 中更新时,我的应用程序正在绘制来自 Json 的纬度和经度的标记,它工作正常并且标记得到更新。但有时所有标记都会被禁用一秒钟并启用。我不希望所有标记在任何时候都被禁用,请帮我修复它,我的代码如下所示。

@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

    } 


}

【问题讨论】:

    标签: android json google-maps google-play android-maps-v2


    【解决方案1】:

    我认为这是一些内存问题...

    尝试重写此方法...

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

    【讨论】:

    • @Rishad Appat : 在覆盖低内存方法后我仍然遇到同样的问题
    【解决方案2】:

    在这个 for loo 中,您引用了相同的标记变量:

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

    所以只需删除 marker = 并制作它

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

    希望这会奏效

    【讨论】:

    • Gaurav Polekar:如果我使用 marker.remove() 应用程序崩溃如何删除标记任何示例代码?
    • 不建议你不要写 marker = mMap.addMarker();只需使用 mmap.addMarker();如果你想使用 marker.remove();然后使用 if(marker != null) marker.remove();
    • Gaurav Polekar:是的,我用作 mMap.addMarker(new MarkerOptions().position(position).icon(BitmapDescriptorFactory .fromResource(R.drawable.buspng)).title(ime1)); -- 但我仍然遇到标记有时会禁用和启用的问题
    • 我尝试了类似的代码,它对我来说工作正常,尝试调试它并检查标记在什么时候被禁用
    猜你喜欢
    • 2023-03-26
    • 1970-01-01
    • 2016-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-19
    相关资源
    最近更新 更多