本文介绍了我无法在Google地图中检索当前的经度和纬度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我使用下面的代码来获取当前位置的lat和lang,但它给了我CameraPosition中的空值..任何想法为什么? private void moveMapToMyLocation(){ LocationManager locMan =(LocationManager)MapsActivity.this.getSystemService(Context.LOCATION_SERVICE); Criteria crit = new Criteria(); 位置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); 解决方案 public class GPSTracker extends Service implements LocationListener { private final Context mContext; // GPS状态标志 boolean isGPSEnabled = false; //网络状态标志 boolean isNetworkEnabled = false; // GPS状态标志 boolean canGetLocation = false; 位置位置; //位置双纬度; //纬度双经度; // longitude //改变的最小距离以米为单位的更新 private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 1; // 10米 //以毫秒为单位的最小更新时间间隔 private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1分钟 //声明位置管理器受保护的LocationManager locationManager; 公共GPSTracker(上下文环境){ this.mContext = context; getLocation(); public Location getLocation(){ try { locationManager =(LocationManager)mContext .getSystemService(LOCATION_SERVICE); //获取GPS状态 isGPSEnabled = locationManager .isProviderEnabled(LocationManager.GPS_PROVIDER); //获取网络状态 isNetworkEnabled = locationManager .isProviderEnabled(LocationManager.NETWORK_PROVIDER); if(!isGPSEnabled&&!isNetworkEnabled){ //没有启用网络提供程序} else { this.canGetLocation = true; //首先从网络供应商处获得位置 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); $ b $ catch(SecurityException e) { } if(location!= null){ latitude = location.getLatitude(); longitude = location.getLongitude(); $ b //如果GPS启用使用GPS服务获得经纬度 if(isGPSEnabled){ if(location == null ){ 尝试 { locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES,this); Log.d(已启用GPS,已启用GPS); if(locationManager!= null){ location = locationManager .getLastKnownLocation(LocationManager.GPS_PROVIDER); if(location!= null){ latitude = location.getLatitude(); longitude = location.getLongitude(); $ b catch(SecurityException s) { } } $ b $ (例外e){ e.printStackTrace(); $ b} } 返回地点; $ b $ ** *停止使用GPS监听器 *调用此函数将停止在您的应用中使用GPS * * / public void stopUsingGPS(){ if(locationManager!= null){ try { locationManager.removeUpdates(GPSTracker.this); catch(SecurityException e) { } } } / ** *获取纬度的函数 * * / public double getLatitude(){ if(location!= null){ latitude = location.getLatitude (); } //返回纬度返回纬度; $ b / ** *获得经度的函数 * * / public double getLongitude(){ if(location! = null){ longitude = location.getLongitude(); } //返回经度返回经度; } $ b $ ** *检查GPS / wifi的功能 * @return boolean * * / public boolean canGetLocation (){ return this.canGetLocation; } / ** *显示设置提示对话框的功能 *按下设置按钮后,将显示设置选项 * * / public void showSettingsAlert(){ AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext); //设置对话框标题 alertDialog.setTitle(GPS Settings); //设置对话框消息 alertDialog.setMessage(GPS未激活,你想打开吗?); //按设置按钮 alertDialog.setPositiveButton(Settings,新的DialogInterface.OnClickListener(){ public void onClick(DialogInterface对话框,int which){ Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); mContext.startActivity(intent); } }); //按下取消按钮 alertDialog.setNegativeButton(取消,新的DialogInterface.OnClickListener(){ public void onClick(DialogInterface对话框,int其中){ dialog.cancel(); } }); //显示提示信息 alertDialog.show(); } @Override public void onLocationChanged(Location location){} @Override public void onProviderDisabled(字符串提供者){} @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; 双纬度; 双经度; gps = new GPSTracker(getActivity()); //检查GPS是否启用 if(gps.canGetLocation()) { latitude = gps.getLatitude(); longitude = gps.getLongitude(); } else { gps.showSettingsAlert(); } 我们走到了尽头。请确保给出所有权限 <使用权限android:name =android.permission.ACCESS_COARSE_LOCATION/> ; < uses-permission android:name =android.permission.ACCESS_FINE_LOCATION/> I am using the following code to get the lat and lang of my current location but it gives me null value in CameraPosition.. any idea why ? 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);} 解决方案 Here is the helper class which manages the operations related to 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; } }After create the GPSTracker class successfully, you need to call it in the activity where you wanna take the latitude and longitude. Use following code lines in the onCreate method. 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(); }We came to the end. Please make sure that all permissions are given<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 这篇关于我无法在Google地图中检索当前的经度和纬度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-12 11:23