2017-03-12 33 views
0

我瀏覽過文檔,找不到任何爲什麼MapFragment只顯示灰色屏幕,左下角帶有Google徽標的原因。在我的清單中,我在應用程序級別上有適當的元數據(GMS版本號和api鍵)。模塊gradle文件具有所有需要的依賴關係。該錯誤可能是在Java代碼中,主要內容如下:MapFragment不顯示地圖

import android.content.pm.PackageManager; 
import android.location.Location; 
import android.location.LocationListener; 
import android.os.Bundle; 
import android.support.annotation.NonNull; 
import android.support.annotation.Nullable; 
import android.support.v4.app.ActivityCompat; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.content.ContextCompat; 

import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.android.gms.location.LocationServices; 
import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.OnMapReadyCallback; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions; 
import com.google.android.gms.maps.model.Polyline; 
import com.google.android.gms.maps.model.PolylineOptions; 

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, 
     GoogleApiClient.OnConnectionFailedListener, LocationListener { 

    private GoogleMap mMap; 
    private Polyline polyline; 
    private PolylineOptions mPLO; 
    private GoogleApiClient mGoogleApiClient; 
    private boolean mLocationPermissionGranted; 
    private static final int PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1; 
    private Location mLastKnownLocation; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_maps); 
     // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .enableAutoManage(this /* FragmentActivity */, 
         this /* OnConnectionFailedListener */) 
       .addConnectionCallbacks(this) 
       .addApi(LocationServices.API) 
       .build(); 
     mGoogleApiClient.connect(); 
    } 


    /** 
    * Manipulates the map once available. 
    * This callback is triggered when the map is ready to be used. 
    * This is where we can add markers or lines, add listeners or move the camera. In this case, 
    * we just add a marker near Sydney, Australia. 
    * If Google Play services is not installed on the device, the user will be prompted to install 
    * it inside the SupportMapFragment. This method will only be triggered once the user has 
    * installed Google Play services and returned to the app. 
    */ 
    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     mMap = googleMap; 
     LatLng sydney = new LatLng(-33.852, 151.211); 
     mMap.addMarker(new MarkerOptions().position(sydney) 
       .title("Marker in Sydney")); 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 
     //mPLO = new PolylineOptions().geodesic(true);; 
     //polyline = mMap.addPolyline(mPLO); 

     // Turn on the My Location layer and the related control on the map. 
     // updateLocationUI(); 

     // Get the current location of the device and set the position of the map. 
     //getDeviceLocation(); 
    } 

    @Override 
    public void onConnected(@Nullable Bundle bundle) { 
     SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 

    } 

    @Override 
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 

    } 

    @Override 
    public void onLocationChanged(Location location) { 
     // mPLO.add(new LatLng(location.getLatitude(), location.getLongitude())); 
    } 

    @Override 
    public void onProviderDisabled(String s) { 

    } 

    @Override 
    public void onProviderEnabled(String s) { 

    } 

    @Override 
    public void onStatusChanged(String s, int i, Bundle bundle) { 

    } 

    private void getDeviceLocation() { 
     if (ContextCompat.checkSelfPermission(this.getApplicationContext(), 
       android.Manifest.permission.ACCESS_FINE_LOCATION) 
       == PackageManager.PERMISSION_GRANTED) { 
      mLocationPermissionGranted = true; 
     } else { 
      ActivityCompat.requestPermissions(this, 
        new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 
        PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION); 
     } 
     // A step later in the tutorial adds the code to get the device location. 
    } 

    /** 
    * Handles the result of the request for location permissions. 
    */ 
    @Override 
    public void onRequestPermissionsResult(int requestCode, 
              @NonNull String permissions[], 
              @NonNull int[] grantResults) { 
     mLocationPermissionGranted = false; 
     switch (requestCode) { 
      case PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: { 
       // If request is cancelled, the result arrays are empty. 
       if (grantResults.length > 0 
         && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
        mLocationPermissionGranted = true; 
       } 
      } 
     } 
     updateLocationUI(); 
    } 

    /** 
    * Updates the map's UI settings based on whether the user has granted location permission. 
    */ 
    private void updateLocationUI() { 
     if (mMap == null) { 
      return; 
     } 

     /* 
     * Request location permission, so that we can get the location of the 
     * device. The result of the permission request is handled by a callback, 
     * onRequestPermissionsResult. 
     */ 
     if (ContextCompat.checkSelfPermission(this.getApplicationContext(), 
       android.Manifest.permission.ACCESS_FINE_LOCATION) 
       == PackageManager.PERMISSION_GRANTED) { 
      mLocationPermissionGranted = true; 
     } else { 
      ActivityCompat.requestPermissions(this, 
        new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 
        PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION); 
     } 

     if (mLocationPermissionGranted) { 
      mMap.setMyLocationEnabled(true); 
      mMap.getUiSettings().setMyLocationButtonEnabled(true); 
     } else { 
      mMap.setMyLocationEnabled(false); 
      mMap.getUiSettings().setMyLocationButtonEnabled(false); 
      mLastKnownLocation = null; 
     } 
    } 

} 

的logcat中顯示了以下錯誤

234-31464/com.gjd.capstone E/Google Maps Android API: Authorization failure. Please see https://developers.google.com/maps/documentation/android-api/start for how to correctly set up the map. 
03-12 15:30:12.967 30234-31464/com.gjd.capstone E/Google Maps Android API: In the Google Developer Console (https://console.developers.google.com) 
                      Ensure that the "Google Maps Android API v2" is enabled. 
                      Ensure that the following Android Key exists: 
                      API Key: <Redacted> 
                      Android Application (<cert_fingerprint>;<package_name>): 47:B9:AC:D7:30:4D:AB:2D:6E:29:06:99:24:B1:7C:07:95:0A:8B:96;com.gjd.capstone 

我甚看着開發商控制檯查看API是否被禁用,它不是。我仍然不確定該做什麼。我不確定是否啓用了「Google地圖Android API v2」,但我無法找到它的選項。

+0

您是否設置了API密鑰? – azizbekian

+0

是的,我有。我可以在清單和google_maps_api.xml文件中看到它。當我生成mapfragment文件是自動生成的時候,我按照指示獲取api鍵並粘貼到我需要的地方。 –

+0

請上傳logcat輸出 –

回答

0

我找到了解決我的問題的方法。在創建MapsActivity之後,我更改了java文件的包,但沒有在dev控制檯中更改包名。

0

通過在另一臺PC上打開項目,MapFragmant不再顯示。爲了解決這個問題,我在我的電腦上用密鑰庫簽了我的應用程序。

在xamarin 1.創建我的應用程序的存檔(一個APK) 2.創建一個密鑰,如果它沒有這樣做 3.單擊分配 4.選擇特設 5.選擇密鑰庫 6.點擊保存並保存至應用程序 7.重建項目