0

현지화에 큰 어려움이 있습니다 (GoogleApiClient). 내가 intorface GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener 구현하고 LocationListener 전혀 작동하지 않습니다. 그 인터페이스가 내 애플 리케이션에 등록되지 않은 것 같습니다. 아래는 제 코드입니다. 방법을보십시오 : onLocationChanged, onConnectionSuspended, onConnected, onConnectionFailed. 모든 예에서 일부 텍스트를 인쇄하려면 LOG으로 추가하고 그 중 하나의 예는 logs입니다.Android의 GoogleApiClient를 애플리케이션에 등록 할 수 없습니다.

mLocationRequest = LocationRequest.create() 
       .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) 
       .setInterval(10 * 1000)  // 10 seconds, in milliseconds 
       .setFastestInterval(1 * 1000); 

지도는 10 초마다 새로 고침한다 - 그래서 그것은 또한 Log.d("location test", "tes2"); 10 초마다 인쇄해야 의미 -하지만 그렇지 않습니다 :

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener { 

    private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000; 

    private GoogleMap mMap; 
    private Location location; 

    ///////////////////////////////////////////// 
    private GoogleApiClient mGoogleApiClient; 
    public static final String TAG = MapsActivity.class.getSimpleName(); 
    private LocationRequest mLocationRequest; 
    private LatLng latLng; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_maps); 


     // Create the LocationRequest object 
     mLocationRequest = LocationRequest.create() 
       .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) 
       .setInterval(10 * 1000)  // 10 seconds, in milliseconds 
       .setFastestInterval(1 * 1000); // 1 second, in milliseconds 


     dataBaseHelper = new DataBaseHelper(getApplicationContext()); 

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


     /* some code here */ 

     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addApi(LocationServices.API) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .build(); 
    } 


    @Override 
    public void onMapReady(final GoogleMap googleMap) { 
     mMap = googleMap; 


    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     setUpMapIfNeeded(); 
     mGoogleApiClient.connect(); 

    } 

    private void setUpMapIfNeeded() { 

     if (mMap == null) { 
      ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)) 
        .getMapAsync(this); 
      if (mMap != null) { 
       //setUpMap(); 
      } 
     } 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     if (mGoogleApiClient.isConnected()) { 
      LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 
      mGoogleApiClient.disconnect(); 
     } 
    } 


    private void handleNewLocation(Location location) { 
     Log.d(TAG, location.toString()); 

     double currentLatitude = location.getLatitude(); 
     double currentLongitude = location.getLongitude(); 
     final LatLng latLng = new LatLng(currentLatitude, currentLongitude); 
     setLatLng(latLng); 

     MarkerOptions options = new MarkerOptions() 
       .position(latLng) 
       .title("I am here!"); 
     mMap.addMarker(options); 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
     mMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null); 

     generateMap(getLatLng(), "3000"); 

     sItems.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 

      @Override 
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
       washLocations.clear(); 
       washLocations.size(); 
       String latitude = String.valueOf(latLng.latitude); 
       String longitude = String.valueOf(latLng.longitude); 

       generateMap(getLatLng(), null); 
      } 

      @Override 
      public void onNothingSelected(AdapterView<?> parent) { 
      } 
     }); 
    } 


    private void setUpMap() { 
     mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker")); 
    } 

    @Override 
    public void onConnected(@Nullable Bundle bundle) { 
     Log.d("location test", "tes1"); 
     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      return; 
     } 
     Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
     if (location == null) { 
      LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
     } else { 
      handleNewLocation(location); 
     } 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 
     Log.i("faild", "Location services suspended. Please reconnect."); 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 
     if (connectionResult.hasResolution()) { 
      try { 
       // Start an Activity that tries to resolve the error 
       connectionResult.startResolutionForResult(this, CONNECTION_FAILURE_RESOLUTION_REQUEST); 
      } catch (IntentSender.SendIntentException e) { 
       e.printStackTrace(); 
      } 
     } else { 
      Log.i("failds", "Location services connection failed with code " + connectionResult.getErrorCode()); 
     } 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     Log.d("location test", "tes2"); 
     handleNewLocation(location); 
    } 
} 

당신은 위 참조한다. 적용 플러그인 :

그리고 여기가 내 내가 모든 필요한 libriaries을 넣어 build.gradle입니다 'com.android.application'법 아래 같은 모든 CREAT googleApiClient 객체의

android { 
    compileSdkVersion 25 
    buildToolsVersion "25.0.2" 
    defaultConfig { 
     applicationId "com.example.micha.locationtest" 
     minSdkVersion 15 
     targetSdkVersion 25 
     versionCode 1 
     versionName "1.0" 
     testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
    useLibrary 'org.apache.http.legacy' 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 
     exclude group: 'com.android.support', module: 'support-annotations' 
    }) 
    compile 'com.android.support:appcompat-v7:25.2.0' 
    compile 'com.google.android.gms:play-services-maps:10.2.0' 
    testCompile 'junit:junit:4.12' 
    compile 'com.android.support:recyclerview-v7:25.2.0' 
    compile 'com.google.android.gms:play-services-location:10.2.0' 
} 

답변

0

먼저

private synchronized void connectToGoolgeAPIClient() { 
    if (mGoogleApiClient == null) { 
     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(LocationServices.API) 
       .build(); 
    } 
} 

그 다음 위치는 아래 코드와 같습니다.

if (mGoogleApiClient.isConnected()) { 
    mLocationRequest = new LocationRequest(); 
    mLocationRequest.setInterval(2000); 
    mLocationRequest.setFastestInterval(10); 
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 


if (mLastLocation != null) { 
double lon = mLastLocation.getLongitude(); 
double lat = mLastLocation.getLatitude(); 

} 
+0

작동하지 않습니다. 'Utilis'란 무엇입니까? 나는 그것을 전혀 가져올 수 없다 ... – bielas

+0

그 util을 무시한다. 단지 나의 편집 된 포스트를 본다. –

0

마지막으로이 방법을 변경하여 케이스를 해결했습니다.

 @Override 
    public void onConnected(@Nullable Bundle bundle) { 

     mLocationRequest = LocationRequest.create(); 
     mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
     mLocationRequest.setInterval(10000); 
     mLocationRequest.setFastestInterval(10000); 
     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      return; 
     } 
     LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
     Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
     handleNewLocation(location); 
    } 
관련 문제