-1

얻기 안녕하세요 아래 앱스에서 LatLong 점점 오전 :onMapReady() 메서드 호출 GoogleApiClient을 구축하기 전에 - LatLong 0.0

만든 GoogleApiClient 구축하는 나의 조각의 방법

private synchronized void buildGoogleApiClient() { 
    MyApp.getGoogleApiHelper().setConnectionListener(new GoogleApiHelper.ConnectionListener() { 
     @Override 
     public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 

     } 

     @Override 
     public void onConnectionSuspended(int i) { 

     } 

     @Override 
     public void onConnected(Bundle bundle) { 
      //this function will call whenever google api connected or already connected when setting listener 
      //You are connected do what ever you want 
      //Like i get last known location 
      try { 
       if (!Constant.checkPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION)) { 
        Constant.requestPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION, PLACE_PICKER_REQUEST); 
       } else { 
        if (Constant.isOnline(mContext)) { 
         mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
         mCurrentLatitude = mLastLocation.getLatitude(); 
         mCurrentLongitude = mLastLocation.getLongitude(); 
         locationUpdate(); 
        } else { 
         Constant.displayToast(mContext, getResources().getString(R.string.msg_internet)); 
        } 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

OnMapReady을 () 메소드는 다음과 같습니다. loadDefault() 메소드를 호출하여 현재 위치를로드합니다. 나는() 메소드 loadDefault 내부 선 아래에서 mCurrentLongitude 0.0 mCurrentLaltitude을 얻고,

private void loadDefault(GoogleMap googleMap) { 
    try { 
     if (Prefrences.checkPref(mContext, NEAR_ME_SEARCH)) 
      googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(Double.parseDouble(Prefrences.getPref(mContext, NEAR_ME_LAT)), Double.parseDouble(Prefrences.getPref(mContext, NEAR_ME_LNG))), 8)); 
     else 
      googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(mCurrentLatitude, mCurrentLongitude), 8f)); 
    } catch (MalformedParameterizedTypeException e) { 
     handleException(mContext, e); 
    } catch (Exception e) { 
     handleException(mContext, e); 
    } 
} 

그러나 :

public void onMapReady(final GoogleMap googleMap) { 
    loadDefault(googleMap); 
} 

내 loadDefault() 메소드는 다음과 같습니다.

googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(mCurrentLatitude, mCurrentLongitude), 8f)); 

하시기 바랍니다. 감사.

편집는 : onMapReady가 호출되고지도가로드 된 경우

private void initialization(View rootView, Bundle savedInstanceState) { 
    try { 
     mModelBarList = BarListModel.getBarListInstance(); 
     if (!Constant.checkPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION)) { 
      Constant.requestPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION, PLACE_PICKER_REQUEST); 
     } else { 
      if (Constant.isOnline(mContext)) { 
       mGoogleApiClient = MyApp.getGoogleApiHelper().getGoogleApiClient(); 
       buildGoogleApiClient(); 
      } else { 
       Constant.displayToast(mContext, getResources().getString(R.string.msg_internet)); 
      } 
     } 

     mMapView = (MapView) rootView.findViewById(R.id.map); 
     mMapView.onCreate(savedInstanceState); 
     mMapView.onResume(); 
     mMapView.getMapAsync(MapFragment.this); 
} 
+0

누가'buildGoogleApiClient()'메소드를 호출하고 있습니까? 내 생각에이 메소드는 전혀 호출되지 않았거나'onMapReady()'후에 호출되고있다. Android 용 Google지도를 사용할 때 방향 변경 또는 다시로드를 통해 위치를 유지해야하는 경우 내 위치를 인스턴스 상태로 저장합니다. –

+0

@TimBiegeleisen, 위의 조건에서지도를 더 빨리 초기화 할 수 있습니까? – Krantiz

+0

나는 당신이 무엇을하려고하는지 모르기 때문에 정확한 답변을 드릴 수 없습니다. 액티비티가로드 될 때 값을로드 한 다음 인스턴스 상태에 저장합니다. 적어도이 작업은 수행합니다. 예를 들어 환경 설정에서 가져 오거나 SQLite 데이터베이스에서 가져온 경우와 같이 솔루션이 다를 수 있습니다. –

답변

0

,이 두 작업은 비동기 적으로 실행하기 때문에 구글 API 클라이언트가 연결되어 있는지 확인하지 않습니다. 따라서 논리를 추가하여 먼저 완료되는 논리가 다른 논리가 끝날 때까지 대기 한 다음지도에 좌표를 표시해야합니다.

+0

나중에 물어볼 내용은 무엇입니까? –

+0

죄송하지만 과거 사본 솔루션을 제공 할 수 없습니다. 너무 우아한 해결책은지도가로드 될 때지도의 참조를 유지하고 google API 클라이언트에서 좌표를 가져 와서 mMap! = null인지 확인하고지도 작업을 계속 진행하면됩니다. 따라서 onMapReady가 호출 될 때 mCurrentLatitude, mCurrentLongitude! = 0인지 확인하고 true이면 계속 진행합니다. –