使用Kotlin在Android Studio中搜索附近的位置
我想在用户当前位置的一定半径内搜索附近的地方(例如:医院、学校、普通诊所等)。 我成功地获得了用户的当前位置,但不知道如何在半径范围内寻找附近的地方。 我已成功设置html属性以获取附近的位置,如下所示: (我把钥匙放好了) 但我似乎不知道在哪里以及如何实现这个Url。除非有人能告诉我如何使用这个URL。这也会有很大的帮助! 谢谢大家! 这是我目前的代码:使用Kotlin在Android Studio中搜索附近的位置,android,android-studio,google-maps,kotlin,google-nearby,Android,Android Studio,Google Maps,Kotlin,Google Nearby,我想在用户当前位置的一定半径内搜索附近的地方(例如:医院、学校、普通诊所等)。 我成功地获得了用户的当前位置,但不知道如何在半径范围内寻找附近的地方。 我已成功设置html属性以获取附近的位置,如下所示: (我把钥匙放好了) 但我似乎不知道在哪里以及如何实现这个Url。除非有人能告诉我如何使用这个URL。这也会有很大的帮助! 谢谢大家! 这是我目前的代码: package com.sampathpatro.vipalsiddh import android.content.pm.Package
package com.sampathpatro.vipalsiddh
import android.content.pm.PackageManager
import android.location.Address
import android.location.Geocoder
import android.location.Location
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationServices
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.OnMapReadyCallback
import com.google.android.gms.maps.SupportMapFragment
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.Marker
import com.google.android.gms.maps.model.MarkerOptions
import java.io.IOException
class MapsActivity : AppCompatActivity(), OnMapReadyCallback, GoogleMap.OnMarkerClickListener {
private lateinit var map: GoogleMap
private lateinit var fusedLocationProviderClient: FusedLocationProviderClient
private lateinit var lastLocation: Location
private fun placeMarkerOnMap(location: LatLng){
val markerOptions = MarkerOptions().position(location)
val titleString = getAddress(location)
markerOptions.title(titleString)
map.addMarker(markerOptions)
}
private fun getAddress(latLng: LatLng): String {
val geocoder = Geocoder (this)
val addresses: List<Address>
val address: Address?
var addressText = ""
try {
addresses = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1)
if (null != addresses && addresses.isNotEmpty()){
address = addresses[0]
for (i in 0 until address.maxAddressLineIndex){
addressText += if (i==0) address.getAddressLine(i) else
"\n" + address.getAddressLine(i)
}
}
} catch (e: IOException){
Log.e("MapsActivity", e.localizedMessage)
}
return addressText
}
companion object {
private const val LOCATION_PERMISSION_REQUEST_CODE = 1
}
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
fusedLocationProviderClient.lastLocation.addOnSuccessListener(this) { location ->
if (location != null){
lastLocation = location
val currentLatLng = LatLng(location.latitude, location.longitude)
placeMarkerOnMap(currentLatLng)
map.animateCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng, 12f))
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_maps)
// 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)
fusedLocationProviderClient = 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.uiSettings.isZoomControlsEnabled = true
map.setOnMarkerClickListener(this)
setUpMap()
}
override fun onMarkerClick(p0: Marker?) = false
} ```
package com.sampathro.vipalsiddh
导入android.content.pm.PackageManager
导入android.location.Address
导入android.location.Geocoder
导入android.location.location
导入android.os.Bundle
导入android.util.Log
导入androidx.appcompat.app.appcompat活动
导入androidx.core.app.ActivityCompat
导入com.google.android.gms.location.FusedLocationProviderClient
导入com.google.android.gms.location.LocationServices
导入com.google.android.gms.maps.CameraUpdateFactory
导入com.google.android.gms.maps.GoogleMap
导入com.google.android.gms.maps.OnMapReadyCallback
导入com.google.android.gms.maps.SupportMapFragment
导入com.google.android.gms.maps.model.LatLng
导入com.google.android.gms.maps.model.Marker
导入com.google.android.gms.maps.model.MarkerOptions
导入java.io.IOException
类MapsActivity:AppCompatActivity(),OnMapReadyCallback,GoogleMap.OnMarkerClickListener{
私有lateinit变量映射:GoogleMap
私有lateinit变量fusedLocationProviderClient:fusedLocationProviderClient
私有lateinit var lastLocation:位置
私人娱乐场所地图(位置:LatLng){
val markerOptions=markerOptions().位置(位置)
val titleString=getAddress(位置)
标记选项。标题(标题串)
map.addMarker(markerOptions)
}
private-fun-getAddress(latLng:latLng):字符串{
val geocoder=地理编码器(本)
val地址:列表
val地址:地址?
var addressText=“”
试一试{
addresses=geocoder.getFromLocation(latLng.latitude,latLng.longitude,1)
if(null!=地址和地址(&addresses.isNotEmpty()){
地址=地址[0]
for(0中的i,直到address.maxAddressLineIndex){
addressText+=if(i==0)address.getAddressLine(i)else
“\n”+地址。getAddressLine(i)
}
}
}捕获(e:IOException){
Log.e(“MapsActivity”,e.localizedMessage)
}
返回地址文本
}
伴星{
私有const val LOCATION\u PERMISSION\u REQUEST\u CODE=1
}
私人娱乐设置地图(){
if(ActivityCompat.checkSelfPermission(这个,android.Manifest.permission.ACCESS\u FINE\u位置)=
PackageManager.权限(已授予){
ActivityCompat.requestPermissions(这个,arrayOf(android.Manifest.permission.ACCESS\u FINE\u位置),
位置\权限\请求\代码)
返回
}
map.isMyLocationEnabled=true
fusedLocationProviderClient.lastLocation.addOnSuccessListener(此){location->
如果(位置!=null){
lastLocation=位置
val currentLatLng=LatLng(位置.纬度,位置.经度)
地点标记地图(当前LATLNG)
map.animateCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng,12f))
}
}
}
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity\u映射)
//获取SupportMapFragment,并在地图准备好使用时收到通知。
val mapFragment=supportFragmentManager
.findFragmentById(R.id.map)作为SupportMapFragment
getMapAsync(此)
fusedLocationProviderClient=LocationServices.getFusedLocationProviderClient(此)
}
/**
*一旦可用,就可以操纵贴图。
*当映射准备好使用时,将触发此回调。
*这是我们可以添加标记或线条、添加侦听器或移动摄影机的地方。在这种情况下,
*我们只是在澳大利亚悉尼附近加了一个标记。
*如果设备上未安装Google Play服务,系统将提示用户安装
*它位于SupportMapFragment内。此方法仅在用户
*已安装Google Play服务并返回应用程序。
*/
在mapready上覆盖乐趣(谷歌地图:谷歌地图){
地图=谷歌地图
map.uiSettings.isZoomControlsEnabled=true
map.setOnMarkerClickListener(此)
setUpMap()
}
在MarkerClick上覆盖乐趣(p0:Marker?)=false
} ```
使用OkHttp库安全地进行API调用。
将库包含在gradle文件中
将GSON
library与它一起使用,它将使您的操作变得简单,为简单起见,我将通过JSONObject
在模块级渐变中,包括以下内容
implementation("com.squareup.okhttp3:okhttp:4.8.1")
使用此方法进行调用
fun makeApiCall(location:Location){
val request = Request.Builder().url("https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=${location.latitude},${location.longitude}&radius=1500&type=restaurant&key=YOUR_API_KEY")
.build()
val response = OkHttpClient().newCall(request).execute().body().string()
val jsonObject = JSONObject(response) // This will make the json below as an object for you
// You can access all the attributes , nested ones using JSONArray and JSONObject here
}
当您进行api调用时,会得到如下响应
{
"debug_log" : {
"line" : []
},
"html_attributions" : [],
"logging_info" : {
"experiment_id" : [],
"query_geographic_location" : "AU"
},
"results" : [
{
"geometry" : {
"location" : {
"lat" : -33.86879,
"lng" : 151.194217
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
"id" : "21a0b251c9b8392186142c798263e289fe45b4aa",
"name" : "Rhythmboat Cruises",
"opening_hours" : {
"open_now" : false,
"weekday_text" : []
},
"photos" : [
{
"height" : 426,
"html_attributions" : [
"\u003ca href=\"https://www.google.com/maps/views/profile/104066891898402903288\"\u003eRhythmboat Cruises\u003c/a\u003e"
],
"photo_reference" : "CmRdAAAA-YL_I_Gk02blOX6S0nKHry8PFu9pDyp3Y9AnqISsa3Eq8mkbdD5mXuu1Fax60s0nSy3iiX-h5j-ztyLHcc1-782MsLQsgLLa4t3ZgDmCMll-a8ABapZGnZwDKByk67LFEhBgedv_u_eYFsEo9ay8jxJjGhTUHKPJ4G82vBJqSNliuv7UlAtclw",
"width" : 640
}
],
"place_id" : "ChIJyWEHuEmuEmsRm9hTkapTCrk",
"reference" : "CnRmAAAAvQlMKw-XtxEY4vWFCvudF7CEMQGI5ycNbfVgGl9rAF75fdiPOiLJw1k9NL2v8ZIJsOJuRS3Lm9Dw1vga4ajycAs7PlxN1MVnnYT9la0pBvEvSQNlyvszKANS1R4P7Mvk_jhqswMggqCUtwJ13LN2hRIQOiAkLTWUi3DOjVVOw7J5IRoUb_cJyJaJNqKdmkDM2f0OjQjh9F0",
"scope" : "GOOGLE",
"types" : [ "restaurant", "food", "point_of_interest", "establishment" ],
"vicinity" : "Pyrmont Bay Wharf (Near Australia Maritime Museum), Pyrmont, NSW 2009"
},
{
"geometry" : {
"location" : {
"lat" : -33.867591,
"lng" : 151.201196
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
"id" : "a97f9fb468bcd26b68a23072a55af82d4b325e0d",
"name" : "Australian Cruise Group",
"opening_hours" : {
"open_now" : false,
"weekday_text" : []
},
"photos" : [
{
"height" : 1331,
"html_attributions" : [
"\u003ca href=\"https://www.google.com/maps/views/profile/110751364053842618118\"\u003eAustralian Cruise Group\u003c/a\u003e"
],
"photo_reference" : "CmRdAAAAtEoj29FJcNBccrsu6bHt0xgwVGhYlciCY1fe6gTr_d5_KkeP3LITnOwnpNRJWnX39B04-aIBOKXKJH6ltx948T5vWIYBoah1yZDXsWngWZ5kMsK7xyCB5P_q_xBIBxxUEhAYWeB4PiOm_Jy093fB-j0iGhSYmIs9xB2aa6u-RH8V8lZEk-Q5ig",
"width" : 2000
}
],
"place_id" : "ChIJrTLr-GyuEmsRBfy61i59si0",
"reference" : "CnRqAAAAFbukrZvRNsc05TreHUCrPEya5NcN9v0fFLLaK-D1fSyxFTuQlUDhDstU3qwXKw_fADX4W6guUkexax1nufgiYIuGCKoZPEnup1r-LhGGNz9dn1uf9Of5iOtZ1XgCeDjJaYvGbSB3C0pAXL8r9kOsmhIQbx2Sia2DAWTjtSZwuh5aehoUhM6upqBCDLhGruZAGGsCOwAiIq8",
"scope" : "GOOGLE",
"types" : [
"restaurant",
"travel_agency",
"food",
"point_of_interest",
"establishment"
],
"vicinity" : "32 The Promenade, King Street Wharf 5, Sydney"
},
{
"geometry" : {
"location" : {
"lat" : -33.870943,
"lng" : 151.190311
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/bar-71.png",
"id" : "e644f7f34cf875b9919c6548f1b721947362850a",
"name" : "Lunch Cruise with Jazz on Sydney Harbour",
"opening_hours" : {
"open_now" : false,
"weekday_text" : []
},
"photos" : [
{
"height" : 292,
"html_attributions" : [
"\u003ca href=\"https://www.google.com/maps/views/profile/105423912060796272053\"\u003eFrom a Google User\u003c/a\u003e"
],
"photo_reference" : "CmRdAAAAR4bqFTKYWdBwZwdBdON_JRD7V_joTwwIPwRUpZIZWkSSd8GQ3P2O-_aQbUJdL2RhoAyzCUIF0f--DI4oXFneTpj5zZfFq-iFiT7i_x0tjnDveIY8tJv-6o0uWSSjYqabEhCKqQWZqrKAoddjDcc64N48GhQZ2T1_ntPzNKCooHpZzlYQ7AxFOA",
"width" : 438
}
],
"place_id" : "ChIJLfySpTOuEmsRPCRKrzl8ZEY",
"reference" : "CoQBewAAAFd2fO_YWGTiT4RzXWb5tsOuOt7YyV_ScQOwm0tqJSrAyACCczeOzV-P_mgZLro1oKP_34Nt0nVC_1OEKAQUcd7cUm7xmAMSX-EkbSWiD0kOWGgGgKuDRtb0t_8qsxBGU_izugWCyK7SRWezTxELYNdkS0OEiSWPnvhxvXuQktBBEhAAtEe7fagW2kUR14T1QpVsGhQBO7YpIyYSPvo4zUJuL_bX30nJZw",
"scope" : "GOOGLE",
"types" : [ "bar", "restaurant", "food", "point_of_interest", "establishment" ],
"vicinity" : "37 Bank St, Pyrmont"
}
],
"status" : "OK"
}
GoogleMaps'-正是PlacesAPI,但它是为Android设计的,在Android中,您不需要使用HTTP请求,而是使用SDK本身的方法。例如,“当前地点”将帮助您找到设备当前所在的本地企业或其他地点。要开始,您可以检查以下链接: