android - 应用程序无法到达附近的地方

标签 android google-maps kotlin geolocation location

我是Kotlin的初学者,我正在尝试构建附近的场所应用程序。

我的应用程序应该像这样工作:

第一页是不同地点类型的列表(例如“餐厅”,“博物馆”,“酒吧”,“标记” ...)。如果用户单击其中一种类型,则会将其发送到带有 map 的新页面。在此 map 上,他们可以看到他们附近所选择地点的类型。为此,我遵循Ed YouTube channel ,该 channel 的功能与我相似,但设计不同。

这里是链接:https://www.youtube.com/watch?v=tI4dV9n6-yE&t=2937s

  • 第1部分:设置Google Maps
  • 第2部分:获取当前位置
  • 第3部分:获取附近的地方

  • (我在第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/

    相关文章:

    android - 如何将 onDismissListener 写入 AlertDialog 构建器?

    android - 画廊android中的图像之间有很大的空间

    java - 我希望我的 Android 应用程序有一个简单的表单,可以将信息发送到我的 Gmail

    javascript - Google map API 门牌号未显示

    android - 如何使具有服务器流连接的Android grpc客户端保持 Activity 状态?

    android - 我在Android中收到NoClassFoundError

    java - android:checkableBehavior 以编程方式设置

    javascript - 如何在 map 上创建一个覆盖标记,它保持在 react-native 的屏幕中心

    javascript - node.js同步mysql查询

    android - Jetpack 撰写 : Textfield and FAB not using full width