2016-09-20 77 views
1

我試圖在Android中使用谷歌地圖實用程序庫顯示熱圖點,沒有任何顯示在地圖上。我不知道我是否需要別的東西,我看過例子,但在我的情況下它不起作用。我正在使用片段。熱地圖不顯示在片段

public class MapFragment extends Fragment { 


MapView mMapView; 
private GoogleMap googleMap; 
private HeatmapTileProvider mProvider; 
protected LatLng mCenterLocation = new LatLng(39.7392, -104.9903); 

public MapFragment() { 
    // Required empty public constructor 
} 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View rootView = inflater.inflate(R.layout.fragment_map, container, false); 

    mMapView = (MapView) rootView.findViewById(R.id.mapView); 
    mMapView.onCreate(savedInstanceState); 
    mMapView.onResume(); // needed to get the map to display immediately 
    try { 
     MapsInitializer.initialize(getActivity().getApplicationContext()); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    mMapView.getMapAsync(new OnMapReadyCallback() { 
     @Override 
     public void onMapReady(GoogleMap mMap) { 
      googleMap = mMap; 

      // For showing a move to my location button 
      if (ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
       // TODO: Consider calling 
       // ActivityCompat#requestPermissions 
       // here to request the missing permissions, and then overriding 
       // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
       //           int[] grantResults) 
       // to handle the case where the user grants the permission. See the documentation 
       // for ActivityCompat#requestPermissions for more details. 
       return; 
      } 
      googleMap.setMyLocationEnabled(true); 

      // For dropping a marker at a point on the Map 
      LatLng colorado = new LatLng(39.7392, -104.9903); 
      googleMap.addMarker(new MarkerOptions().position(colorado).title("Marker Title").snippet("Marker Description")); 

      // For zooming automatically to the location of the marker 
      CameraPosition cameraPosition = new CameraPosition.Builder().target(colorado).zoom(12).build(); 
      googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 

      addHeatMap(); 

     } 
    }); 




    return rootView; 
} 


public void addHeatMap(){ 
    ArrayList<LatLng> locations = generateLocations(); 
    mProvider = new HeatmapTileProvider.Builder().data(locations).build(); 
    mProvider.setRadius(HeatmapTileProvider.DEFAULT_RADIUS); 
    googleMap.addTileOverlay(new TileOverlayOptions().tileProvider(mProvider)); 
} 

private ArrayList<LatLng> generateLocations() { 
    ArrayList<LatLng> locations = new ArrayList<LatLng>(); 
    double lat; 
    double lng; 
    Random generator = new Random(); 
    for (int i = 0; i < 1000; i++) { 
     lat = generator.nextDouble()/3; 
     lng = generator.nextDouble()/3; 
     if (generator.nextBoolean()) { 
      lat = -lat; 
     } 
     if (generator.nextBoolean()) { 
      lng = -lng; 
     } 
     locations.add(new LatLng(mCenterLocation.latitude + lat, mCenterLocation.longitude + lng)); 
    } 

    return locations; 
} 

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

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

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

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

} 

回答

0

檢查Heatmaps guide,我沒有看到任何問題。嘗試撥打clearTileCache(),在API reference中指出您應該在setRadius之後撥打電話。

+0

感謝您的回答,我注意到標記也沒有出現,並且相機位置不起作用,我認爲我在地圖設置方面存在問題。 我不知道什麼是錯的。 –