'Geocode is throwing an error in android. Trying to build a cab booking app
This is a user app cab booking system in android, Geocode throwing the error. Only thing I get is Unable to get location please try again...
I am not able to understand the code fully as I a beginner, I got this code to debug from my manager. Please help
Google API are enabled and API key is working fine. Please Help
* Fetch Location from address if Geocode available get from geocode otherwise get location from google
*/
fun fetchLocation(addresss: String, type: String) {
getAddress = addresss
object : AsyncTask<Void, Void, String>() {
var locations: String = ""
override fun doInBackground(vararg params: Void): String? {
if (Geocoder.isPresent())
// Check geo code available or not
{
try {
println("Geocoder.isPresent")
val geocoder = Geocoder(applicationContext, Locale.getDefault())
val address: List<Address>?
// May throw an IOException
address = geocoder.getFromLocationName(getAddress, 5)
if (address == null) {
return null
}
val location = address[0]
countrys = address[0].countryName
if (isPickup) {
if ("" != countrys)
pickipCountry = getAddress
} else {
destCountry = getAddress
if ("" == destCountry)
destCountry = pickipCountry
}
location.latitude
location.longitude
lat = location.latitude.toString()
log = location.longitude.toString()
locations = "$lat,$log"
} catch (ignored: Exception) {
ignored.printStackTrace()
// after a while, Geocoder start to throw "Service not available" exception. really weird since it was working before (same device, same Android version etc..
}
}
return if (locations != null)
// i.e., Geocoder succeed
{
locations
} else
// i.e., Geocoder failed
{
fetchLocationUsingGoogleMap() // If geocode not available or location null call google API
}
}
// Geocoder failed :-(
// Our B Plan : Google Map
private fun fetchLocationUsingGoogleMap(): String? {
println("fetchLocationUsingGoogleMap")
getAddress = getAddress.replace(" ".toRegex(), "%20")
val googleMapUrl = ("https://maps.google.com/maps/api/geocode/json?address=" + getAddress + "&sensor=false"
+ "&key=" + sessionManager.googleMapKey)
println("GoogleMapUrl ${googleMapUrl}")
try {
val googleMapResponse = JSONObject(ANDROID_HTTP_CLIENT.execute(HttpGet(googleMapUrl),
BasicResponseHandler()))
// many nested loops.. not great -> use expression instead
// loop among all results
if (googleMapResponse.length() > 0) {
val longitute = (googleMapResponse.get("results") as JSONArray).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getString("lng")
val latitude = (googleMapResponse.get("results") as JSONArray).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getString("lat")
val len = (googleMapResponse.get("results") as JSONArray).getJSONObject(0)
.getJSONArray("address_components").length()
for (i in 0 until len) {
if ((googleMapResponse.get("results") as JSONArray).getJSONObject(0)
.getJSONArray("address_components").getJSONObject(i).getJSONArray("types").getString(0) == "country") {
countrys = (googleMapResponse.get("results") as JSONArray).getJSONObject(0)
.getJSONArray("address_components").getJSONObject(i).getString("long_name")
}
}
if (isPickup) {
if ("" != countrys)
pickipCountry = countrys
} else {
destCountry = countrys
}
return "$latitude,$longitute"
} else {
return null
}
} catch (ignored: Exception) {
ignored.printStackTrace()
}
return null
}
override fun onPostExecute(location: String?) {
if (location != null) {
try{
val parts = location.split(",".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
val lat = java.lang.Double.valueOf(parts[0])
val lng = java.lang.Double.valueOf(parts[1])
val latLng = LatLng(lat, lng)
if ("homeclick" == type) {
if (isPickup) {
Constants.changedPickupLatlng = latLng
markerPoints.set(0, latLng)
pickupaddress.setText(getAddress)
homeClick()
} else {
markerPoints.set(1, latLng)
destadddress.setText(getAddress)
homeClick()
}
} else if ("workclick" == type) {
if (isPickup) {
Constants.changedPickupLatlng = latLng
markerPoints.set(0, latLng)
pickupaddress.setText(getAddress)
workClick()
} else {
markerPoints.set(1, latLng)
destadddress.setText(getAddress)
workClick()
}
} else if ("searchitemclick" == type) {
if ("" == countrys)
countrys = pickipCountry
searchItemClick(latLng, countrys)
}
}catch (e: Exception){
e.printStackTrace()
}
} else {
commonMethods.showMessage(this@PlaceSearchActivity, dialog, "Unable to get location please try again...")
commonMethods.hideProgressDialog()
}
}
}.execute()
}```
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|