use of com.bigyoshi.qrhunt.qr.PlayableQrCode in project QRHunt by CMPUT301W22T00.
the class FragmentMap method onCreateView.
/**
* Sets up fragment to be loaded in, finds all views, sets onClickListener for buttons
* @param inflater inflater
* @param container Where the fragment is contained
* @param savedInstanceState SavedInstanceState
* @return root
*/
@SuppressLint("ResourceType")
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Load/Initialize osmdroid configuration, database, and location
db = FirebaseFirestore.getInstance();
ctx = getActivity().getApplicationContext();
Configuration.getInstance().load(ctx, PreferenceManager.getDefaultSharedPreferences(ctx));
client = LocationServices.getFusedLocationProviderClient(this.getActivity());
startPollingLocation();
// Makes the map load faster via caching
IConfigurationProvider osmConf = Configuration.getInstance();
File basePath = new File(this.getActivity().getCacheDir().getAbsolutePath(), "osmdroid");
basePath.mkdirs();
osmConf.setOsmdroidBasePath(basePath);
File tileCache = new File(osmConf.getOsmdroidBasePath().getAbsolutePath(), "tile");
tileCache.mkdirs();
osmConf.setOsmdroidTileCache(tileCache);
osmConf.setUserAgentValue("QRHunt");
binding = FragmentMapBinding.inflate(inflater, container, false);
View root = binding.getRoot();
/* Inflate and create the map
setContentView(R.layout.fragment_map); */
map = binding.mapView;
map.setTileSource(TileSourceFactory.MAPNIK);
// Map Zoom Controls
map.setBuiltInZoomControls(false);
map.setMultiTouchControls(true);
IMapController mapController = map.getController();
mapController.setZoom(20);
// Creates user location
this.mLocationOverlay = new MyLocationNewOverlay(new GpsMyLocationProvider(ctx), map);
// This gets the current user location
getLocation().addOnCompleteListener(task -> {
if (task.isSuccessful()) {
Location location = task.getResult();
if (location != null) {
currentLocation = new Location(location);
}
/* this stops listening to the updates that
we didn't actually care about in the first place for; see startPollingLocation
*/
LocationServices.getFusedLocationProviderClient(ctx).removeLocationUpdates(hackyLocationCallback);
}
});
// Change person and directional icon
Drawable directionIcon = ContextCompat.getDrawable(getActivity(), R.drawable.ic_player_nav);
Drawable personIcon = ContextCompat.getDrawable(getActivity(), R.drawable.ic_person_icon);
this.mLocationOverlay.setDirectionArrow(drawableToBitmap(personIcon), drawableToBitmap(directionIcon));
// Enables and follows user location
this.mLocationOverlay.enableMyLocation();
this.mLocationOverlay.enableFollowLocation();
this.mLocationOverlay.setPersonHotspot(30.0f, 30.0f);
// Recenters the map onto the user
btnRecenter = (ImageButton) root.findViewById(R.id.map_recenter);
btnRecenter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
GeoPoint myLocation = new GeoPoint(currentLocation.getLatitude(), currentLocation.getLongitude());
map.getController().animateTo(myLocation);
map.getController().setZoom(20);
}
});
// Adding the overlays
map.getOverlays().add(this.mLocationOverlay);
Drawable pinIcon = ContextCompat.getDrawable(getActivity(), R.drawable.ic_qr_pin);
Query qrLocation = db.collectionGroup("qrCodes");
qrLocation.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot doc : task.getResult()) {
if (doc.exists()) {
PlayableQrCode qrCode = doc.toObject(PlayableQrCode.class);
if (qrCode.getLocation() != null) {
lat = qrCode.getLocation().getLatitude();
lng = qrCode.getLocation().getLongitude();
if (lat != null && lng != null) {
Log.d(TAG, String.format("lat %f long %f", lat, lng));
// Creates marker on map
geoPin = new Marker(map);
geoPin.setPosition(new GeoPoint(lat, lng));
geoPin.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
// Stuff for distance
Location markerLocation = new Location("");
markerLocation.setLatitude(lat);
markerLocation.setLongitude(lng);
float distance = currentLocation.distanceTo(markerLocation);
String d = formatDistance(distance);
Log.d(TAG, String.format("Distance: %s", d));
// Creates custom info window for the marker
CustomInfoWindow blurb = new CustomInfoWindow(map, d, qrCode);
geoPin.setInfoWindow(blurb);
geoPin.setIcon(pinIcon);
// Shows/Hides info window on click
// From: https://stackoverflow.com/a/45043472
// Author: https://stackoverflow.com/users/6889839/fregomene
geoPin.setOnMarkerClickListener(new Marker.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker, MapView mapView) {
if (!isInfoWindowShown) {
marker.showInfoWindow();
isInfoWindowShown = true;
} else {
marker.closeInfoWindow();
isInfoWindowShown = false;
}
return true;
}
});
// Adds the pins on the map
map.getOverlays().add(geoPin);
}
}
} else {
Log.d(TAG, "No such document");
}
}
} else {
Log.d(TAG, "get failed with ", task.getException());
}
}
});
return root;
}
Aggregations