android– 我无法在谷歌地图中检索我当前的经度和纬度

android– 我无法在谷歌地图中检索我当前的经度和纬度,第1张

概述我使用以下代码来获取我当前位置的lat和lang,但它在CameraPosition中给我null值.任何想法为什么?privatevoidmoveMapToMyLocation(){LocationManagerlocMan=(LocationManager)MapsActivity.this.getSystemService(Context.LOCATION_SERVICE);Criteriacrit

我使用以下代码来获取我当前位置的lat和lang,但它在Cameraposition中给我null值.任何想法为什么?

   private voID moveMapToMyLocation() {    LocationManager locMan = (LocationManager) MapsActivity.this.getSystemService(Context.LOCATION_SERVICE);    Criteria crit = new Criteria();    Location loc = locMan.getLastKNownLocation(LocationManager.GPS_PROVIDER);    Cameraposition camPos = new Cameraposition.Builder()            .target(new LatLng(loc.getLatitude(), loc.getLongitude()))            .zoom(12.8f)            .build();    CameraUpdate camUpdate = CameraUpdateFactory.newCameraposition(camPos);   mMap.moveCamera(camUpdate);}

解决方法:

这是帮助类,它管理与GPS相关的 *** 作.

 public class GPSTracker  extends Service implements LocationListener {        private final Context mContext;        // flag for GPS status        boolean isGPSEnabled = false;        // flag for network status        boolean isNetworkEnabled = false;        // flag for GPS status        boolean canGetLocation = false;        Location location; // location        double latitude; // latitude        double longitude; // longitude        // The minimum distance to change Updates in meters        private static final long MIN_disTANCE_CHANGE_FOR_UPDATES = 1; // 10 meters        // The minimum time between updates in milliseconds        private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute        // Declaring a Location Manager        protected LocationManager locationManager;        public GPSTracker(Context context) {            this.mContext = context;            getLocation();        }        public Location getLocation() {            try {                locationManager = (LocationManager) mContext                        .getSystemService(LOCATION_SERVICE);                // getting GPS status                isGPSEnabled = locationManager                        .isProvIDerEnabled(LocationManager.GPS_PROVIDER);                // getting network status                isNetworkEnabled = locationManager                        .isProvIDerEnabled(LocationManager.NETWORK_PROVIDER);                if (!isGPSEnabled && !isNetworkEnabled) {                    // no network provIDer is enabled                } else {                    this.canGetLocation = true;                    // First get location from Network ProvIDer                    if (isNetworkEnabled) {                        try                        {                            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_disTANCE_CHANGE_FOR_UPDATES, this);                        }                        catch (SecurityException e)                        {                        }                        Log.d("Network", "Network");                        if (locationManager != null) {                            try                            {                                location = locationManager                                        .getLastKNownLocation(LocationManager.NETWORK_PROVIDER);                            }                            catch (SecurityException e)                            {                            }                            if (location != null) {                                latitude = location.getLatitude();                                longitude = location.getLongitude();                            }                        }                    }                    // if GPS Enabled get lat/long using GPS Services                    if (isGPSEnabled) {                        if (location == null) {                            try                            {                                locationManager.requestLocationUpdates(                                        LocationManager.GPS_PROVIDER,                                        MIN_TIME_BW_UPDATES,                                        MIN_disTANCE_CHANGE_FOR_UPDATES, this);                                Log.d("GPS Enabled", "GPS Enabled");                                if (locationManager != null) {                                    location = locationManager                                            .getLastKNownLocation(LocationManager.GPS_PROVIDER);                                    if (location != null) {                                        latitude = location.getLatitude();                                        longitude = location.getLongitude();                                    }                                }                            }                            catch (SecurityException s)                            {                            }                        }                    }                }            } catch (Exception e) {                e.printstacktrace();            }            return location;        }        /**         * Stop using GPS Listener         * Calling this function will stop using GPS in your app         * */        public voID stopUsingGPS(){            if(locationManager != null){                try                {                    locationManager.removeUpdates(GPSTracker.this);                }                catch (SecurityException e)                {                }            }        }        /**         * Function to get latitude         * */        public double getLatitude(){            if(location != null){                latitude = location.getLatitude();            }            // return latitude            return latitude;        }        /**         * Function to get longitude         * */        public double getLongitude(){            if(location != null){                longitude = location.getLongitude();            }            // return longitude            return longitude;        }        /**         * Function to check GPS/wifi enabled         * @return boolean         * */        public boolean canGetLocation() {            return this.canGetLocation;        }        /**         * Function to show settings alert dialog         * On pressing Settings button will lauch Settings Options         * */        public voID showSettingsAlert(){            AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);            // Setting Dialog Title            alertDialog.setTitle("GPS Settings");            // Setting Dialog Message            alertDialog.setMessage("GPS is not active. Do you want to open?");            // On pressing Settings button            alertDialog.setPositivebutton("Settings", new DialogInterface.OnClickListener() {                public voID onClick(DialogInterface dialog,int which) {                    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);                    mContext.startActivity(intent);                }            });            // on pressing cancel button            alertDialog.setNegativebutton("Cancel", new DialogInterface.OnClickListener() {                public voID onClick(DialogInterface dialog, int which) {                    dialog.cancel();                }            });            // Showing Alert Message            alertDialog.show();        }        @OverrIDe        public voID onLocationChanged(Location location) {        }        @OverrIDe        public voID onProvIDerDisabled(String provIDer) {        }        @OverrIDe        public voID onProvIDerEnabled(String provIDer) {        }        @OverrIDe        public voID onStatusChanged(String provIDer, int status, Bundle extras) {        }        @OverrIDe        public IBinder onBind(Intent arg0) {            return null;        }    }

成功创建GPSTracker课程后,您需要在想要获取纬度和经度的活动中调用它.在onCreate方法中使用以下代码行.

    GPSTracker gps;    double latitude;    double longitude;    gps = new GPSTracker(getActivity());    // check if GPS enabled    if(gps.canGetLocation())    {        latitude = gps.getLatitude();        longitude = gps.getLongitude();    }    else    {        gps.showSettingsAlert();    }

我们走到了尽头.请确保提供所有权限

<uses-permission androID:name="androID.permission.ACCESS_COARSE_LOCATION"/><uses-permission androID:name="androID.permission.ACCESS_FINE_LOCATION"/>
总结

以上是内存溢出为你收集整理的android – 我无法在谷歌地图中检索我当前的经度和纬度全部内容,希望文章能够帮你解决android – 我无法在谷歌地图中检索我当前的经度和纬度所遇到的程序开发问题。

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

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

原文地址: http://outofmemory.cn/web/1099195.html

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

发表评论

登录后才能评论

评论列表(0条)

保存