我是Kotlin的初学者,我正在尝试构建附近的场所应用程序。
我的应用程序应该像这样工作:
第一页是不同地点类型的列表(例如“餐厅”,“博物馆”,“酒吧”,“标记” ...)。如果用户单击其中一种类型,则会将其发送到带有 map 的新页面。在此 map 上,他们可以看到他们附近所选择地点的类型。为此,我遵循Ed YouTube channel ,该 channel 的功能与我相似,但设计不同。
这里是链接:https://www.youtube.com/watch?v=tI4dV9n6-yE&t=2937s
(我在第3部分)
我的问题如下:我创建了带有列表 View 的第一页,并且当我单击某个元素时,我得到了一张带有我的位置的 map ,但是我没有到附近的地方。
当应用程序运行时,我可以在控制台上看到以下内容:
URL_DEBUG: maps.googleapis.com/maps/api/place/nearbysearch/json?location=0.0,0.0&radius=10000&type=museum&key=MY_API_KEY
当我调用
getUrl()
函数时,我不明白为什么纬度和经度为0。有人能帮我吗?
最佳答案
看起来对nearByPlace
的调用在执行位置回调之前运行。尝试放置以下代码:
val placeChoix = intent.getStringExtra("CHOIX")
when(placeChoix)
{
"Restaurants" -> nearByPlace("restaurant")
"Bars" -> nearByPlace("bar")
"Musées" -> nearByPlace("museum")
"SuperMarché" -> nearByPlace("supermarket")
"Hopital" -> nearByPlace("hospital")
"Magasins" -> nearByPlace("shopping_mall")
}
在回调内部:
private fun buildLocationCallBack() {
locationCallback = object : LocationCallback() {
override fun onLocationResult(p0: LocationResult?) {
mLastLocation = p0!!.locations.get(p0.locations.size-1) //get last location
if(mMarker != null)
{
mMarker!!.remove()
}
latitude = mLastLocation.latitude
longitude = mLastLocation.longitude
//Toast.makeText(applicationContext, "latitude: $latitude \n longitude : $longitude", Toast.LENGTH_LONG).show()
val latLng = LatLng(latitude,longitude)
val markerOptions = MarkerOptions()
.position(latLng)
.title("Votre position")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))
mMarker = mMap.addMarker(markerOptions)
//move camera
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng))
mMap.animateCamera(CameraUpdateFactory.zoomTo(11f))
val placeChoix = intent.getStringExtra("CHOIX")
when(placeChoix)
{
"Restaurants" -> nearByPlace("restaurant")
"Bars" -> nearByPlace("bar")
"Musées" -> nearByPlace("museum")
"SuperMarché" -> nearByPlace("supermarket")
"Hopital" -> nearByPlace("hospital")
"Magasins" -> nearByPlace("shopping_mall")
}
}
}
}
希望这可以帮助!
关于android - 应用程序无法到达附近的地方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61775076/