2017-09-13 62 views
0

我想在我的android應用程序中顯示街景,但我的android設備上顯示的是黑色屏幕,而不是街景時運行我的應用程序。我使用StreetViewPanoramaView。任何幫助 我的XML代碼是在這裏XML我用這個...Android:街景只顯示黑屏

<com.google.android.gms.maps.StreetViewPanoramaView 
    android:layout_below="@+id/place_autocomplete_fragment" 
    android:id="@+id/steet_view_panorama" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"/> 

Livesearch.Java就是我試圖展示街景我的Java活動。

public class Livesearch extends AppCompatActivity { 
StreetViewPanoramaFragment streetViewPanoramaFragment; 

StreetViewPanoramaView mStreetViewPanoramaView; 
private StreetViewPanorama mPanorama; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.livesearch); 
    PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment) 
      getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment); 

    mStreetViewPanoramaView = (StreetViewPanoramaView) findViewById(R.id.steet_view_panorama); 
    mStreetViewPanoramaView.onCreate(savedInstanceState); 

    mStreetViewPanoramaView.getStreetViewPanoramaAsync(new OnStreetViewPanoramaReadyCallback() { 
     @Override 
     public void onStreetViewPanoramaReady(StreetViewPanorama panorama) { 
      panorama.setPosition(new LatLng(55.758818, 37.620587)); 
      mPanorama=panorama; 
     } 
    }); 
    autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() { 
     @Override 
     public void onPlaceSelected(Place place) { 


      Toast.makeText(getApplicationContext(),place.getName(),Toast.LENGTH_LONG).show(); 

     } 

     @Override 
     public void onError(Status status) { 
      // TODO: Handle the error. 
      Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_LONG).show(); 
     } 
    }); 
} 
@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    mStreetViewPanoramaView.onDestroy(); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    mStreetViewPanoramaView.onResume(); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    mStreetViewPanoramaView.onPause(); 
} 

@Override 
public void onLowMemory() { 
    super.onLowMemory(); 
    mStreetViewPanoramaView.onLowMemory(); 
} 

@Override 
protected void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    mStreetViewPanoramaView.onSaveInstanceState(outState); 
} 

} 

回答

0

我檢查了你的代碼,用相同的代碼製作了一個演示應用程序,它完全適合我。我只是刪除了片段代碼。我想你的Google Maps API密鑰存在問題。你添加了嗎?如果沒有,請按照以下說明操作:

  1. 從控制檯獲取Google API密鑰。遵循如內部應用程序標記

此鏈接,在AndroidManifest.xml步驟https://developers.google.com/maps/documentation/android-api/signup

  • 添加您的谷歌API密鑰

    <application 
         android:allowBackup="true" 
         android:icon="@mipmap/ic_launcher" 
         android:label="@string/app_name" 
         android:roundIcon="@mipmap/ic_launcher_round" 
         android:supportsRtl="true" 
         android:theme="@style/AppTheme"> 
    
         <!-- 
          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/. 
         --> 
         <meta-data 
          android:name="com.google.android.geo.API_KEY" 
          android:value="YOUR_KEY" /> 
    
    
         <activity android:name=".MainActivity"> 
          <intent-filter> 
           <action android:name="android.intent.action.MAIN"/> 
    
           <category android:name="android.intent.category.LAUNCHER"/> 
          </intent-filter> 
         </activity> 
        </application> 
    
  • 運行它,它會工作。
  • 希望能爲你效勞!

    +0

    感謝兄弟對你的建議,它爲我工作:) –