首页 > 解决方案 > 使用 Google 地图显示特定地点,但使用代码

问题描述

我正在使用 kotlin 开发一个应用程序,在其中我实现了谷歌地图及其 API,或者我获得了位置和所有内容,但我需要的是过滤特定类型的地点,它将在应用程序中预定义,并且它的工作原理当进行搜索时,例如使用医院,并显示附近的医院,您将如何从代码中进行此搜索?

class HomeActivity : AppCompatActivity(), OnMapReadyCallback, GoogleMap.OnMarkerClickListener {
private lateinit var fusedLocationClient : FusedLocationProviderClient

private lateinit var lastLocation: Location

companion object {
    private const val LOCATION_PERMISSION_REQUEST_CODE = 1
}

private lateinit var map: GoogleMap

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_home)
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    val mapFragment = supportFragmentManager
        .findFragmentById(R.id.map) as SupportMapFragment
    mapFragment.getMapAsync(this)

    fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
}



/**
 * Manipulates the map once available.
 * This callback is triggered when the map is ready to be used.
 * This is where we can add markers or lines, add listeners or move the camera. In this case,
 * we just add a marker near Sydney, Australia.
 * If Google Play services is not installed on the device, the user will be prompted to install
 * it inside the SupportMapFragment. This method will only be triggered once the user has
 * installed Google Play services and returned to the app.
 */
override fun onMapReady(googleMap: GoogleMap) {
    map = googleMap



    map.setOnMarkerClickListener(this)
    map.uiSettings.isZoomControlsEnabled = true

    setUpMap()

}



private fun setUpMap(){
    if(ActivityCompat.checkSelfPermission(this,
            android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this,
            arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION), LOCATION_PERMISSION_REQUEST_CODE)
        return
    }

    map.isMyLocationEnabled = true
    map.mapType = GoogleMap.MAP_TYPE_HYBRID




    fusedLocationClient.lastLocation.addOnSuccessListener (this){location:Location ->
        lastLocation = location
        val currentLatLong = LatLng(location.latitude, location.longitude)
        map.animateCamera(CameraUpdateFactory.newLatLngZoom(currentLatLong, 16f))


    }
}

override fun onMarkerClick(p0: Marker?): Boolean =  false

这是我目前拥有的代码,它是控制地图上所有内容的代码

我要求你给我看这样的网站。

我搜索了“医院”,它用标记向我展示了该地区的医院,我需要在谷歌地图的应用搜索医院中看到类似的内容

标签: androidandroid-studiogoogle-maps

解决方案


推荐阅读