0

내 애플리케이션 용 Google지도 및 Google 장소를 구현했습니다. 나는 처음으로 응용 프로그램을 설치할 때 Google지도가 제대로 초기화되지 않는다는 것을 깨달았습니다. 즉, 사용 권한이 대화 상자의 권한 팝업을 통해 Google지도 권한을 허용 할 때 Google지도 창 기본값이 내 현재 위치와 장소를 표시하지 않고 원래지도로 설정됩니다. 자동 완성 필드가 작동하지 않습니다 (Google지도가 초기화되지 않았다는 의미입니다). 그러나, 내가 두 번째와 세 번째로 앱을 실행할 때 완벽하게 작동합니다.Google지도 V2가 팝업창에 권한을 부여한 후 초기화되지 않습니다.

public class MapsActivity extends AppCompatActivity implements 
     GoogleApiClient.ConnectionCallbacks, 
     GoogleApiClient.OnConnectionFailedListener, 
     View.OnClickListener, OnMapReadyCallback, 
     LocationListener { 
    protected GoogleApiClient mGoogleApiClient; 
    private boolean executeOnStart = false; 
    LocationRequest mLocationRequest; 
    private static final LatLngBounds BOUNDS_INDIA = new LatLngBounds(
      new LatLng(-0, 0), new LatLng(0, 0)); 
    public GoogleMap mMap; 
    Location mLastLocation; 
    Marker mCurrLocationMarker; 
    private String mapAddress = null; 
    private ImageView imageView; 
    private Button checkAvailability; 
    private StringBuilder sb = null; 
    private EditText mAutocompleteView; 
    private Geocoder geocoder; 
    private MarkerOptions markerOptions; 
    private LatLng latLng; 
    private Marker marker; 
    private RecyclerView mRecyclerView; 
    private LinearLayoutManager mLinearLayoutManager; 
    private PlacesAutoCompleteAdapter mAutoCompleteAdapter; 
    PlacesAutoCompleteAdapter.PlaceAutocomplete item; 
    String featureName, locality, countryName; 
    String placeId; 
    ImageView delete; 
    SupportMapFragment mapFragment; 
    private boolean allowEditTextChangeListener = true; 
    private LatLng pos; 
    static boolean invalidLocation = false; 
    private LinearLayout linearLayout; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     buildGoogleApiClient(); 
     setContentView(R.layout.activity_search); 
     mAutocompleteView = (EditText) findViewById(R.id.autocomplete_places); 

     mapFragment = (SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(MapsActivity.this); 

     linearLayout = (LinearLayout) findViewById(R.id.linear_layout); 

     Log.d("allowEditTextChange1",allowEditTextChangeListener + ""); 
     checkAvailability = (Button) findViewById(R.id.check_availability); 
     checkAvailability.setTypeface(Display.displayTypeface(getApplicationContext(), 1)); 


     geocoder = new Geocoder(this, Locale.getDefault()); 
     markerOptions = new MarkerOptions(); 

     buildGoogleApiClient(); 
     delete = (ImageView) findViewById(R.id.cross); 

     mAutoCompleteAdapter = new PlacesAutoCompleteAdapter(this, R.layout.searchview_adapter, 
       mGoogleApiClient, BOUNDS_INDIA, null); 

     mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView); 
     mLinearLayoutManager = new LinearLayoutManager(this); 
     mRecyclerView.setLayoutManager(mLinearLayoutManager); 
     mRecyclerView.setAdapter(mAutoCompleteAdapter); 
     // mRecyclerView.setLayoutFrozen(true); 
     delete.setOnClickListener(this); 

     if (allowEditTextChangeListener) { 
      Log.d("allowEditTextChangz1",allowEditTextChangeListener + ""); 
      mAutocompleteView.addTextChangedListener(new TextWatcher() { 
       public void onTextChanged(CharSequence s, int start, int before, 
              int count) { 
        Log.d("allowEditTextChangz1","jo"); 
        if (!s.toString().equals("") && mGoogleApiClient.isConnected()) { 
         mAutoCompleteAdapter.getFilter().filter(s.toString()); 
        } else if (!mGoogleApiClient.isConnected()) { 
        // Toast.makeText(getApplicationContext(), Constants.API_NOT_CONNECTED, Toast.LENGTH_SHORT).show(); 
         Log.e(Constants.PlacesTag, Constants.API_NOT_CONNECTED); 
        } 

       } 

       public void beforeTextChanged(CharSequence s, int start, int count, int after) {} 
       public void afterTextChanged(Editable s) {} 
      }); 
     } 
     mRecyclerView.addOnItemTouchListener(
       new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() { 
        @Override 
        public void onItemClick(View view, int position) { 
         item = mAutoCompleteAdapter.getItem(position); 
         placeId = String.valueOf(item.placeId);PendingResult<PlaceBuffer> placeResult = Places.GeoDataApi 
           .getPlaceById(mGoogleApiClient, placeId); 
         placeResult.setResultCallback(new ResultCallback<PlaceBuffer>() { 
          @Override 
          public void onResult(PlaceBuffer places) { 
           if (places.getCount() == 1) { 
            mAutoCompleteAdapter.clear(); 
            mAutocompleteView.setText(""); 
            String country = ""; 

            CameraPosition cameraPosition = new CameraPosition.Builder() 
              .target(places.get(0).getLatLng())  // Sets the center of the map to Mountain View 
              .zoom(12)     // Sets the zoom 
              .bearing(90)    // Sets the orientation of the camera to east 
              .tilt(30)     // Sets the tilt of the camera to 30 degrees 
              .build();     // Creates a CameraPosition from the builder 

            marker.remove(); // remove any marker from maponclick or maponlonclick 
            marker = mMap.addMarker(new MarkerOptions().position(places.get(0).getLatLng()) 
              .title("My Location")); 
            mMap.setTrafficEnabled(true); 
            mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 
          } 
         }); 
         Log.i("TAG", "Clicked: " + item.description); 
         Log.i("TAG", "Called getPlaceById to get Place details for " + item.placeId); 
        } 
       }) 
     ); 

     imageView = (ImageView) findViewById(R.id.maps_close); 
     imageView.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       Log.d("close", "close"); 
       if (mAutoCompleteAdapter.getItemCount() != 0){ 
        mAutoCompleteAdapter.clear(); 
       } 
       mAutocompleteView.setText(""); 
      } 
     }); 

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
      checkLocationPermission(); 
     } 
    } 

    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     mMap = googleMap; 
     mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
     if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
      if (ContextCompat.checkSelfPermission(this, 
        Manifest.permission.ACCESS_FINE_LOCATION) 
        == PackageManager.PERMISSION_GRANTED) { 
       buildGoogleApiClient(); 
       mMap.setMyLocationEnabled(true); 

      } 
     } else { 
      buildGoogleApiClient(); 
      mMap.setMyLocationEnabled(true); 

     } 

    } 

    @Override 
    public void onLocationChanged(Location location) { 

     mLastLocation = location; 

     if (mCurrLocationMarker != null) { 
      mCurrLocationMarker.remove(); 
     } 

     final Double lat = location.getLatitude(); 
     final Double lng = location.getLongitude(); 
     Log.d("LATLANGz", lat + "|" + lng); 
     latLng = new LatLng(lat, lng); 
     markerOptions = new MarkerOptions(); 
     markerOptions.position(latLng); 
     markerOptions.title("Current Positionn"); 
     markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)); 
     marker = mMap.addMarker(markerOptions); 

     mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
     mMap.animateCamera(CameraUpdateFactory.zoomTo(11)); 

     if (mGoogleApiClient != null) { 
      LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 
     } 
    } 

    protected synchronized void buildGoogleApiClient() { 
     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(LocationServices.API) 
       .addApi(Places.GEO_DATA_API) 
       .build(); 
     mGoogleApiClient.connect(); 
    } 

    @Override 
    public void onConnectionSuspended(int i) {} 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) {} 

    @Override 
    public void onClick(View v) { 
     if(v==delete){ 
      mAutocompleteView.setText(""); 
     } 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
     if (!mGoogleApiClient.isConnected() && !mGoogleApiClient.isConnecting()){ 
      Log.v("Google API","Connecting"); 
      mGoogleApiClient.connect(); 
     } 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 
     if(mGoogleApiClient.isConnected()){ 
      Log.v("Google API","Dis-Connecting"); 
      mGoogleApiClient.disconnect(); 
     } 
    } 

    @Override 
    public void onBackPressed() { 
     super.onBackPressed(); 
    } 

    public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99; 
    public boolean checkLocationPermission(){ 
     if (ContextCompat.checkSelfPermission(this, 
       Manifest.permission.ACCESS_FINE_LOCATION) 
       != PackageManager.PERMISSION_GRANTED) { 
      if (ActivityCompat.shouldShowRequestPermissionRationale(this, 
        Manifest.permission.ACCESS_FINE_LOCATION)) { 
       ActivityCompat.requestPermissions(this, 
         new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
         MY_PERMISSIONS_REQUEST_LOCATION); 

      } else { 
       ActivityCompat.requestPermissions(this, 
         new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
         MY_PERMISSIONS_REQUEST_LOCATION); 
      } 
      return false; 
     } else { 
      return true; 
     } 
    } 

    @Override 
    public void onConnected(Bundle bundle) { 
     mLocationRequest = new LocationRequest(); 
     mLocationRequest.setInterval(1000); 
     mLocationRequest.setFastestInterval(1000); 
     mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); 

     if (mGoogleApiClient.isConnected()){ 
      if (ContextCompat.checkSelfPermission(this, 
        Manifest.permission.ACCESS_FINE_LOCATION) 
        == PackageManager.PERMISSION_GRANTED) { 
       LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
      } 
     } 
    } 
    @Override 
    public void onRequestPermissionsResult(int requestCode, 
              String permissions[], int[] grantResults) { 
     switch (requestCode) { 
      case MY_PERMISSIONS_REQUEST_LOCATION: { 
       if (grantResults.length > 0 
         && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
        if (ContextCompat.checkSelfPermission(this, 
          Manifest.permission.ACCESS_FINE_LOCATION) 
          == PackageManager.PERMISSION_GRANTED) { 

         if (mGoogleApiClient == null) { 
          buildGoogleApiClient(); 
         } 
        } 

       } else { 
       } 
       return; 
      } 
     } 
    } 

    @Override 
    public void onStart(){ 
     super.onStart(); 
     if (isNetworkAvailable() == true){ 
      if (isLocationEnabled(getApplicationContext())){ 
       if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
        checkLocationPermission(); 
       } 
       // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
       SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
         .findFragmentById(R.id.map); 
       mapFragment.getMapAsync(this); 
      } else{ 
       showLocationDialog(); 
      } 
     } else { 
      showInternetDialog(); 
     } 

    } 

    public void showInternetDialog(){ 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setMessage("Please turn on mobile network or Wi-Fi in Settings.") 
       .setTitle("WIFI Disabled") 
       .setCancelable(false) 
       .setPositiveButton("Settings", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int id) { 
           Intent i = new Intent(Settings.ACTION_WIRELESS_SETTINGS); 
           startActivity(i); 
          } 
         } 
       ) 
       .setNegativeButton("Cancel", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int id) { 
           MapsActivity.this.finish(); 
          } 
         } 
       ); 
     AlertDialog alert = builder.create(); 
     alert.show(); 
    } 

    // check internet connectivity 
    public boolean isNetworkAvailable() { 
     ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo activeNetworkInfo = connectivityManager 
       .getActiveNetworkInfo(); 
     return activeNetworkInfo != null; 
    } 

    public static boolean isLocationEnabled(Context context) { 
     int locationMode = 0; 
     String locationProviders; 

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){ 
      try { 
       locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE); 

      } catch (Settings.SettingNotFoundException e) { 
       e.printStackTrace(); 
       return false; 
      } 

      return locationMode != Settings.Secure.LOCATION_MODE_OFF; 

     }else{ 
      locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); 
      return !TextUtils.isEmpty(locationProviders); 
     } 
    } 

    public void showLocationDialog(){ 
     executeOnStart = true; 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setMessage(R.string.gps_not_found_message) 
       .setTitle(R.string.gps_not_found_title) 
       .setCancelable(false) 
       .setPositiveButton("Settings", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int id) { 
           Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
           startActivity(i); 
          } 
         } 
       ) 
       .setNegativeButton("Cancel", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dg, int id) { 
           MapsActivity.this.finish(); 

          } 
         } 
       ); 
     AlertDialog alert = builder.create(); 
     alert.show(); 
    } 
} 

답변

0

:이 때문에에 onMapReady() 당신의 할 일이다

권한이 이미 부여 된 경우
buildGoogleApiClient(); 
mMap.setMyLocationEnabled(true); 

, 만 : onRequestPermissionsResult()에서

if (mGoogleApiClient == null) { 
    buildGoogleApiClient(); 
} 

. 그냥 그렇게 onRequestPermissionsResult()mMap.setMyLocationEnabled(true);을 추가

if (mGoogleApiClient == null) { 
    buildGoogleApiClient(); 
    mMap.setMyLocationEnabled(true); 
} 

를 자동 완성의 경우 : protected void onCreate(Bundle savedInstanceState) 당신이 buildGoogleApiClient();를 호출하고 (첫 번째 앱 시작시) 허가를 얻기 전에 mAutoCompleteAdapter을 만듭니다

... 
buildGoogleApiClient(); 
delete = (ImageView) findViewById(R.id.cross); 

mAutoCompleteAdapter = new PlacesAutoCompleteAdapter(this, R.layout.searchview_adapter, mGoogleApiClient, BOUNDS_INDIA, null); 
.... 

정도, 당신은 또한 mAutoCompleteAdapter = new PlacesAutoCompleteAdapter(...)를 호출해야합니다 onRequestPermissionsResult().

+0

매우 효과적이었습니다. –

+0

이렇게 대답 할 수 있습니다 :) (이렇게하려면 질문 비율 아래의 체크 표시를 클릭해야합니다. 투표 화살표 아래에 개미가 녹색이되어야합니다.) –

+0

@ G.Joe Thanx! 행운을 빕니다! –

0

은 어쩌면 당신은 onRequestPermissionsResult 방법 중에 제대로로드되지 않습니다

다음은 내 코드입니다. 스플래시 페이지 활동 중에 권한 요청을 요청하는 몇 가지 트릭을 시도하십시오. 현재 위치

+0

이것은 좋은 생각이므로 스플래시 화면 작업에 대한 모든 응용 프로그램 권한을 요청해야합니다. –

+0

네, 처음으로 앱 스플래시 화면이 실행 중일 때 –

관련 문제