2016-07-12 61 views
0

我正在嘗試使用加載程序來更新地圖上的標記。以下是我的代碼。問題在於即使我點擊刷新標記數據庫的應用程序上的刷新按鈕,標記也不會出現。這是一個適當的方式來實現這個裝載機?加載程序不更新片段內的地圖標記

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


} 
+0

我們真的需要檢查200行代碼?我相信你可以指出問題出在哪裏 – agustin

回答

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