I am trying to set the location's latitude and longitude so I can use an API to get weather info.
When I run the app, I don't get any errors, but the location is 0.0, 0.0. A default location shows within the app.
I have made sure I'm checking/asking for location permission too.
This is code in Kotlin. I have implemented 'com.google.android.gms:play-services-location:17.1.0
, added kotlin-android-extensions
in the gradle, and added coarse, fine location permissions and internet permissions.
This is my location part of the MainActivity.kt
@SuppressLint("MissingPermission")
private fun getLastLocation() {
if(CheckPermission()){
if(isLocationEnabled()){
fusedLocationProviderClient.lastLocation.addOnCompleteListener{task ->
var location = task.result
if(location == null){
getNewLocation()
}else {
locationResult = location
}
}
}else{
Toast.makeText(this, "Location service not enabled", Toast.LENGTH_SHORT).show()
}
}else{
RequestPermission()
}
}
private fun CheckPermission() : Boolean {
if( (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
|| (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) ){
return true
}
return false
}
private fun RequestPermission() {
ActivityCompat.requestPermissions(
this,
arrayOf(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
), PERMISSION_ID
)
}
private fun isLocationEnabled() : Boolean{
var locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == PERMISSION_ID){
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED){
Log.d(TAG, "Permission granted")
}
}
}
@SuppressLint("MissingPermission")
private fun getNewLocation(){
locationRequest = LocationRequest()
locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
locationRequest.interval = 0
locationRequest.fastestInterval = 0
locationRequest.numUpdates = 2
fusedLocationProviderClient!!.requestLocationUpdates(
locationRequest, locationCallback, null
)
}
private val locationCallback = object : LocationCallback() {
override fun onLocationResult(p0: LocationResult?){
var lastLocation = p0?.lastLocation
if (lastLocation != null) {
locationResult = lastLocation
}
}
}
MainActivity.kt -- FULL
package <censored>.weatherapp
import android.Manifest
import android.annotation.SuppressLint
import android.content.Context
import android.content.pm.PackageManager
import android.location.Location
import android.location.LocationManager
import android.os.AsyncTask
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.ProgressBar
import android.widget.RelativeLayout
import android.widget.TextView
import android.widget.Toast
import androidx.core.app.ActivityCompat
import com.google.android.gms.location.*
import com.squareup.picasso.Picasso
import kotlinx.android.synthetic.main.activity_main.*
import org.json.JSONObject
import java.lang.Exception
import java.net.URL
import java.text.SimpleDateFormat
import java.util.*
class MainActivity : AppCompatActivity() {
companion object {
val API : String = "<censored>"
private var PERMISSION_ID : Int = 1000
val TAG = "MainActivity"
}
lateinit var fusedLocationProviderClient: FusedLocationProviderClient
lateinit var locationRequest : LocationRequest
lateinit var locationResult : Location
var latitude : Double = 0.0
var longitude : Double = 0.0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this)
getLastLocation()
weatherTask().execute()
}
inner class weatherTask() : AsyncTask<String, Void, String>() {
override fun onPreExecute() {
super.onPreExecute()
//Showing the ProgressBar, making the main design gone
findViewById<ProgressBar>(R.id.loader).visibility = View.VISIBLE
findViewById<RelativeLayout>(R.id.mainContainer).visibility = View.GONE
}
override fun doInBackground(vararg params: String?): String? {
latitude = locationResult.latitude
longitude = locationResult.longitude
Log.d(TAG, "$latitude , $longitude")
var response : String?
try{
response = URL("https://api.openweathermap.org/data/2.5/weather?lat=$latitude&lon=$longitude&units=metric&appid=$API")
.readText(Charsets.UTF_8)
}catch (e: Exception){
response = null
}
return response
}
override fun onPostExecute(result: String?) {
super.onPostExecute(result)
try {
// Extracting JSON returns from the API
val jsonObj = JSONObject(result)
val main = jsonObj.getJSONObject("main")
val sys = jsonObj.getJSONObject("sys")
val wind = jsonObj.getJSONObject("wind")
val weather = jsonObj.getJSONArray("weather").getJSONObject(0)
val updatedAt:Long = jsonObj.getLong("dt")
val updatedAtText = "Updated at: "+ SimpleDateFormat("dd/MM/yyyy H:mm ", Locale.ENGLISH).format(Date(updatedAt*1000))
val temp = main.getDouble("temp").toInt().toString()
val tempMin = main.getDouble("temp_min").toInt().toString()
val tempMax = main.getDouble("temp_max").toInt().toString()
val tempText = "$temp°C"
val tempMinText = "Min Temp: $tempMin°C"
val tempMaxText = "Max Temp: $tempMax°C"
val pressure = main.getString("pressure")
val humidity = main.getString("humidity")
val sunrise:Long = sys.getLong("sunrise")
val sunset:Long = sys.getLong("sunset")
val windSpeed = wind.getDouble("speed").toInt()
val weatherDescription = weather.getString("description")
val address = jsonObj.getString("name")+", "+sys.getString("country")
val icon = weather.getString("icon")
val conditionUrl = "https://openweathermap.org/img/wn/$icon@2x.png"
// Populating extracted data into our views
Picasso.get().load(conditionUrl).into(condition_imageView)
findViewById<TextView>(R.id.address).text = address
findViewById<TextView>(R.id.updated_at).text = updatedAtText
findViewById<TextView>(R.id.status).text = weatherDescription.capitalize()
findViewById<TextView>(R.id.temp).text = tempText
findViewById<TextView>(R.id.temp_min).text = tempMinText
findViewById<TextView>(R.id.temp_max).text = tempMaxText
findViewById<TextView>(R.id.sunrise).text = SimpleDateFormat("HH:mm", Locale.ENGLISH).format(Date(sunrise * 1000))
findViewById<TextView>(R.id.sunset).text = SimpleDateFormat("HH:mm", Locale.ENGLISH).format(Date(sunset * 1000))
findViewById<TextView>(R.id.wind).text = "$windSpeed km/h"
findViewById<TextView>(R.id.pressure).text = "$pressure mb"
findViewById<TextView>(R.id.humidity).text = "$humidity %"
// Views populated, hiding the loader, showing the main design
findViewById<ProgressBar>(R.id.loader).visibility = View.GONE
findViewById<RelativeLayout>(R.id.mainContainer).visibility = View.VISIBLE
}
catch (e: Exception){
findViewById<ProgressBar>(R.id.loader).visibility = View.GONE
findViewById<TextView>(R.id.errorText).visibility = View.VISIBLE
}
}
}
@SuppressLint("MissingPermission")
private fun getLastLocation() {
if(CheckPermission()){
if(isLocationEnabled()){
fusedLocationProviderClient.lastLocation.addOnCompleteListener{task ->
var location = task.result
if(location == null){
getNewLocation()
}else {
locationResult = location
}
}
}else{
Toast.makeText(this, "Location service not enabled", Toast.LENGTH_SHORT).show()
}
}else{
RequestPermission()
}
}
private fun CheckPermission() : Boolean {
if( (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
|| (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) ){
return true
}
return false
}
private fun RequestPermission() {
ActivityCompat.requestPermissions(
this,
arrayOf(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
), PERMISSION_ID
)
}
private fun isLocationEnabled() : Boolean{
var locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == PERMISSION_ID){
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED){
Log.d(TAG, "Permission granted")
}
}
}
@SuppressLint("MissingPermission")
private fun getNewLocation(){
locationRequest = LocationRequest()
locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
locationRequest.interval = 0
locationRequest.fastestInterval = 0
locationRequest.numUpdates = 2
fusedLocationProviderClient!!.requestLocationUpdates(
locationRequest, locationCallback, null
)
}
private val locationCallback = object : LocationCallback() {
override fun onLocationResult(p0: LocationResult?){
var lastLocation = p0?.lastLocation
if (lastLocation != null) {
locationResult = lastLocation
}
}
}
}