2017-05-25 67 views
0

我知道在stackoverflow上有很多關於熔合位置管理器的問題。但沒有人解決我的問題。所以: 我做了簡單的類使用官方doc使用熔合位置提供程序獲取位置

並注意到奇怪的行爲。我的onConnected方法從不被調用。我也檢查了onConnectionFailed,它是一樣的。 問題是什麼?

我的類:

public class MapActivity extends FragmentActivity implements OnMapReadyCallback, LocationListener, GoogleMap.OnMarkerClickListener 
, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{ 

    private GoogleMap mMap; 
    private double naviLat, naviLong; 
    private double latitude, longitude; 
    private GoogleApiClient mGoogleApiClient; 

    @BindView(R.id.adView) 
    AdView adView; 
    @BindView(R.id.butnavi) 
    ImageButton butnavi; 
    private GPStracker gpStracker; 
    private Location location; 
    private RealmResults<RowModel> rowModels; 
    private String shopName; 
    private Bundle bundle; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_map2); 
     ButterKnife.bind(this); 

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



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


     mapFragment.getMapAsync(this); 

     AdRequest adRequest = new AdRequest.Builder().build(); 
     adView.loadAd(adRequest); 
    } 



    @Override 
    public void onConnected(Bundle connectionHint) { 
     if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      Toast.makeText(this, "Uprawnienia nie przyznane", Toast.LENGTH_SHORT).show(); 
     } 
     Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
       mGoogleApiClient); 
     if (mLastLocation != null) { 
      Log.d("ddsa", String.valueOf(mLastLocation.getLatitude()) + " " + 
      String.valueOf(mLastLocation.getLongitude())); 
     } 
    } 



    @Override 
    public void onConnectionSuspended(int i) { 

    } 

    @Override 
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 
     Log.d("aaa", "Failed"); 
    } 
} 

搖籃:

apply plugin: 'com.android.application' 
apply plugin: 'realm-android' 



android { 
    compileSdkVersion 25 
    buildToolsVersion "25.0.2" 
    defaultConfig { 
     applicationId 'com.ddd.kkk' 
     minSdkVersion 15 
     targetSdkVersion 25 
     versionCode 8 
     versionName '4.0' 
     testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
     multiDexEnabled false 

    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
    productFlavors { 
    } 
} 

dependencies { 
    compile fileTree(include: ['*.jar'], dir: 'libs') 
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 
     exclude group: 'com.android.support', module: 'support-annotations' 
    }) 

    compile 'com.google.android.gms:play-services-identity:10.2.6' 
    compile 'com.google.android.gms:play-services-maps:10.2.6' 
    compile 'com.google.android.gms:play-services-ads:10.2.6' 
    compile 'com.google.android.gms:play-services-location:10.2.6' 

    compile 'com.android.support:appcompat-v7:25.3.1' 
    compile 'com.android.support:design:25.3.1' 
    compile 'com.android.support:cardview-v7:25.3.1' 
    compile 'com.github.bumptech.glide:glide:3.8.0' 
    compile 'com.android.support:recyclerview-v7:25.3.1' 
    compile 'com.jakewharton:butterknife:8.5.1' 
    compile 'com.squareup.retrofit2:retrofit:2.3.0' 
    compile 'com.squareup.retrofit2:converter-gson:2.3.0' 
    testCompile 'junit:junit:4.12' 
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1' 
} 
+0

是否安裝了播放服務並且是最新的在設備上? – Ryan

+0

onConnectionFailed叫? – xbadal

+0

你有運行時的位置權限? – xbadal

回答

0

我回答自己,因爲我失去了這一切的一天,也許有人會用它。如果您在AVD上測試您的應用程序,請記得每次清理應用程序緩存 - 運行幾次相同的應用程序會導致並非代碼中所做的所有更改都更新爲AVD ...

相關問題