Skip to content

Instantly share code, notes, and snippets.

@vamjakuldip
Created July 1, 2020 14:36
Show Gist options
  • Save vamjakuldip/de486177ba1dfeeef486c76fa43c5490 to your computer and use it in GitHub Desktop.
Save vamjakuldip/de486177ba1dfeeef486c76fa43c5490 to your computer and use it in GitHub Desktop.
- get location fro geo Uri.
private fun parseGeoIntent(uri: Uri): Location? {
var schemeSpecific: String = uri.schemeSpecificPart ?: ""
if (uri.schemeSpecificPart.contains("%2B")) {
schemeSpecific = schemeSpecific.replace("+", "%2B")
}
var name: String? = null
val namePattern: Pattern = Pattern.compile("[\\+\\s]*\\((.*)\\)[\\+\\s]*$")
val nameMatcher: Matcher = namePattern.matcher(schemeSpecific)
if (nameMatcher.find()) {
name = URLDecoder.decode(nameMatcher.group(1))
if (name != null) {
schemeSpecific = schemeSpecific.substring(0, nameMatcher.start())
}
}
val positionPart: String
val queryStartIndex = schemeSpecific.indexOf('?')
if (queryStartIndex == -1) {
positionPart = schemeSpecific
} else {
positionPart = schemeSpecific.substring(0, queryStartIndex)
}
val positionPattern: Pattern = Pattern.compile("([+-]?\\d+(?:\\.\\d+)?),\\s?([+-]?\\d+(?:\\.\\d+)?)")
val positionMatcher: Matcher = positionPattern.matcher(positionPart)
var lat = 0.0
var lon = 0.0
if (positionMatcher.find()) {
lat = java.lang.Double.valueOf(positionMatcher.group(1))
lon = java.lang.Double.valueOf(positionMatcher.group(2))
}
val location = Location("")
location.latitude = lat
location.longitude = lon
return location
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment