Commit e5d39397 authored by Sajal Narang's avatar Sajal Narang

Add location FAB

parent df3ddfef
...@@ -21,18 +21,24 @@ android { ...@@ -21,18 +21,24 @@ android {
} }
} }
ext {
supportLibVersion = '25.3.1'
playServicesVersion = '11.0.2'
butterKnifeVersion = '8.6.0'
appAuthVersion = '0.2.0'
}
dependencies { dependencies {
compile fileTree(dir: 'libs', include: ['*.jar']) compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations' exclude group: 'com.android.support', module: 'support-annotations'
}) })
compile 'com.android.support:appcompat-v7:25.3.1' compile "com.android.support:appcompat-v7:${supportLibVersion}"
compile 'net.openid:appauth:0.2.0' compile "net.openid:appauth:${appAuthVersion}"
testCompile 'junit:junit:4.12' testCompile "junit:junit:4.12"
compile 'com.android.support:design:25.3.1' compile "com.android.support:design:${supportLibVersion}"
compile 'com.google.android.gms:play-services-maps:10.2.6' compile "com.google.android.gms:play-services-maps:${playServicesVersion}"
compile 'com.android.support.constraint:constraint-layout:1.0.2' compile "com.android.support:support-v4:${supportLibVersion}"
compile 'com.android.support:support-v4:25.3.1' compile "com.jakewharton:butterknife:${butterKnifeVersion}"
compile 'com.jakewharton:butterknife:8.6.0' annotationProcessor "com.jakewharton:butterknife-compiler:${butterKnifeVersion}"
annotationProcessor 'com.jakewharton:butterknife-compiler:8.6.0'
} }
...@@ -2,7 +2,11 @@ package in.ac.iitb.gymkhana.iitbapp.fragments; ...@@ -2,7 +2,11 @@ package in.ac.iitb.gymkhana.iitbapp.fragments;
import android.Manifest; import android.Manifest;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.graphics.PorterDuff;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle; import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater; import android.view.LayoutInflater;
...@@ -10,6 +14,7 @@ import android.view.View; ...@@ -10,6 +14,7 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.Toast; import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback; import com.google.android.gms.maps.OnMapReadyCallback;
...@@ -18,12 +23,18 @@ import com.google.android.gms.maps.model.LatLng; ...@@ -18,12 +23,18 @@ import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds; import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.MarkerOptions; import com.google.android.gms.maps.model.MarkerOptions;
import java.util.List;
import in.ac.iitb.gymkhana.iitbapp.R; import in.ac.iitb.gymkhana.iitbapp.R;
import static android.content.Context.LOCATION_SERVICE;
public class MapFragment extends Fragment implements OnMapReadyCallback { public class MapFragment extends Fragment implements OnMapReadyCallback {
SupportMapFragment gMapFragment; SupportMapFragment gMapFragment;
GoogleMap googleMap ; GoogleMap googleMap;
FloatingActionButton locationButton;
Location currentLocation;
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
...@@ -36,44 +47,72 @@ public class MapFragment extends Fragment implements OnMapReadyCallback { ...@@ -36,44 +47,72 @@ public class MapFragment extends Fragment implements OnMapReadyCallback {
return view; return view;
} }
private Location getLastKnownLocation() {
LocationManager mLocationManager = (LocationManager) getContext().getSystemService(LOCATION_SERVICE);
List<String> providers = mLocationManager.getProviders(true);
Location bestLocation = null;
for (String provider : providers) {
Location l = mLocationManager.getLastKnownLocation(provider);
if (l == null) {
continue;
}
if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
bestLocation = l;
}
}
return bestLocation;
}
@Override @Override
public void onMapReady(GoogleMap gMap) { public void onMapReady(GoogleMap gMap) {
googleMap=gMap; //TODO: Handle Location permissions in MainActivity, permission required to view fragment
if (ContextCompat.checkSelfPermission(getActivity(), googleMap = gMap;
Manifest.permission.ACCESS_FINE_LOCATION) if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
== PackageManager.PERMISSION_GRANTED) {
googleMap.setMyLocationEnabled(true); googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true); googleMap.getUiSettings().setMyLocationButtonEnabled(false);
googleMap.getUiSettings().setZoomGesturesEnabled(true); googleMap.getUiSettings().setZoomGesturesEnabled(true);
} else { } else {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 0); requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 0);
} }
LatLngBounds iitbBounds=new LatLngBounds(new LatLng(19.1249000, 72.9046000),new LatLng(19.143522, 72.920000)); LatLngBounds iitbBounds = new LatLngBounds(new LatLng(19.1249000, 72.9046000), new LatLng(19.143522, 72.920000));
googleMap.setLatLngBoundsForCameraTarget(iitbBounds); googleMap.setLatLngBoundsForCameraTarget(iitbBounds);
googleMap.setMaxZoomPreference(30); googleMap.setMaxZoomPreference(30);
googleMap.setMinZoomPreference((float) 14.5); googleMap.setMinZoomPreference(14.5f);
// Position the map's camera near Mumbai // Position the map's camera near Mumbai
LatLng iitb = new LatLng(19.1334, 72.9133); LatLng iitb = new LatLng(19.1334, 72.9133);
googleMap.addMarker(new MarkerOptions().position(iitb) googleMap.addMarker(new MarkerOptions().position(iitb).title("Marker in IITB"));
.title("Marker in IITB"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(iitb)); googleMap.moveCamera(CameraUpdateFactory.newLatLng(iitb));
googleMap.setOnCameraMoveListener(new GoogleMap.OnCameraMoveListener() {
@Override
public void onCameraMove() {
locationButton.getDrawable().setColorFilter(ContextCompat.getColor(getContext(), R.color.colorGray), PorterDuff.Mode.SRC_IN);
} }
public void onRequestPermissionsResult (int requestCode, });
String[] permissions,
int[] grantResults) locationButton = (FloatingActionButton) getActivity().findViewById(R.id.location);
{ locationButton.setOnClickListener(new View.OnClickListener() {
if(grantResults[0]==PackageManager.PERMISSION_GRANTED) @Override
{ public void onClick(View v) {
currentLocation = getLastKnownLocation();
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()), 17);
googleMap.animateCamera(cameraUpdate);
locationButton.getDrawable().setColorFilter(ContextCompat.getColor(getContext(), R.color.colorPrimary), PorterDuff.Mode.SRC_IN);
}
});
}
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
googleMap.setMyLocationEnabled(true); googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true); googleMap.getUiSettings().setMyLocationButtonEnabled(false);
googleMap.getUiSettings().setZoomGesturesEnabled(true); googleMap.getUiSettings().setZoomGesturesEnabled(true);
} } else {
else
{
Toast toast = Toast.makeText(getActivity(), "Need Permission", Toast.LENGTH_SHORT); Toast toast = Toast.makeText(getActivity(), "Need Permission", Toast.LENGTH_SHORT);
toast.show(); toast.show();
} }
}} }
}
...@@ -12,4 +12,15 @@ ...@@ -12,4 +12,15 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
map:cameraZoom="18" /> map:cameraZoom="18" />
<!--TODO: Change colours-->
<android.support.design.widget.FloatingActionButton
android:id="@+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginBottom="48dp"
android:layout_marginEnd="@dimen/fab_margin"
android:src="@drawable/ic_my_location_black_24dp" />
</FrameLayout> </FrameLayout>
\ No newline at end of file
...@@ -4,4 +4,5 @@ ...@@ -4,4 +4,5 @@
<color name="colorPrimaryDark">#44C0CA</color> <color name="colorPrimaryDark">#44C0CA</color>
<color name="colorAccent">#ECF833</color> <color name="colorAccent">#ECF833</color>
<color name="colorCalendarWeek">#000000</color> <color name="colorCalendarWeek">#000000</color>
<color name="colorGray">#757575</color>
</resources> </resources>
...@@ -7,7 +7,7 @@ buildscript { ...@@ -7,7 +7,7 @@ buildscript {
jcenter() jcenter()
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:3.0.0-alpha3' classpath 'com.android.tools.build:gradle:3.0.0-alpha4'
// NOTE: Do not place your application dependencies here; they belong // NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files // in the individual module build.gradle files
......
#Fri May 19 05:35:14 IST 2017 #Fri Jun 30 22:37:20 IST 2017
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.0-milestone-1-all.zip distributionUrl=https\://services.gradle.org/distributions/gradle-4.0-rc-1-all.zip
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment