作为Google Place Picker的替代方案,您可以使用
Leku。这是一个易于使用的库,适合您的需求。
您只需要添加依赖项:
dependencies {
implementation 'com.schibstedspain.android:leku:7.0.0'
}
在您的清单文件中声明该活动:
<activity
android:name="com.schibstedspain.leku.LocationPickerActivity"
android:label="@string/leku_title_activity_location_picker"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
android:windowSoftInputMode="adjustPan"
android:parentActivityName=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/leku_searchable" />
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
在需要的地方启动活动:
val locationPickerIntent = LocationPickerActivity.Builder()
.build(applicationContext)
startActivityForResult(locationPickerIntent, MAP_BUTTON_REQUEST_CODE)
最后,处理结果:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (resultCode == Activity.RESULT_OK && data != null) {
Log.d("RESULT****", "OK")
if (requestCode == 1) {
val latitude = data.getDoubleExtra(LATITUDE, 0.0)
Log.d("LATITUDE****", latitude.toString())
val longitude = data.getDoubleExtra(LONGITUDE, 0.0)
Log.d("LONGITUDE****", longitude.toString())
val address = data.getStringExtra(LOCATION_ADDRESS)
Log.d("ADDRESS****", address.toString())
val postalcode = data.getStringExtra(ZIPCODE)
Log.d("POSTALCODE****", postalcode.toString())
val bundle = data.getBundleExtra(TRANSITION_BUNDLE)
Log.d("BUNDLE TEXT****", bundle.getString("test"))
val fullAddress = data.getParcelableExtra<Address>(ADDRESS)
if (fullAddress != null) {
Log.d("FULL ADDRESS****", fullAddress.toString())
}
val timeZoneId = data.getStringExtra(TIME_ZONE_ID)
Log.d("TIME ZONE ID****", timeZoneId)
val timeZoneDisplayName = data.getStringExtra(TIME_ZONE_DISPLAY_NAME)
Log.d("TIME ZONE NAME****", timeZoneDisplayName)
} else if (requestCode == 2) {
val latitude = data.getDoubleExtra(LATITUDE, 0.0)
Log.d("LATITUDE****", latitude.toString())
val longitude = data.getDoubleExtra(LONGITUDE, 0.0)
Log.d("LONGITUDE****", longitude.toString())
val address = data.getStringExtra(LOCATION_ADDRESS)
Log.d("ADDRESS****", address.toString())
val lekuPoi = data.getParcelableExtra<LekuPoi>(LEKU_POI)
Log.d("LekuPoi****", lekuPoi.toString())
}
}
if (resultCode == Activity.RESULT_CANCELED) {
Log.d("RESULT****", "CANCELLED")
}
}
并且你已经准备好了。 ;)