Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
I
InstiApp
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
RAHUL SHARMA
InstiApp
Commits
756d9335
Commit
756d9335
authored
Jan 16, 2018
by
Owais Chunawala
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add location FAB functionality
parent
c2d729ee
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
310 additions
and
7 deletions
+310
-7
app/build.gradle
app/build.gradle
+3
-2
app/src/main/java/in/ac/iitb/gymkhana/iitbapp/fragment/MapFragment.java
...ava/in/ac/iitb/gymkhana/iitbapp/fragment/MapFragment.java
+300
-3
app/src/main/res/layout/fragment_map.xml
app/src/main/res/layout/fragment_map.xml
+1
-1
app/src/main/res/values/strings.xml
app/src/main/res/values/strings.xml
+4
-0
gradle.properties
gradle.properties
+2
-1
No files found.
app/build.gradle
View file @
756d9335
...
@@ -41,15 +41,16 @@ dependencies {
...
@@ -41,15 +41,16 @@ dependencies {
compile
"net.openid:appauth:${appAuthVersion}"
compile
"net.openid:appauth:${appAuthVersion}"
testCompile
"junit:junit:4.12"
testCompile
"junit:junit:4.12"
compile
"com.android.support:design:${supportLibVersion}"
compile
"com.android.support:design:${supportLibVersion}"
compile
"com.google.android.gms:play-services-maps:${playServicesVersion}"
compile
"com.android.support:support-v4:${supportLibVersion}"
compile
"com.android.support:support-v4:${supportLibVersion}"
compile
"com.jakewharton:butterknife:${butterKnifeVersion}"
compile
"com.jakewharton:butterknife:${butterKnifeVersion}"
compile
"com.google.android.gms:play-services-gcm:
${playServicesVersion}
"
compile
"com.google.android.gms:play-services-gcm:
11.6.2
"
annotationProcessor
"com.jakewharton:butterknife-compiler:${butterKnifeVersion}"
annotationProcessor
"com.jakewharton:butterknife-compiler:${butterKnifeVersion}"
compile
"com.squareup.retrofit2:retrofit:${retrofitVersion}"
compile
"com.squareup.retrofit2:retrofit:${retrofitVersion}"
compile
"com.squareup.retrofit2:converter-gson:${retrofitVersion}"
compile
"com.squareup.retrofit2:converter-gson:${retrofitVersion}"
compile
"com.squareup.okhttp3:okhttp:${okhttpVersion}"
compile
"com.squareup.okhttp3:okhttp:${okhttpVersion}"
compile
"com.squareup.okhttp3:logging-interceptor:${okhttpVersion}"
compile
"com.squareup.okhttp3:logging-interceptor:${okhttpVersion}"
compile
"com.squareup.picasso:picasso:${picassoVersion}"
compile
"com.squareup.picasso:picasso:${picassoVersion}"
compile
'com.google.android.gms:play-services-maps:11.6.2'
compile
'com.google.android.gms:play-services-location:11.6.2'
}
}
apply
plugin:
'com.google.gms.google-services'
apply
plugin:
'com.google.gms.google-services'
app/src/main/java/in/ac/iitb/gymkhana/iitbapp/fragment/MapFragment.java
View file @
756d9335
package
in.ac.iitb.gymkhana.iitbapp.fragment
;
package
in.ac.iitb.gymkhana.iitbapp.fragment
;
import
android.Manifest
;
import
android.app.AlertDialog
;
import
android.content.Context
;
import
android.content.DialogInterface
;
import
android.content.Intent
;
import
android.content.IntentSender
;
import
android.content.pm.PackageManager
;
import
android.content.res.ColorStateList
;
import
android.graphics.Color
;
import
android.graphics.PorterDuff
;
import
android.location.Location
;
import
android.location.LocationManager
;
import
android.net.Uri
;
import
android.os.Build
;
import
android.os.Bundle
;
import
android.os.Bundle
;
import
android.provider.Settings
;
import
android.support.design.widget.FloatingActionButton
;
import
android.support.v4.app.ActivityCompat
;
import
android.support.v4.app.Fragment
;
import
android.support.v4.app.Fragment
;
import
android.support.v4.content.ContextCompat
;
import
android.util.Log
;
import
android.view.LayoutInflater
;
import
android.view.LayoutInflater
;
import
android.view.View
;
import
android.view.View
;
import
android.view.ViewGroup
;
import
android.view.ViewGroup
;
import
android.widget.Toast
;
import
com.google.android.gms.common.ConnectionResult
;
import
com.google.android.gms.common.api.GoogleApiClient
;
import
com.google.android.gms.common.api.PendingResult
;
import
com.google.android.gms.common.api.ResultCallback
;
import
com.google.android.gms.common.api.Status
;
import
com.google.android.gms.location.LocationListener
;
import
com.google.android.gms.location.LocationRequest
;
import
com.google.android.gms.location.LocationServices
;
import
com.google.android.gms.location.LocationSettingsRequest
;
import
com.google.android.gms.location.LocationSettingsResult
;
import
com.google.android.gms.location.LocationSettingsStates
;
import
com.google.android.gms.location.LocationSettingsStatusCodes
;
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
;
import
com.google.android.gms.maps.SupportMapFragment
;
import
com.google.android.gms.maps.SupportMapFragment
;
import
com.google.android.gms.maps.model.LatLng
;
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.Marker
;
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
;
public
class
MapFragment
extends
Fragment
implements
OnMapReadyCallback
{
import
static
android
.
content
.
Context
.
LOCATION_SERVICE
;
public
class
MapFragment
extends
Fragment
implements
OnMapReadyCallback
,
LocationListener
,
GoogleApiClient
.
ConnectionCallbacks
,
GoogleApiClient
.
OnConnectionFailedListener
{
SupportMapFragment
gMapFragment
;
SupportMapFragment
gMapFragment
;
GoogleMap
googleMap
;
GoogleMap
googleMap
;
GoogleApiClient
mGoogleApiClient
;
Location
mLastLocation
;
Marker
mCurrLocationMarker
;
LocationRequest
mLocationRequest
;
private
FloatingActionButton
locationButton
;
private
Location
currentLocation
;
public
static
final
int
MY_PERMISSIONS_REQUEST_LOCATION
=
99
;
public
View
onCreateView
(
LayoutInflater
inflater
,
ViewGroup
container
,
public
View
onCreateView
(
LayoutInflater
inflater
,
ViewGroup
container
,
Bundle
savedInstanceState
)
{
Bundle
savedInstanceState
)
{
...
@@ -31,12 +77,160 @@ public class MapFragment extends Fragment implements OnMapReadyCallback {
...
@@ -31,12 +77,160 @@ public class MapFragment extends Fragment implements OnMapReadyCallback {
return
view
;
return
view
;
}
}
@Override
public
void
onStart
(){
super
.
onStart
();
locationButton
=
(
FloatingActionButton
)
getActivity
().
findViewById
(
R
.
id
.
location_button
);
locationButton
.
setImageResource
(
R
.
drawable
.
ic_my_location_black_24dp
);
locationButton
.
getDrawable
().
setColorFilter
(
ContextCompat
.
getColor
(
getContext
(),
R
.
color
.
colorPrimaryDark
),
PorterDuff
.
Mode
.
SRC_IN
);
locationButton
.
setOnClickListener
(
new
View
.
OnClickListener
()
{
@Override
public
void
onClick
(
View
v
)
{
Log
.
v
(
"MapFragment"
,
"Location button pressed"
);
try
{
LocationManager
lm
=
(
LocationManager
)
getContext
().
getSystemService
(
Context
.
LOCATION_SERVICE
);
boolean
gps_enabled
=
false
;
boolean
network_enabled
=
false
;
if
(
ActivityCompat
.
checkSelfPermission
(
getActivity
(),
android
.
Manifest
.
permission
.
ACCESS_FINE_LOCATION
)
!=
PackageManager
.
PERMISSION_GRANTED
&&
ActivityCompat
.
checkSelfPermission
(
getContext
(),
android
.
Manifest
.
permission
.
ACCESS_COARSE_LOCATION
)
!=
PackageManager
.
PERMISSION_GRANTED
)
{
ActivityCompat
.
requestPermissions
(
getActivity
(),
new
String
[]{
Manifest
.
permission
.
ACCESS_COARSE_LOCATION
},
MY_PERMISSIONS_REQUEST_LOCATION
);
return
;
}
try
{
gps_enabled
=
lm
.
isProviderEnabled
(
LocationManager
.
GPS_PROVIDER
);
}
catch
(
Exception
ex
)
{}
try
{
network_enabled
=
lm
.
isProviderEnabled
(
LocationManager
.
NETWORK_PROVIDER
);
}
catch
(
Exception
ex
)
{}
if
(!
gps_enabled
&&
!
network_enabled
)
{
LocationRequest
locationRequest
=
LocationRequest
.
create
();
locationRequest
.
setPriority
(
LocationRequest
.
PRIORITY_HIGH_ACCURACY
);
locationRequest
.
setInterval
(
30
*
1000
);
locationRequest
.
setFastestInterval
(
5
*
1000
);
LocationSettingsRequest
.
Builder
builder
=
new
LocationSettingsRequest
.
Builder
()
.
addLocationRequest
(
locationRequest
);
//**************************
builder
.
setAlwaysShow
(
true
);
//this is the key ingredient
//**************************
PendingResult
<
LocationSettingsResult
>
result
=
LocationServices
.
SettingsApi
.
checkLocationSettings
(
mGoogleApiClient
,
builder
.
build
());
result
.
setResultCallback
(
new
ResultCallback
<
LocationSettingsResult
>()
{
@Override
public
void
onResult
(
LocationSettingsResult
result
)
{
final
Status
status
=
result
.
getStatus
();
final
LocationSettingsStates
state
=
result
.
getLocationSettingsStates
();
switch
(
status
.
getStatusCode
())
{
case
LocationSettingsStatusCodes
.
SUCCESS
:
// All location settings are satisfied. The client can initialize location
// requests here.
break
;
case
LocationSettingsStatusCodes
.
RESOLUTION_REQUIRED
:
// Location settings are not satisfied. But could be fixed by showing the user
// a dialog.
try
{
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
status
.
startResolutionForResult
(
getActivity
(),
1000
);
}
catch
(
IntentSender
.
SendIntentException
e
)
{
// Ignore the error.
}
break
;
case
LocationSettingsStatusCodes
.
SETTINGS_CHANGE_UNAVAILABLE
:
// Location settings are not satisfied. However, we have no way to fix the
// settings so we won't show the dialog.
break
;
}
}
});
}
currentLocation
=
getLastKnownLocation
();
if
(
currentLocation
!=
null
)
{
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
);
}
}
catch
(
Exception
e
){
checkLocationPermission
();
Toast
.
makeText
(
getContext
(),
"Please turn on Location from the Settings"
,
Toast
.
LENGTH_SHORT
).
show
();
}
}
});
}
private
Location
getLastKnownLocation
()
{
//Create a location manager object instance
LocationManager
mLocationManager
=
(
LocationManager
)
getContext
().
getSystemService
(
LOCATION_SERVICE
);
//Get all the different providers which can give current location info
List
<
String
>
providers
=
mLocationManager
.
getProviders
(
true
);
//Initialising the location to be null
Location
bestLocation
=
null
;
//Creating a for loop to go through all the location providers and get the location
for
(
String
provider
:
providers
)
{
//if permission is not granted, then get the permission
if
(
ContextCompat
.
checkSelfPermission
(
getContext
(),
android
.
Manifest
.
permission
.
ACCESS_COARSE_LOCATION
)
!=
PackageManager
.
PERMISSION_GRANTED
)
{
ActivityCompat
.
requestPermissions
(
getActivity
(),
new
String
[]{
android
.
Manifest
.
permission
.
ACCESS_COARSE_LOCATION
},
MY_PERMISSIONS_REQUEST_LOCATION
);
}
//Get the last known location from the data provider
Location
l
=
mLocationManager
.
getLastKnownLocation
(
provider
);
//If the last know location provided by the data provider is null then ignore the provider and move to the next one.
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
;
googleMap
=
gMap
;
// googleMap.setMyLocationEnabled(true);
if
(
ActivityCompat
.
checkSelfPermission
(
getContext
(),
Manifest
.
permission
.
ACCESS_FINE_LOCATION
)
!=
PackageManager
.
PERMISSION_GRANTED
&&
ActivityCompat
.
checkSelfPermission
(
getContext
(),
Manifest
.
permission
.
ACCESS_COARSE_LOCATION
)
!=
PackageManager
.
PERMISSION_GRANTED
)
{
checkLocationPermission
();
}
else
{
googleMap
.
setMyLocationEnabled
(
true
);
googleMap
.
getUiSettings
().
setMyLocationButtonEnabled
(
false
);
googleMap
.
getUiSettings
().
setMyLocationButtonEnabled
(
false
);
}
locationButton
.
setBackgroundTintList
(
ColorStateList
.
valueOf
(
Color
.
parseColor
(
"#FFFFFF"
)));
//Initialize Google Play Services
if
(
android
.
os
.
Build
.
VERSION
.
SDK_INT
>=
Build
.
VERSION_CODES
.
M
)
{
if
(
ContextCompat
.
checkSelfPermission
(
getActivity
(),
Manifest
.
permission
.
ACCESS_FINE_LOCATION
)
==
PackageManager
.
PERMISSION_GRANTED
)
{
//Location Permission already granted
buildGoogleApiClient
();
googleMap
.
setMyLocationEnabled
(
true
);
}
else
{
//Request Location Permission
checkLocationPermission
();
}
}
else
{
buildGoogleApiClient
();
googleMap
.
setMyLocationEnabled
(
true
);
}
googleMap
.
getUiSettings
().
setZoomGesturesEnabled
(
true
);
googleMap
.
getUiSettings
().
setZoomGesturesEnabled
(
true
);
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
));
...
@@ -49,4 +243,107 @@ public class MapFragment extends Fragment implements OnMapReadyCallback {
...
@@ -49,4 +243,107 @@ public class MapFragment extends Fragment implements OnMapReadyCallback {
.
title
(
"Marker in IITB"
));
.
title
(
"Marker in IITB"
));
googleMap
.
moveCamera
(
CameraUpdateFactory
.
newLatLng
(
iitb
));
googleMap
.
moveCamera
(
CameraUpdateFactory
.
newLatLng
(
iitb
));
}
}
private
void
checkLocationPermission
()
{
if
(
ContextCompat
.
checkSelfPermission
(
getActivity
(),
Manifest
.
permission
.
ACCESS_FINE_LOCATION
)
!=
PackageManager
.
PERMISSION_GRANTED
)
{
// Should we show an explanation?
if
(
ActivityCompat
.
shouldShowRequestPermissionRationale
(
getActivity
(),
Manifest
.
permission
.
ACCESS_FINE_LOCATION
))
{
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
new
AlertDialog
.
Builder
(
getActivity
())
.
setTitle
(
"Location Permission Needed"
)
.
setMessage
(
"This app needs the Location permission, please accept to use location functionality"
)
.
setPositiveButton
(
"OK"
,
new
DialogInterface
.
OnClickListener
()
{
@Override
public
void
onClick
(
DialogInterface
dialogInterface
,
int
i
)
{
//Prompt the user once explanation has been shown
requestPermissions
(
new
String
[]{
Manifest
.
permission
.
ACCESS_FINE_LOCATION
},
MY_PERMISSIONS_REQUEST_LOCATION
);
}
})
.
create
()
.
show
();
}
else
{
// No explanation needed, we can request the permission.
requestPermissions
(
new
String
[]{
Manifest
.
permission
.
ACCESS_FINE_LOCATION
},
MY_PERMISSIONS_REQUEST_LOCATION
);
}
}
}
protected
synchronized
void
buildGoogleApiClient
()
{
mGoogleApiClient
=
new
GoogleApiClient
.
Builder
(
getActivity
())
.
addConnectionCallbacks
(
this
)
.
addOnConnectionFailedListener
(
this
)
.
addApi
(
LocationServices
.
API
)
.
build
();
mGoogleApiClient
.
connect
();
}
@Override
public
void
onLocationChanged
(
Location
location
)
{
mLastLocation
=
location
;
if
(
mCurrLocationMarker
!=
null
)
{
mCurrLocationMarker
.
remove
();
}
//Place current location marker
LatLng
latLng
=
new
LatLng
(
location
.
getLatitude
(),
location
.
getLongitude
());
//move map camera
googleMap
.
moveCamera
(
CameraUpdateFactory
.
newLatLng
(
latLng
));
googleMap
.
animateCamera
(
CameraUpdateFactory
.
zoomTo
(
17
));
//stop location updates
if
(
mGoogleApiClient
!=
null
)
{
LocationServices
.
FusedLocationApi
.
removeLocationUpdates
(
mGoogleApiClient
,
this
);
}
}
@Override
public
void
onConnectionSuspended
(
int
i
)
{
}
@Override
public
void
onRequestPermissionsResult
(
int
requestCode
,
String
permissions
[],
int
[]
grantResults
)
{
switch
(
requestCode
)
{
case
MY_PERMISSIONS_REQUEST_LOCATION:
{
// If request is cancelled, the result arrays are empty.
if
(
grantResults
.
length
>
0
&&
grantResults
[
0
]
==
PackageManager
.
PERMISSION_GRANTED
)
{
if
(
ContextCompat
.
checkSelfPermission
(
getActivity
(),
Manifest
.
permission
.
ACCESS_FINE_LOCATION
)
==
PackageManager
.
PERMISSION_GRANTED
)
{
if
(
mGoogleApiClient
==
null
)
{
buildGoogleApiClient
();
}
googleMap
.
setMyLocationEnabled
(
true
);
}
}
return
;
}
}
}
@Override
public
void
onConnectionFailed
(
ConnectionResult
connectionResult
)
{
}
@Override
public
void
onConnected
(
Bundle
bundle
)
{
mLocationRequest
=
new
LocationRequest
();
mLocationRequest
.
setInterval
(
1000
);
mLocationRequest
.
setFastestInterval
(
1000
);
mLocationRequest
.
setPriority
(
LocationRequest
.
PRIORITY_BALANCED_POWER_ACCURACY
);
if
(
ContextCompat
.
checkSelfPermission
(
getActivity
(),
Manifest
.
permission
.
ACCESS_FINE_LOCATION
)
==
PackageManager
.
PERMISSION_GRANTED
)
{
LocationServices
.
FusedLocationApi
.
requestLocationUpdates
(
mGoogleApiClient
,
mLocationRequest
,
this
);
}
}
}
}
app/src/main/res/layout/fragment_map.xml
View file @
756d9335
...
@@ -15,7 +15,7 @@
...
@@ -15,7 +15,7 @@
<!--TODO: Change colours-->
<!--TODO: Change colours-->
<android.support.design.widget.FloatingActionButton
<android.support.design.widget.FloatingActionButton
android:id=
"@+id/location"
android:id=
"@+id/location
_button
"
android:layout_width=
"wrap_content"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_gravity=
"bottom|end"
android:layout_gravity=
"bottom|end"
...
...
app/src/main/res/values/strings.xml
View file @
756d9335
...
@@ -8,4 +8,8 @@
...
@@ -8,4 +8,8 @@
<!-- TODO: Remove or change this placeholder text -->
<!-- TODO: Remove or change this placeholder text -->
<string
name=
"hello_blank_fragment"
>
Hello blank fragment
</string>
<string
name=
"hello_blank_fragment"
>
Hello blank fragment
</string>
<string
name=
"Cancel"
>
Cancel
</string>
<string
name=
"gps_network_not_enabled"
>
Location is not enabled. Please turn on your location from the settings.
</string>
<string
name=
"open_location_settings"
>
Open Location Settings
</string>
</resources>
</resources>
gradle.properties
View file @
756d9335
...
@@ -17,4 +17,5 @@ org.gradle.jvmargs=-Xmx1536m
...
@@ -17,4 +17,5 @@ org.gradle.jvmargs=-Xmx1536m
# org.gradle.parallel=true
# org.gradle.parallel=true
# TODO Replace Key and put in local:properties
# TODO Replace Key and put in local:properties
GOOGLE_MAPS_API_KEY
=
AIzaSyB72DhM4E1NW0F-_SLnYtYYvz0rT9kne-U
#GOOGLE_MAPS_API_KEY=AIzaSyB72DhM4E1NW0F-_SLnYtYYvz0rT9kne-U
GOOGLE_MAPS_API_KEY
=
AIzaSyC31o6h06Z36V8M0lXgkGj4Y1cmNRVV9s0
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment