Google地图无法在发布模式下运行,但在调试模式下可以正常运行

时间:2019-04-05 18:42:25

标签: android

Google地图活动在发布模式下不起作用,但在调试模式下工作正常。

尝试了与我的查询相关的堆栈溢出的所有内容,但无法正常工作。

我还使用“ keytool -list -v -keystore mystore.keystore”从cmd提示符复制了SHA1密钥,并将其粘贴到了Google控制台中,但仍然无法正常工作。

在调试模式下,一切正常。 我还为Android棉花糖及更高版本授予了运行时权限。

注意点: 我用mapView代替了片段。 我也尝试过使用地图片段,但是仍然是同样的问题。

activity_maps.xml

<com.google.android.gms.maps.MapView
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

您可以在这里看到区别:

In debug modeIn release mode

等级:

implementation 'com.google.android.gms:play-services-maps:16.1.0'
implementation 'com.google.android.gms:play-services-location:16.0.0'

AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> 
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> 
<permission android:name="com.example.mapsact.permission.MAPS_RECEIVE" android:protectionLevel="signature" /> 
<uses-permission android:name="com.example.mapsact.permission.MAPS_RECEIVE" />

MapsActivity.java

MapView mMapView;
GoogleMap mMap;
LocationManager location;

@Override
public void onResume() {
    super.onResume();

    try {
        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 20, 1, (android.location.LocationListener) this);
    } catch (SecurityException e) {
        e.printStackTrace();
    }
    mMapView.onResume();
    //ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}

@Override
public void onLocationChanged(Location location) {

    // Getting latitude of the current location
    latitude = location.getLatitude();
    // Getting longitude of the current location
    longitude = location.getLongitude();
    // Creating a LatLng object for the current location
    LatLng latLng = new LatLng(latitude, longitude);
    //now = mMap.addMarker(new MarkerOptions().position(latLng).title("Location").snippet("Current"));
    // Showing the current location in Google Map
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    mMap.animateCamera(CameraUpdateFactory.zoomTo(15.0f));

    if(isNetworkConnected()) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("mobile", mobile);
            jsonObject.put("lati", latitude);
            jsonObject.put("longi", longitude);
            SendLoc sendloc = new SendLoc();
            sendloc.execute(jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    else {
        Toast.makeText(this,"No Internet Connectivity",Toast.LENGTH_LONG).show();
    }
}
    public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    mMap.animateCamera(CameraUpdateFactory.zoomTo(15.0f));
    mMap.setMyLocationEnabled(true);
}

2 个答案:

答案 0 :(得分:1)

只是一个猜测,但在发布模式下,我认为您需要为清单定义的键是来自清单

<!--
         The API key for Google Maps-based APIs is defined as a 
          string resource.
         (See the file "res/values/google_maps_api.xml").
         Note that the API key is linked to the encryption key 
          used to sign the APK.
         You need a different API key for each encryption key, 
          including the release key that is used to
         sign the APK for publishing.
         You can define the keys for the debug and release 
          targets in src/debug/ and src/release/. 
    -->

答案 1 :(得分:0)

自最近几天以来,我也面临着同样的问题,我花了大约2-3天的时间才弄清问题所在。您需要在两个位置添加API密钥,一个在app / src / debug / res / values / google_maps_api.xml中,另一个在app / src / release / res / values / google_maps_api.xml中。

您可以在项目模式下(而非Android /应用程序模式下)找到release / google_maps_api.xml。

You can check the pic here that where the release/google_maps_api.xml is available.