Android 使用融合位置提供程序获取位置
我知道stackoverflow上有很多关于fused location manager的问题。但没人能解决我的问题。因此: 我用官方软件制作了一个简单的类 注意奇怪的动作。从未调用我的onConnected方法。我还检查了onConnectionFailed,结果是一样的。 有什么问题 我的班级:Android 使用融合位置提供程序获取位置,android,google-play-services,android-location,Android,Google Play Services,Android Location,我知道stackoverflow上有很多关于fused location manager的问题。但没人能解决我的问题。因此: 我用官方软件制作了一个简单的类 注意奇怪的动作。从未调用我的onConnected方法。我还检查了onConnectionFailed,结果是一样的。 有什么问题 我的班级: public class MapActivity extends FragmentActivity implements OnMapReadyCallback, LocationListener,
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");
}
}
我回答我自己,因为我一整天都把它弄丢了,也许有人会用它。如果您在AVD上测试应用程序,请记住每次都要清理应用程序缓存-运行多次相同的应用程序会导致并非所有对AVD进行的代码更新更改…设备上是否安装了播放服务并且是最新的?调用了onConnectionFailed?您是否具有运行时位置权限???@Ryan我测试了它所以我认为google play服务应该达到date@xabadalonConnectionFailed我认为应该在onConnection为false时自动调用。但它也不起作用。
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'
}