加载程序不会更新片段内的地图标记

时间:2016-07-12 16:16:53

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

我正在尝试使用加载程序更新地图上的标记。以下是我的代码。问题是标记没有出现,即使我点击我的应用程序刷新标记数据库的刷新按钮。这是实现这个加载器的正确方法吗?

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


}

1 个答案:

答案 0 :(得分:0)

您只在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();
}