Search in sources :

Example 1 with PlayableQrCode

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;
}
Also used : Query(com.google.firebase.firestore.Query) QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot) MyLocationNewOverlay(org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay) GpsMyLocationProvider(org.osmdroid.views.overlay.mylocation.GpsMyLocationProvider) Drawable(android.graphics.drawable.Drawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable) PlayableQrCode(com.bigyoshi.qrhunt.qr.PlayableQrCode) Marker(org.osmdroid.views.overlay.Marker) View(android.view.View) MapView(org.osmdroid.views.MapView) QuerySnapshot(com.google.firebase.firestore.QuerySnapshot) GeoPoint(org.osmdroid.util.GeoPoint) MapView(org.osmdroid.views.MapView) IConfigurationProvider(org.osmdroid.config.IConfigurationProvider) IMapController(org.osmdroid.api.IMapController) File(java.io.File) Location(android.location.Location) SuppressLint(android.annotation.SuppressLint)

Aggregations

SuppressLint (android.annotation.SuppressLint)1 BitmapDrawable (android.graphics.drawable.BitmapDrawable)1 Drawable (android.graphics.drawable.Drawable)1 Location (android.location.Location)1 View (android.view.View)1 PlayableQrCode (com.bigyoshi.qrhunt.qr.PlayableQrCode)1 Query (com.google.firebase.firestore.Query)1 QueryDocumentSnapshot (com.google.firebase.firestore.QueryDocumentSnapshot)1 QuerySnapshot (com.google.firebase.firestore.QuerySnapshot)1 File (java.io.File)1 IMapController (org.osmdroid.api.IMapController)1 IConfigurationProvider (org.osmdroid.config.IConfigurationProvider)1 GeoPoint (org.osmdroid.util.GeoPoint)1 MapView (org.osmdroid.views.MapView)1 Marker (org.osmdroid.views.overlay.Marker)1 GpsMyLocationProvider (org.osmdroid.views.overlay.mylocation.GpsMyLocationProvider)1 MyLocationNewOverlay (org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay)1