获取用户当前位置android

获取用户当前位置android,第1张

概述嗨,我正在开发 Android应用程序,我希望用户当前位置足够准确.所以我有2个选项来实现这一个 检索当前位置. http://developer.android.com/training/location/retrieve-current.html 和 接收位置更新http://developer.android.com/training/location/receive-location-up 嗨,我正在开发 Android应用程序,我希望用户当前位置足够准确.所以我有2个选项来实现这一个

检索当前位置. http://developer.android.com/training/location/retrieve-current.html

接收位置更新http://developer.android.com/training/location/receive-location-updates.html

所以这是我的问题.我经历了检索当前位置,我意识到最后它给了我用户最后知名的位置.但我的要求是用户当前位置.

所以我走向接收位置更新.我虽然可以获得第一次更新,但我会停止更新.但这也是第一次它给了我最后的位置而不是新的位置.并在第一次阅读后启动gps以提供连续更新.但我只对第一次更新感兴趣.

所以我需要一个好的解决方案,它会给我用户准确的当前位置.使用gps或网络或wifi任何可用的.

如果我做错了什么,请更正.需要帮忙.谢谢.

解决方法 这就是我如何获取用户当前位置以用于我的Google地图.
if (ActivityCompat.checkSelfPermission(getActivity(),Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(getActivity(),Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {            mMap.setMyLocationEnabled(true);            Criteria criteria = new Criteria();            LocationManager locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);            String provIDer = locationManager.getBestProvIDer(criteria,false);            Location location = locationManager.getLastKNownLocation(provIDer);            double lat =  location.getLatitude();            double lng = location.getLongitude();            LatLng coordinate = new LatLng(lat,lng);            CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(coordinate,13);            mMap.animateCamera(yourLocation);        } else {            final AlertDialog alertDialogGPS = new AlertDialog.Builder(getActivity()).create();            alertDialogGPS.setTitle("Info");            alertDialogGPS.setMessage("Looks like you have not given GPS permissions. Please give GPS permissions and return back to the app.");            alertDialogGPS.setIcon(androID.R.drawable.ic_dialog_alert);            alertDialogGPS.setbutton("OK",new DialogInterface.OnClickListener() {                public voID onClick(DialogInterface dialog,int which) {                    Intent intentSettings = new Intent(Settings.ACTION_APPliCATION_SETTINGS);                    startActivity(intentSettings);                    alertDialogGPS.dismiss();                }            });            alertDialogGPS.show();        }
总结

以上是内存溢出为你收集整理的获取用户当前位置android全部内容,希望文章能够帮你解决获取用户当前位置android所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1141087.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存