嗨,我想使用 placeID 获取地点详细信息,并使用 Kotlin 在 Android 地图活动中添加标记。 我已经用 latlng 完成了它,但没有使用 placeID . 可以有任何愉悦的帮助。 我用拉特伦的代码.
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
val mdis= LatLng(1.301440, 103.847980)
mMap.addMarker(MarkerOptions().position(mdis).title("MDIS").icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)))
mMap.moveCamera(CameraUpdateFactory.newLatLng(mdis))
mMap.animateCamera(CameraUpdateFactory.zoomIn())
mMap.animateCamera(CameraUpdateFactory.zoomTo(15F),2000,null)
mMap.uiSettings.isZoomControlsEnabled=true
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mMap.isMyLocationEnabled = true
}
else {//condition for Marshmello and above
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), MY_PERMISSION_FINE_LOCATION)
}
}
mMap.setOnMarkerClickListener(this)
}
尝试了这个,并在 placesClient.fetchPlace(request(.addOnSuccessListener(response( ->{ 收到错误 错误提示类型不匹配 必需:成功侦听器 找到 : 获取位置响应
这是我更新的代码:
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
var mdisplaceId = "ChIJRwCirzca2jERhd68E52R_2Q"
//val placesClient = Places.createClient(this)
var placeFields : List<Place.Field>
placeFields = Arrays.asList(Place.Field.ID , Place.Field.NAME)
var request : FetchPlaceRequest
request = FetchPlaceRequest.newInstance(mdisplaceId, placeFields)
lateinit var response : FetchPlaceResponse
lateinit var placesClient : PlacesClient
placesClient.fetchPlace(request).addOnSuccessListener(response) ->{ //error at this line near response
var place : Place
place = response.getPlace()
})
}
使用地理编码 api,
https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=YOUR_API_KEY
这将返回以下 JSON 响应,
{
"results" : [
{
"address_components" : [
{
"long_name" : "277",
"short_name" : "277",
"types" : [ "street_number" ]
},
{
"long_name" : "Bedford Avenue",
"short_name" : "Bedford Ave",
"types" : [ "route" ]
},
{
"long_name" : "Williamsburg",
"short_name" : "Williamsburg",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Brooklyn",
"short_name" : "Brooklyn",
"types" : [ "political", "sublocality", "sublocality_level_1" ]
},
{
"long_name" : "Kings County",
"short_name" : "Kings County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "New York",
"short_name" : "NY",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "11211",
"short_name" : "11211",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "277 Bedford Ave, Brooklyn, NY 11211, USA",
"geometry" : {
"location" : {
"lat" : 40.7142205,
"lng" : -73.9612903
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 40.71556948029149,
"lng" : -73.95994131970849
},
"southwest" : {
"lat" : 40.7128715197085,
"lng" : -73.9626392802915
}
}
},
"place_id" : "ChIJd8BlQ2BZwokRAFUEcm_qrcA",
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
从几何对象获取经度,并在谷歌地图上添加标记。