【问题标题】:Find places using Google Maps and GPS使用 Google 地图和 GPS 查找地点
【发布时间】:2014-12-29 06:39:13
【问题描述】:

demkey我正在尝试创建一个谷歌地图请求,以查找我所在位置附近最近的药店或医院,但我遇到了一些问题。

代码是:

public class MostrarFarmaciasHospitales extends Activity {

    private double latitude;
    private double longitude;
        @Override
        protected void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);


            LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            comprobarGPSActivado(locationManager);
            Criteria criteria = new Criteria();
            String provider = locationManager.getBestProvider(criteria, true);
            Location myLocation = locationManager.getLastKnownLocation(provider);
            latitude = myLocation.getLatitude();
            longitude = myLocation.getLongitude();



            farmaciasHospitalesCercanos();
        }


    private void comprobarGPSActivado(LocationManager locationManager) {
        if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("El GPS está desactivado. ¿Quiere activarlo? ")
                    .setCancelable(true)
                    .setPositiveButton("Sí.",
                            new DialogInterface.OnClickListener() {
                                public void onClick(final DialogInterface dialog, final int id) {
                                    dialog.cancel();
                                    startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));

                                }
                            })
                    .setNegativeButton("No", new DialogInterface.OnClickListener() {
                        public void onClick(final DialogInterface dialog, final int id) {
                            dialog.cancel();
                        }
                    });
            final AlertDialog alert = builder.create();
            alert.show();

        }
    }

    public void farmaciasHospitalesCercanos() {

        String[] farmaciasHopsitales = {"Farmacias cercanas a mí" , "Hospitales a mi alrededor"};
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Qué quiere buscar?")
                .setItems(farmaciasHopsitales, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // The 'which' argument contains the index position
                        // of the selected item
                        switch (which){
                            case 0: //Farmacias
                                //String urlPH = "https://maps.googleapis.com/maps/api/place/radarsearch/json?location="+latitude+","+longitude+"&radius=5000&types=farmacia&sensor=false&key=demkey";
                                String urlPH = ("https://www.google.es/maps/search/farmacia/@"+latitude+","+longitude);
                                Intent intentPH = new Intent(Intent.ACTION_VIEW, Uri.parse(urlPH));
                                intentPH.setClassName("com.google.android.apps.maps","com.google.android.maps.MapsActivity");
                                startActivity(intentPH);
                                MostrarFarmaciasHospitales.this.finish();
                                break;

                            case 1://Hospitales
                                String urlH = "https://maps.googleapis.com/maps/api/place/radarsearch/json?location="+latitude+","+longitude+"&radius=5000&types=hospital&sensor=false&key=demkey";
                                Intent intentH = new Intent(Intent.ACTION_VIEW, Uri.parse(urlH));
                                intentH.setClassName("com.google.android.apps.maps","com.google.android.maps.MapsActivity");
                                startActivity(intentH);
                                MostrarFarmaciasHospitales.this.finish();
                                break;
                        }

                    }
                });
        AlertDialog alert = builder.create();
        alert.show();
    }
}

如您所见,我尝试使用简单的谷歌地图请求以及雷达搜索。他们都没有按预期工作。

如果我使用正常的谷歌请求,搜索是正确的,但不是我所在的地方,只是在国内的一个随机地方。而且,如果我使用此选项,当设备中启用 GPS 时,应用程序会崩溃。如您所见,我有一个方法要求用户启用 GPS,但它永远不会出现,就像永远无法达到该方法一样。

如果我使用雷达搜索,它会将摄像头集中在我所在的好位置,但在搜索栏中没有输入任何文字。

我该如何解决?似乎雷达搜索是一种方式,但我不知道为什么会失败。非常感谢。

【问题讨论】:

    标签: android gps maps


    【解决方案1】:

    美好的一天。请参阅此链接,它们绝对会帮助您并帮助您入门。这两个链接都有可供下载的源代码,因此如果您愿意,您无需费力:)

    https://www.youtube.com/watch?v=awX5T-EwLPc

    第二

    https://www.youtube.com/watch?v=mnEjFjQPNHM

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-12
      • 2013-06-27
      • 1970-01-01
      • 2020-11-05
      • 1970-01-01
      • 2015-12-07
      相关资源
      最近更新 更多