Search in sources :

Example 11 with BloodRequest

use of com.jexapps.bloodhub.m_Model.BloodRequest in project BloodHub by kazijehangir.

the class RequestMapFragment method setupMap.

public void setupMap() {
    Log.d("JK", "setupMap: called ");
    try {
        MapsInitializer.initialize(getActivity().getApplicationContext());
    } catch (Exception e) {
        e.printStackTrace();
    }
    mMapView.getMapAsync(new OnMapReadyCallback() {

        @Override
        public void onMapReady(GoogleMap mMap) {
            googleMap = mMap;
            if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                googleMap.setMyLocationEnabled(true);
            }
            // For dropping a marker at a point on the Map
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            Date startDate = cal.getTime();
            FirebaseDatabase.getInstance().getReference().child("bloodrequests").orderByChild("date").startAt(startDate.getTime()).limitToFirst(15).addListenerForSingleValueEvent(new ValueEventListener() {

                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    for (DataSnapshot child : dataSnapshot.getChildren()) {
                        BloodRequest request = child.getValue(BloodRequest.class);
                        if (request.latitude != -1 && request.longitude != -1) {
                            String needs = request.quantity + " bags of " + request.blood_group;
                            Marker marker = googleMap.addMarker(new MarkerOptions().position(new LatLng(request.latitude, request.longitude)).title(request.name).snippet(needs));
                            marker.setTag(child.getKey());
                        }
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                }
            });
            // if we can't access the location yet
            if (!location.hasLocationEnabled()) {
                // ask the user to enable location access
                SimpleLocation.openSettings(getContext());
            }
            final double latitude = location.getLatitude();
            final double longitude = location.getLongitude();
            CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(latitude, longitude)).zoom(12).build();
            googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
            googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {

                @Override
                public void onInfoWindowClick(Marker marker) {
                    Intent intent = new Intent(getContext(), RequestDetail.class);
                    intent.putExtra("request", (String) marker.getTag());
                    getContext().startActivity(intent);
                }
            });
        }
    });
}
Also used : MarkerOptions(com.google.android.gms.maps.model.MarkerOptions) Calendar(java.util.Calendar) OnMapReadyCallback(com.google.android.gms.maps.OnMapReadyCallback) Intent(android.content.Intent) Marker(com.google.android.gms.maps.model.Marker) DataSnapshot(com.google.firebase.database.DataSnapshot) Date(java.util.Date) CameraPosition(com.google.android.gms.maps.model.CameraPosition) DatabaseError(com.google.firebase.database.DatabaseError) GoogleMap(com.google.android.gms.maps.GoogleMap) ValueEventListener(com.google.firebase.database.ValueEventListener) LatLng(com.google.android.gms.maps.model.LatLng) BloodRequest(com.jexapps.bloodhub.m_Model.BloodRequest)

Aggregations

BloodRequest (com.jexapps.bloodhub.m_Model.BloodRequest)11 Date (java.util.Date)8 DataSnapshot (com.google.firebase.database.DataSnapshot)7 DatabaseError (com.google.firebase.database.DatabaseError)7 ValueEventListener (com.google.firebase.database.ValueEventListener)7 TextView (android.widget.TextView)4 Intent (android.content.Intent)3 Bundle (android.os.Bundle)3 View (android.view.View)3 Calendar (java.util.Calendar)3 Dialog (android.app.Dialog)2 Context (android.content.Context)2 ColorDrawable (android.graphics.drawable.ColorDrawable)2 DefaultItemAnimator (android.support.v7.widget.DefaultItemAnimator)2 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)2 Button (android.widget.Button)2 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)2 FirebaseAuth (com.google.firebase.auth.FirebaseAuth)2 FirebaseUser (com.google.firebase.auth.FirebaseUser)2 Donation (com.jexapps.bloodhub.m_Model.Donation)2