java - 即使 map 显示没有任何错误,也不会调用 onMapReady

标签 java android google-maps google-maps-android-api-2

我正在构建一个跟踪 map ,其中包含两个标记,一个是当前位置,第二个是从数据库服务器获取其 LatLng,它在两周内运行良好,现在它只显示没有任何标记的 map ,当我尝试调试应用程序并向 onMapReady() 函数设置断点时,我发现它没有被调用

Layout.XML 文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".MapsActivity"
    android:orientation="vertical"
    android:background="@color/Defualt">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/gradient_bar"
        android:theme="@style/AppTheme.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:textColor="@android:color/background_light"
            app:popupTheme="@style/AppTheme.PopupOverlay">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/left_arrow_icon"
                android:onClick="goBack"/>

            <TextView
                android:id="@+id/toolbar_title"
                style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Track"
                android:textColor="@color/Defualt" />


        </androidx.appcompat.widget.Toolbar>

    </com.google.android.material.appbar.AppBarLayout>


<fragment
    android:id="@+id/map1"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>





</LinearLayout>


java代码:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, RoutingListener {

    @Nullable
    private GoogleMap mMap;
    Location mLastLocation;
    LocationRequest mLocationRequest;
    private List<Polyline> polylines;

    private FusedLocationProviderClient mFusedLocationClient;

    private LatLng pickupLocation, patientLocation;

    private Marker pickupMarker;

    private SupportMapFragment mapFragment;

protected void onCreate(Bundle savedInstanceState) {
        StatusBar.setStatusBarGradiant(MapsActivity.this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        // Obtain the SupportMapFragment and get notified when the map is ready to be used
        polylines = new ArrayList<>();

        mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

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


    }

public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        mFusedLocationClient.getLastLocation()
                .addOnSuccessListener(this, new OnSuccessListener<Location>() {
                    @Override
                    public void onSuccess(Location location) {
                        // Got last known location. In some rare situations this can be null.
                        pickupLocation = new LatLng(location.getLatitude(), location.getLongitude());

                        pickupMarker = mMap.addMarker(new MarkerOptions()
                                .position(pickupLocation)
                                .icon(BitmapDescriptorFactory.fromResource(R.drawable.location_point))
                                .anchor(0.5f,0.5f));

                        mMap.moveCamera(CameraUpdateFactory.newLatLng(pickupLocation));
                        mMap.animateCamera(CameraUpdateFactory.zoomTo(17));

                        Log.i("mapCurrentLoc", location.getLatitude()+ ", " + location.getLongitude());
                        if (location != null) {
                            // Logic to handle location object
                        }
                    }
                });



        MapStyleOptions style = MapStyleOptions.loadRawResourceStyle(this, R.raw.map_style);
        mMap.setMapStyle(style);


        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(2000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);




        if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            if(ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){

            }else{
                checkLocationPermission();
            }
        }

        mFusedLocationClient.requestLocationUpdates(mLocationRequest, mSecondLocationCallback, Looper.myLooper());
    }

Gradle 文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.android.dtri"
        minSdkVersion 23
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
//    compileOptions {
//        sourceCompatibility JavaVersion.VERSION_1_8
//        targetCompatibility JavaVersion.VERSION_1_8
//    }

}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'androidx.appcompat:appcompat:1.1.0-rc01'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta2'
    implementation 'com.google.android.gms:play-services-maps:17.0.0'
    testImplementation 'junit:junit:4.13-beta-3'
    androidTestImplementation 'androidx.test:core:1.2.0'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test:runner:1.3.0-alpha02'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0-alpha02'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    // navigation menu
    implementation 'com.google.android.material:material:1.1.0-alpha08'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'androidx.recyclerview:recyclerview:1.1.0-beta01'
    // circle image library
    implementation 'de.hdodenhof:circleimageview:3.0.0'
    implementation 'com.squareup.picasso:picasso:2.71828'
    implementation 'com.squareup.retrofit2:retrofit:2.6.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.6.0'
    implementation 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.1.0'
    // for FCM _ push notification
    implementation 'com.google.firebase:firebase-core:17.0.1'
    implementation 'com.google.firebase:firebase-messaging:19.0.1'
//    implementation 'com.google.firebase:firebase-auth:17.0.0'
//    implementation 'com.firebaseui:firebase-ui-auth:4.0.1'


    implementation 'com.github.stankoutils:app:1.3.10'
    implementation 'com.github.devlight.navigationtabstrip:navigationtabstrip:1.0.4'
    implementation 'com.github.bumptech.glide:glide:4.9.0'
    implementation 'org.jetbrains:annotations-java5:15.0'
    implementation project(path: ':infinitecycleviewpager')


    implementation 'com.github.AnyChart:AnyChart-Android:1.1.2'
    implementation 'com.google.android.material:material:1.1.0-alpha08'

    implementation 'com.android.support:appcompat-v7:28.0.0'

    // google map
    implementation 'com.github.jd-alexander:library:1.1.0'
//    implementation 'com.google.android.gms:play-services:12.0.1'
    //implementation 'com.google.android.gms:play-services-location:17.0.0'
//    implementation 'com.google.android.libraries.places:places:2.0.0'


    //implementation 'com.getkeepsafe.dexcount:dexcount-gradle-plugin:0.8.6'
//    implementation 'com.google.ar.sceneform.ux:sceneform-ux:1.10.0'


    implementation 'com.android.support:support-v4:28.0.0'
    implementation 'com.google.android.gms:play-services-basement:17.0.0'
    implementation 'com.google.android.gms:play-services-base:17.0.0'
    implementation 'com.google.android.gms:play-services-gcm:17.0.0'
    implementation 'com.google.android.gms:play-services-location:17.0.0'
    implementation 'com.google.android.libraries.places:places:2.0.0'

    implementation 'com.google.android.gms:play-services-maps:17.0.0'

}

apply plugin: 'com.google.gms.google-services'

当它按我想要的方式工作时所拍摄的屏幕 enter image description here

map 当前结果 enter image description here

最佳答案

正在调用您的 onMapReady 函数。这里发生的情况是,您的设备上尚未授予权限,因此您立即中断了该功能。

您可以按如下方式自行测试:

public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    Log.i("mapReady", "map is ready");

    if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        Log.i("permissionDenied", "no permission");
        return;
    }

    mFusedLocationClient.getLastLocation()
    ...
}

您应该能够在日志中看到“ map 已准备好”和“无权限”。允许在运行应用的设备上进行位置访问应该可以修复您的 map 。

希望这有帮助!

关于java - 即使 map 显示没有任何错误,也不会调用 onMapReady,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57234192/

相关文章:

android - 带有房间和状态处理的 Kotlin Coroutines Flow

java - Spring 框架

java - Jersey ModelValidationException - 未找到注入(inject)源

android - Kotlin 结构化并发 [协程] 模型是否适用于适合数据库写入的 UI?

android - 折线不遵循两个位置之间的实际路径

android - 在 MapFragment 中膨胀类 fragment 时出错

android - Flutter Location Package帮助:我想将用户证明的坐标提取为单独的var/string或double

java - 可配置的依赖关系,易于模拟默认实现

java - 如何在不测试调用数据库的私有(private)方法的情况下进行单元测试

android:如何从触摸事件中获取文本位置