Guest User

Untitled

a guest
Feb 23rd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. 這拉位置的~就一行
  2. mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
  3. 只要mLastLocation不是null接下來都沒問題
  4. 喔還有再跑這行前都會確認mGoogleApiClient
  5. if (mGoogleApiClient != null) {
  6. if (mGoogleApiClient.isConnected())
  7. 看下面初始化的地方
  8.  
  9. 初始化
  10. private void initGoogleApi() {
  11. status = (LocationManager) (this.getSystemService(Context.LOCATION_SERVICE));
  12. if (mGoogleApiClient == null) {
  13. mGoogleApiClient = new GoogleApiClient.Builder(this)
  14. .addConnectionCallbacks(this)
  15. .addOnConnectionFailedListener(this)
  16. .addApi(LocationServices.API)
  17. .build();
  18. mGoogleApiClient.connect();
  19. }
  20. if(!status.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
  21. LogHelper.printDebugLog("No detect gps");
  22. String type = SharedpreferencesEditor.getString(SharedpreferencesEditor.Action.WEATHER_DATA_TYPE.key);
  23. if(type.equals(""))
  24. DialogUtils.makeAlertDialog(this, getString(R.string.dialog_title_gps_not_open_gps),
  25. getString(R.string.dialog_message_gps_not_open_gps) + "臺北市中正區", getString(R.string.dialog_btn_confirm));
  26. else
  27. DialogUtils.makeAlertDialog(this, getString(R.string.dialog_title_gps_not_open_postal),
  28. getString(R.string.dialog_message_gps_not_open_postal), getString(R.string.dialog_btn_confirm));
  29. }
  30.  
  31. if (status.isProviderEnabled(LocationManager.GPS_PROVIDER) && status.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
  32. //如果GPS或網路定位開啟,呼叫locationServiceInitial()更新位置
  33. LogHelper.printDebugLog("No detect gpc");
  34. if (mGoogleApiClient != null) {
  35. if (mGoogleApiClient.isConnected())
  36. getMyLocation();
  37. else
  38. mGoogleApiClient.connect();
  39. } else {
  40. Toast.makeText(HomeActivity.this, "請開啟定位", Toast.LENGTH_LONG).show();
  41. startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)); //開啟設定頁面
  42. }
  43. }
  44. }
  45.  
  46.  
  47. callback:
  48. @Override
  49. public void onConnected(@Nullable Bundle bundle) {
  50. getMyLocation();
  51. }
Add Comment
Please, Sign In to add comment