我有新办法来解决这个问题。在这里,我使用 google http 服务来获得基于经纬度的位置信息。你只需要传递 url 经纬度和你的 API 密钥(ex: latlng = 21.1497409,79.0874797000002 & KEY = YOUR API KEY)。这是我在 ExampleServiceClass 的获取服务
addresses = geocoder.getFromLocation(mMap.getCameraPosition().target.latitude, mMap.getCameraPosition().target.longitude, 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5
String locality = addresses.get(0).getLocality(); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
String subLocality = addresses.get(0).getSubLocality(); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
//String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
String address1 = addresses.get(0).getAddressLine(1); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
String address2 = addresses.get(0).getAddressLine(2); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
String city = addresses.get(0).getLocality();
String state = addresses.get(0).getAdminArea();
String country = addresses.get(0).getCountryName();
// String postalCode = addresses.get(0).getPostalCode();
String knownName = addresses.get(0).getFeatureName();
var geocoder;
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(latitude, longitude);
geocoder.geocode(
{'latLng': latlng},
function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
var add= results[0].formatted_address ;
var value=add.split(",");
count=value.length;
country=value[count-1];
state=value[count-2];
city=value[count-3];
alert("city name is: " + city);
}
else {
alert("address not found");
}
}
else {
alert("Geocoder failed due to: " + status);
}
}
import org.json.JSONObject
fun parseLocation(response: String): GeoLocation? {
val geoCodes by lazy { doubleArrayOf(0.0, 0.0) }
val jObj = JSONObject(response)
if (jObj.has(KEY_RESULTS)) {
val jArrResults = jObj.getJSONArray(KEY_RESULTS)
for (i in 0 until jArrResults.length()) {
val jObjResult = jArrResults.getJSONObject(i)
//getting latitude and longitude
if (jObjResult.has(KEY_GEOMETRY)) {
val jObjGeometry = jObjResult.getJSONObject(KEY_GEOMETRY)
if (jObjGeometry.has(KEY_LOCATION)) {
val jObjLocation = jObjGeometry.getJSONObject(KEY_LOCATION)
if (jObjLocation.has(KEY_LAT)) {
geoCodes[0] = jObjLocation.getDouble(KEY_LAT)
}
if (jObjLocation.has(KEY_LNG)) {
geoCodes[1] = jObjLocation.getDouble(KEY_LNG)
}
}
}
var administrativeAreaLevel1: String? = null
//getting city
if (jObjResult.has(KEY_ADDRESS_COMPONENTS)) {
val jArrAddressComponents = jObjResult.getJSONArray(KEY_ADDRESS_COMPONENTS)
for (i in 0 until jArrAddressComponents.length()) {
val jObjAddressComponents = jArrAddressComponents.getJSONObject(i)
if (jObjAddressComponents.has(KEY_TYPES)) {
val jArrTypes = jObjAddressComponents.getJSONArray(KEY_TYPES)
for (j in 0 until jArrTypes.length()) {
when (jArrTypes.getString(j)) {
VALUE_LOCALITY, VALUE_POSTAL_TOWN -> {
return GeoLocation(jObjAddressComponents.getString(KEY_LONG_NAME), *geoCodes)
}
ADMINISTRATIVE_AREA_LEVEL_1 -> {
administrativeAreaLevel1 = jObjAddressComponents.getString(KEY_LONG_NAME)
}
else -> {
}
}
}
}
}
for (i in 0 until jArrAddressComponents.length()) {
val jObjAddressComponents = jArrAddressComponents.getJSONObject(i)
if (jObjAddressComponents.has(KEY_TYPES)) {
val jArrTypes = jObjAddressComponents.getJSONArray(KEY_TYPES)
val typeList = ArrayList<String>()
for (j in 0 until jArrTypes.length()) {
typeList.add(jArrTypes.getString(j))
}
if (typeList.contains(VALUE_SUBLOCALITY)) {
var hasSubLocalityLevel = false
typeList.forEach { type ->
if (type.contains(VALUE_SUBLOCALITY_LEVEL)) {
hasSubLocalityLevel = true
if (type == VALUE_SUBLOCALITY_LEVEL_1) {
return GeoLocation(jObjAddressComponents.getString(KEY_LONG_NAME), *geoCodes)
}
}
}
if (!hasSubLocalityLevel) {
return GeoLocation(jObjAddressComponents.getString(KEY_LONG_NAME), *geoCodes)
}
}
}
}
}
if (geoCodes.isNotEmpty()) return GeoLocation(administrativeAreaLevel1, geoCodes = *geoCodes)
}
}
return null
}
data class GeoLocation(val latitude: Double = 0.0, val longitude: Double = 0.0, val city: String? = null) : Parcelable {
constructor(city: String? = null, vararg geoCodes: Double) : this(geoCodes[0], geoCodes[1], city)
constructor(source: Parcel) : this(source.readDouble(), source.readDouble(), source.readString())
companion object {
@JvmField
val CREATOR = object : Parcelable.Creator<GeoLocation> {
override fun createFromParcel(source: Parcel) = GeoLocation(source)
override fun newArray(size: Int): Array<GeoLocation?> = arrayOfNulls(size)
}
}
override fun writeToParcel(dest: Parcel, flags: Int) {
dest.writeDouble(latitude)
dest.writeDouble(longitude)
dest.writeString(city)
}
override fun describeContents() = 0
}
private fun getAddress(latLng: LatLng): String {
// 1
val geocoder = Geocoder(this)
val addresses: List<Address>?
var city = "no"
try {
addresses = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1)
if (null != addresses && !addresses.isEmpty()) { //prevent from error
//sometimes the city comes in locality, sometimes in subAdminArea.
if (addresses[0].locality == null) {
city = addresses[0].subAdminArea
} else {
city = addresses[0].locality
}
}
} catch (e: IOException) {
Log.e("MapsActivity", e.localizedMessage)
}
return city
}