AndroidStudio中如何查找附近的地点并将它们作为标记显示出来?
创始人
2024-08-21 02:30:07
0

步骤1:添加Google Maps API

在Android Studio中的build.gradle(module)文件中添加Google Maps API依赖项:

dependencies {
  implementation 'com.google.android.gms:play-services-maps:17.0.0'
}

步骤2:获取用户位置权限

在AndroidManifest.xml文件中添加以下代码以获取用户位置权限:


步骤3:创建GoogleMap对象

在activity中创建GoogleMap对象:

GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
            .getMapAsync(this);
}

步骤4:获取附近的地点

使用Google Places API获取附近的地点:

private void getNearbyPlaces(LatLng latLng) {

    String type = "restaurant"; //查找餐馆
    double radius = 1000; //1000米以内

    String url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=" +
            latLng.latitude + "," + latLng.longitude +
            "&radius=" + radius +
            "&type=" + type +
            "&key=" + API_KEY;

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null,
            response -> {
                try {
                    JSONArray jsonArray = response.getJSONArray("results");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        JSONObject location = jsonObject.getJSONObject("geometry").getJSONObject("location");
                        double lat = location.getDouble("lat");
                        double lng = location.getDouble("lng");
                        String name = jsonObject.getString("name");
                        MarkerOptions markerOptions = new MarkerOptions();
                        markerOptions.position(new LatLng(lat, lng));
                        markerOptions.title(name);
                        mMap.addMarker(markerOptions);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }, error -> {
                Log.e(TAG,

相关内容

热门资讯

开挂辅助脚本"wep... >>您好:wepoker网页版透视方法确实是有挂的,很多玩家在这款wepoker网页版透视方法游戏中...
开挂辅助软件"佛手在... 佛手在线大菠萝技巧开挂教程视频分享装挂详细步骤在当今的网络游戏中,佛手在线大菠萝技巧作为一种经典的娱...
开挂辅助安装"wep... 开挂辅助安装"wepoker插件程序激活码"开挂(透视)辅助脚本(有挂规律)1、下载安装好wepok...
开挂辅助软件"黑侠破... 开挂辅助软件"黑侠破解wepoker"开挂(透视)辅助下载(有挂教程);打开点击测试直接进入微信(1...
开挂辅助工具"hhp... 开挂辅助工具"hhpoker脚本下载"开挂(透视)辅助安装(今日头条) 【无需打开直接搜索加薇136...
开挂辅助脚本"wep... 开挂辅助脚本"wepoker怎么下载游戏"开挂(透视)辅助脚本(有挂透视)wepoker怎么下载游戏...
开挂辅助工具"德扑圈... 开挂辅助工具"德扑圈有透视吗"开挂(透视)辅助下载(有挂实锤);无需打开直接搜索打开薇:136704...
开挂辅助脚本"wep... 开挂辅助脚本"wepoker怎么增加运气"开挂(透视)辅助平台(有挂秘籍)>>您好:软件加13670...
开挂辅助软件"hhp... 开挂辅助软件"hhpoker辅助软件下载"开挂(透视)辅助下载(真是有挂);亲,hhpoker辅助软...
开挂辅助平台"hhp... 开挂辅助平台"hhpoker万能辅助器"开挂(透视)辅助下载(有挂神器);亲,hhpoker万能辅助...