簡體   English   中英

加載程序不會更新片段內的地圖標記

[英]Loader doesn't update map markers inside a fragment

我正在嘗試使用加載程序來更新地圖上的標記。 下面是我的代碼。 問題是,即使我在刷新標記數據庫的應用程序上單擊刷新按鈕時,標記也不會出現。 這是實現此加載程序的正確方法嗎?

package com.example.genexli.policev3;

import android.Manifest;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.genexli.policev3.data.DataContract;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;


/**
 * A fragment for the map
 */


public class MapFragment extends Fragment implements OnMapReadyCallback,
        LoaderManager.LoaderCallbacks<Cursor>{

    MapView m;
    GoogleMap map;

    Cursor hotspots;
    Cursor crimes;
    Cursor police;

    // three instance variable cursors: hotspots, crime and police.



    @Override
    public void onActivityCreated(Bundle savedInstanceState) {


        // save as an instance variable.

        // init three loaders with different IDs. 0 = Hotspots, 1 = crime, 2 = police.
        getLoaderManager().initLoader(0, null, this);
        getLoaderManager().initLoader(1, null, this);
        getLoaderManager().initLoader(2, null, this);

        super.onActivityCreated(savedInstanceState);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // inflate and return the layout
        View v = inflater.inflate(R.layout.fragment_map, container,
                false);

        // this creates the mapview.
        m = (MapView) v.findViewById(R.id.mapView);
        m.onCreate(savedInstanceState);
        // in here it should load the markers using those instance variable cursors.
        m.getMapAsync(this);

        return v;
    }

    // Some required methods for MapView to function correctly.
    @Override
    public void onResume() {
        m.onResume();
        super.onResume();
    }

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

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

    @Override
    public void onMapReady (GoogleMap googleMap){
        map = googleMap;
        if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            Log.v("Map", "location set");
            map.setMyLocationEnabled(true);
            map.getUiSettings().setMyLocationButtonEnabled(true);
        } else {
            requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
        }
        CameraPosition target = CameraPosition.builder()
                .target(new LatLng(36.1447034, -86.8032))
                .zoom(13)
                .build();
        CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(target);
        map.moveCamera(cameraUpdate);
        map.getUiSettings().setZoomControlsEnabled(true);
        map.clear();

        // map put hotspot markers
        // map put crime markers
        // map put police markers
        if (hotspots != null) {
            if(hotspots.moveToFirst())
            // put in new marker.
            do {
                int idx_lat = hotspots.getColumnIndexOrThrow("latitude");
                int idx_long = hotspots.getColumnIndexOrThrow("longitude");
                Log.v("lat and long", hotspots.getDouble(idx_lat) + " " + hotspots.getDouble(idx_long));
                LatLng ltlng = new LatLng(hotspots.getDouble(idx_lat), hotspots.getDouble(idx_long));
                map.addMarker(new MarkerOptions().position(ltlng).title("Hotspot"));
            } while (hotspots.moveToNext());
        }

        if (crimes != null) {
            if(crimes.moveToFirst())
            // put in new marker.
            do {
                int idx_lat = crimes.getColumnIndexOrThrow("latitude");
                int idx_long = crimes.getColumnIndexOrThrow("longitude");
                Log.v("lat and long", crimes.getDouble(idx_lat) + " " + crimes.getDouble(idx_long));

                LatLng ltlng = new LatLng(crimes.getDouble(idx_lat), crimes.getDouble(idx_long));
                map.addMarker(new MarkerOptions().position(ltlng).title("Crime"));
            } while (crimes.moveToNext());
        }

        if (police != null) {
            if(police.moveToFirst())
            // put in new marker.
            do {
                int idx_lat = police.getColumnIndexOrThrow("latitude");
                int idx_long = police.getColumnIndexOrThrow("longitude");
                Log.v("lat and long", police.getDouble(idx_lat) + " " + police.getDouble(idx_long));

                LatLng ltlng = new LatLng(police.getDouble(idx_lat), police.getDouble(idx_long));
                map.addMarker(new MarkerOptions().position(ltlng).title("Police")
                    .icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher)));
            } while (police.moveToNext());
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        // do I need to update all cursors when it is started?
    }

    @Override
    public Loader<Cursor> onCreateLoader(int i, Bundle b) {

        // switch here to assign loader to different cursor based off of the call.
        switch(i) {
            //0 = Hotspots, 1 = crime, 2 = police.
            case 0: {
                return new CursorLoader(getActivity(), DataContract.HotspotsEntry.CONTENT_URI,
                        null, null, null, null);
            }
            case 1:  {
                return new CursorLoader(getActivity(), DataContract.CrimesEntry.CONTENT_URI,
                        null, null, null, null);
            }
            case 2: {
                return new CursorLoader(getActivity(), DataContract.PatrolsEntry.CONTENT_URI,
                        null, null, null, null);
            }
        }
        return null;
    }

    // on loadfinished swaps loaders: use getId() to find the right cursor
    public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
        int id = cursorLoader.getId();

        switch(id) {
            case 0: {
                hotspots = cursor;

                break;
            }
            case 1: {
                crimes = cursor;
            }
            case 2: {
                police = cursor;
            }
        }

        // reload the map?
    }

    //onloaderreset
    @Override
    public void onLoaderReset(Loader<Cursor> cursorLoader) {
        hotspots = null;
        crimes = null;
        police = null;
    }


}

您僅在onMapReady上繪制標記。 您可以將圖形移動到單獨的方法:

private void redrawMap() {
    if (map != null) {
        map.clear();

        // map put hotspot markers
        // map put crime markers
        // map put police markers
        if (hotspots != null) {
            if(hotspots.moveToFirst())
            // put in new marker.
            do {
                int idx_lat = hotspots.getColumnIndexOrThrow("latitude");
                int idx_long = hotspots.getColumnIndexOrThrow("longitude");
                Log.v("lat and long", hotspots.getDouble(idx_lat) + " " + hotspots.getDouble(idx_long));
                LatLng ltlng = new LatLng(hotspots.getDouble(idx_lat), hotspots.getDouble(idx_long));
                map.addMarker(new MarkerOptions().position(ltlng).title("Hotspot"));
            } while (hotspots.moveToNext());
        }

        if (crimes != null) {
            if(crimes.moveToFirst())
            // put in new marker.
            do {
                int idx_lat = crimes.getColumnIndexOrThrow("latitude");
                int idx_long = crimes.getColumnIndexOrThrow("longitude");
                Log.v("lat and long", crimes.getDouble(idx_lat) + " " + crimes.getDouble(idx_long));

                LatLng ltlng = new LatLng(crimes.getDouble(idx_lat), crimes.getDouble(idx_long));
                map.addMarker(new MarkerOptions().position(ltlng).title("Crime"));
            } while (crimes.moveToNext());
        }

        if (police != null) {
            if(police.moveToFirst())
            // put in new marker.
            do {
                int idx_lat = police.getColumnIndexOrThrow("latitude");
                int idx_long = police.getColumnIndexOrThrow("longitude");
                Log.v("lat and long", police.getDouble(idx_lat) + " " + police.getDouble(idx_long));

                LatLng ltlng = new LatLng(police.getDouble(idx_lat), police.getDouble(idx_long));
                map.addMarker(new MarkerOptions().position(ltlng).title("Police")
                    .icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher)));
            } while (police.moveToNext());
        }
    }
}

然后在onLoadFinished上調用它

public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
    int id = cursorLoader.getId();

    switch(id) {
        case 0: {
            hotspots = cursor;

            break;
        }
        case 1: {
            crimes = cursor;
        }
        case 2: {
            police = cursor;
        }
    }

    redrawMap();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM