Code example for MapFragment

Methods: getActivitygetMap

0
 
	public void initMapIfNeeded() { 
		if (_googleMap == null) {
			// 2 cases now. It's either google play service is unavailable or not really initialized 
 
			_googleMap = _mapFragment.getMap();
			if (_googleMap == null) {
				// google play service is not available on user's device, we simply return. 
				// activity will call this method in onResume() 
				return; 
				// then MapFragment will show a label and a button directing user to download google play service 
			} 
 
			try { 
				MapsInitializer.initialize(_mapFragment.getActivity());
			} catch (GooglePlayServicesNotAvailableException e) {
				Log.e(GoogleMapManager.class.getSimpleName(), e.toString());
				return; 
			} 
 
			_uiManager = new UiManager();
			_markerManager = new MarkerManager(_googleMap);
 
			setUpMyLocationButton(); 
	        addBuildingMarkers(); 
	        setListeners(); 
		} 
	} 
 
	private void setListeners() { 
 
		_googleMap.setOnMapClickListener(new OnMapClickListener() {
			@Override 
			public void onMapClick(LatLng position) {
				animateToBuildingZoomLevelIfNeeded(position);
			} 
		}); 
 
		_googleMap.setOnMyLocationChangeListener(new OnMyLocationChangeListener() {
			@Override 
			public void onMyLocationChange(Location location) {
				LatLng position = new LatLng(location.getLatitude(), location.getLongitude());
				animateToBuildingZoomLevel(position);
			} 
		}); 
 
		_markerManager.setMarkerRelatedListeners(_mapFragment.getActivity(), this);
	} 
 
	/** 
	 * Animated camera if current zoom level < <code>BUILDING_ZOOM_LEVEL</code> 
	 * 
	 * @return true if camera is animated; false otherwise. 
	 * @see #BUILDING_ZOOM_LEVEL 
	 */ 
	public boolean animateToBuildingZoomLevelIfNeeded(LatLng latLng) {
		if (_googleMap.getCameraPosition().zoom < GoogleMapManager.BUILDING_ZOOM_LEVEL) {
			animateToBuildingZoomLevel(latLng);
			return true; 
		} 
		else { 
			return false; 
		} 
	} 
 
	public void animateToBuildingZoomLevel(LatLng latLng) {
		_googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, BUILDING_ZOOM_LEVEL));
	} 
 
	public void addBuildingMarkers() { 
    	DaoSession daoSession = DatabaseService.getDaoSession(_mapFragment.getActivity());
    	List<Building> buildings = DatabaseService.getAllBuildings(daoSession);
 
    	BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.ic_building_clickable_icon);
    	_markerManager.addBuildingMarkers(buildings, icon);
	} 
 
	public void setUpMyLocationButton() { 
		_uiManager.setUpGoogleMapMyLocation(_mapFragment, _googleMap);
	} 
 
	public void addDepartmentMarker(Department department) {
		_markerManager.removeAllMarkersIfNeeded();
		_markerManager.addDepartmentMarker(department);
		_markerManager.showLastAddedMarkerInfowindow();
		animateToBuildingZoomLevel(department.getLocation().toLatLng());
	} 
 
	public void addLibraryMarker(Library library) {
		_markerManager.removeAllMarkersIfNeeded();
		_markerManager.addLibraryMarker(library);
		_markerManager.showLastAddedMarkerInfowindow();
		animateToBuildingZoomLevel(library.getLocation().toLatLng());
	} 
 
	public void addFoodServiceMarkers(List<FoodService> foodServices) {
		_markerManager.removeAllMarkersIfNeeded();
		_markerManager.addFoodServiceMarkers(foodServices);
	} 
} 
 
class UiManager { 
 
	public void setUpGoogleMapMyLocation(MapFragment mapFragment, GoogleMap googleMap) {
		// make the button show up 
		googleMap.setMyLocationEnabled(true);
		googleMap.getUiSettings().setMyLocationButtonEnabled(true);
		// move the button to bottom left 
		MapViewUtil.putMyLocationButtonBottomLeft(mapFragment);
	} 
} 
Contextual code suggestions in your IDE  Get Codota for Java