簡體   English   中英

使用融合位置提供程序獲取位置

[英]Get Locatioan using Fused Location Provider

我知道在stackoverflow上有很多關於融合位置管理器的問題。 但是沒人能解決我的問題。 所以:我使用官方文檔進行了簡單的上課

並注意到奇怪的動作。 我的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'
}

我回答自己是因為我整天都丟失了它,也許有人會使用它。 如果您在AVD上測試應用程序,請記住每次都要清理應用程序緩存-多次運行同一應用程序會導致並非代碼中所做的所有更改都更新為AVD ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM