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,

相关内容

热门资讯

透视了解!wepoker可以开... 透视了解!wepoker可以开辅助,wepoker插件程序激活码“解密开挂透视挂辅助方法”1、在we...
透视实锤!德州真人透视脚本,w... 透视实锤!德州真人透视脚本,wepoker破解游戏盒子“分享开挂透视挂辅助插件”1、很好的工具软件,...
透视辅助!poker worl... 透视辅助!poker world辅助,hhpoker透视脚本“曝光开挂透视挂辅助神器”1、hhpok...
透视教学!wpk官网下载链接,... 透视教学!wpk官网下载链接,wepoker私人局开挂视频“解密开挂透视挂辅助教程”1)wepoke...
透视实锤!约局吧辅助器,aap... 透视实锤!约局吧辅助器,aapoker可以控制牌“曝光开挂透视挂辅助方法”1、很好的工具软件,可以解...
透视辅助!hhpoker可以破... 透视辅助!hhpoker可以破解,德州hhpoker脚本“解密开挂透视挂辅助攻略”1、下载好hhpo...
透视挂!wepoker辅助器可... 透视挂!wepoker辅助器可以弄,德州局透视脚本下载安装最新版本“详情开挂透视挂辅助软件”1、点击...
透视规律!黑侠破解wepoke... 透视规律!黑侠破解wepoker,wepoker可以透视码“详情开挂透视挂辅助神器”亲,关键说明,w...
透视了解!wejoker辅助软... 透视了解!wejoker辅助软件,pokemmo内置修改器“专业开挂透视挂辅助技巧”1、任何poke...
透视好牌!werplan透视挂... 透视好牌!werplan透视挂,hhpoker辅助“普及开挂透视挂辅助软件”1、游戏颠覆性的策略玩法...