Created
December 18, 2014 18:05
-
-
Save azec-pdx/4f7f2138318ecb586cfb to your computer and use it in GitHub Desktop.
Dohvacanje lokacije GPS primjer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Location getLocation() { | |
try { | |
locationManager = (LocationManager) mContext | |
.getSystemService(LOCATION_SERVICE); | |
// getting GPS status | |
isGPSEnabled = locationManager | |
.isProviderEnabled(LocationManager.GPS_PROVIDER); | |
// getting network status | |
isNetworkEnabled = locationManager | |
.isProviderEnabled(LocationManager.NETWORK_PROVIDER); | |
if (!isGPSEnabled && !isNetworkEnabled) { | |
// no network provider is enabled | |
} else { | |
this.canGetLocation = true; | |
// First get location from Network Provider | |
if (isNetworkEnabled) { | |
locationManager.requestLocationUpdates( | |
LocationManager.NETWORK_PROVIDER, | |
MIN_TIME_BW_UPDATES, | |
MIN_DISTANCE_CHANGE_FOR_UPDATES, this); | |
Log.d("Network", "Network"); | |
if (locationManager != null) { | |
location = locationManager | |
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); | |
if (location != null) { | |
latitude = location.getLatitude(); | |
longitude = location.getLongitude(); | |
} | |
} | |
} | |
// if GPS Enabled get lat/long using GPS Services | |
if (isGPSEnabled) { | |
if (location == null) { | |
locationManager.requestLocationUpdates( | |
LocationManager.GPS_PROVIDER, | |
MIN_TIME_BW_UPDATES, | |
MIN_DISTANCE_CHANGE_FOR_UPDATES, this); | |
Log.d("GPS Enabled", "GPS Enabled"); | |
if (locationManager != null) { | |
location = locationManager | |
.getLastKnownLocation(LocationManager.GPS_PROVIDER); | |
if (location != null) { | |
latitude = location.getLatitude(); | |
longitude = location.getLongitude(); | |
} | |
} | |
} | |
} | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return location; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment