簡體   English   中英

Android:街景僅顯示黑屏

[英]Android: Street view shows only black screen

我試圖在我的android應用程序中顯示街景視圖,但是運行我的應用程序時,我的android設備上出現黑屏而不是街景視圖。我使用StreetViewPanoramaView。 任何幫助我的XML代碼都在XML中,我使用它來...

<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);
}

}

我簽出了您的代碼,並使用相同的代碼制作了一個演示應用程序,它對我來說非常理想。 我剛剛刪除了片段代碼。 我想您的Google Maps API密鑰有問題。 你加了嗎? 如果沒有,請按照以下說明進行操作:

  1. 從控制台獲取Google API密鑰。 請點擊此鏈接以獲取步驟https://developers.google.com/maps/documentation/android-api/signup

  2. 在應用程序標記內的AndroidManifest.xml中添加您的Google 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>
  1. 運行它,它將起作用。

希望對您有用!

這項工作。

AndroidManifest.xml

    <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
    <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="Your API Key" />

搖動

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

活動

class MainActivity : AppCompatActivity(), OnStreetViewPanoramaReadyCallback {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val streetViewPanoramaFragment = supportFragmentManager
        .findFragmentById(R.id.streetViewMap) as SupportStreetViewPanoramaFragment
    streetViewPanoramaFragment.getStreetViewPanoramaAsync(this)

}

override fun onStreetViewPanoramaReady(panorama: StreetViewPanorama?) {
    panorama?.setPosition(LatLng(-33.87365, 151.20689))
}

}

布局(activity_main)

   <?xml version="1.0" encoding="utf-8"?>
   <FrameLayout 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"
         tools:context=".MainActivity">
   <fragment
        android:id="@+id/streetViewMap"
        android:name="com.google.android.gms.maps.SupportStreetViewPanoramaFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
   </FrameLayout>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM