即使地图显示没有任何错误,也不会调用onMapReady

时间:2019-07-27 16:49:49

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

我正在构建一个包含两个标记的跟踪地图,其中一个是当前位置,第二个是从数据库服务器获取其LatLng,它在两周内运行良好,现在只显示没有任何标记的地图,当我尝试调试时该应用程序,并在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());
    }

成绩文件:

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

地图的当前结果 enter image description here

1 个答案:

答案 0 :(得分:0)

您的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()
    ...
}

您应该能够在日志中看到“地图已准备就绪”和“没有权限”。在运行应用的设备上允许位置访问权限应该可以修复您的地图。

希望这会有所帮助!

相关问题