美文网首页
Android Google Map搜索地图中心点附近的地址

Android Google Map搜索地图中心点附近的地址

作者: itfitness | 来源:发表于2021-12-11 17:30 被阅读0次

目录

效果展示

实现步骤

1.集成Google地图

这里可以参考这篇文章:Android 使用Google地图

2.地图中央显示位置标志

因为Google地图没有像高德地图那样可以让Mark显示在地图上固定位置的API,因此这里我直接在布局里加了个图片,这样看着就像在地图中一样,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

    </data>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <com.google.android.gms.maps.MapView
            android:id="@+id/map"
            android:layout_marginTop="-100dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:cameraZoom="16"
            app:mapType="normal" />
        <ImageView
            android:layout_gravity="center"
            android:src="@drawable/svg_location"
            android:layout_width="50dp"
            android:layout_height="50dp"/>
    </FrameLayout>
</layout>
3.获取地图中心的位置
override fun onMapReady(map: GoogleMap?) {
        map?.let {
            googleMap = it
        }
        //默认设置一个位置,要不获取不到位置的话就到海里了
        val currencyLatLng = LatLng(36.659584, 117.144005)
        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currencyLatLng, 16f))
        //获取地图中心位置
        googleMap.setOnCameraMoveListener {
            with(googleMap.cameraPosition.target){
                Log.e("地图中心位置","Lat:$latitude,Lng:$longitude")
            }
        }
    }
4.使用Google API获取周围地址

这里我们使用的是这个API:https://maps.googleapis.com/maps/api/geocode/json?latlng=lat,lng&language=zh&fields=formatted_address,name,rating,geometry&key=你的key
这里我们创建了一个ViewModule来请求附近地址,网络请求的方式可以参考这篇文章:Android协程+Retrofit简化网络请求

class MainModel:ViewModel(){
    var userInfo = MutableLiveData<GeocodeAddressBean>()
    /**
     * 根据经纬度获取地址
     */
    fun getAddress(latitude: Double, longitude: Double){
        viewModelScope.launch {
            val latlng = "$latitude,$longitude"
            val fields = "formatted_address,name,rating,geometry"
            val key = "你的Key"
            RetrofitManger.getApiService().getAddress(
                latlng, "zh", fields, key
            ).putValue2Data(GeocodeAddressBean::class.java,userInfo)
        }
    }
}

在获取到地图中心点坐标后就进行请求

//获取地图中心位置
        googleMap.setOnCameraMoveListener {
            with(googleMap.cameraPosition.target){
                Log.e("地图中心位置","Lat:$latitude,Lng:$longitude")
                mainModel.getAddress(latitude,longitude)
            }
        }

然后我们可以通过列表将地址数据展示出来,另外文章开头的地址列表可以上拉下拉的效果我是用了之前写到一个自定义View实现的,可以参考我的这篇文章:Android自定义View实现淘宝物流详情效果,我用的是底下额外补充的那个案例,加上了对RecyclerView的适配

案例源码

具体的可以下载源码查看:https://gitee.com/itfitness/google-map-poi

相关文章

网友评论

      本文标题:Android Google Map搜索地图中心点附近的地址

      本文链接:https://www.haomeiwen.com/subject/djwcfrtx.html