【问题标题】:Android: Street view shows only black screenAndroid:街景仅显示黑屏
【发布时间】:2017-09-13 06:21:20
【问题描述】:

我试图在我的 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);
}

}

【问题讨论】:

    标签: android maps google-street-view


    【解决方案1】:

    我检查了您的代码,并使用您的相同代码制作了一个演示应用程序,它非常适合我。我刚刚删除了片段代码。我猜您的 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. 运行它,它会工作。

    希望对你有用!

    【讨论】:

    • 感谢兄弟的建议对我有用:)
    【解决方案2】:

    这项工作。

    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>
    

    【讨论】:

      猜你喜欢
      • 2022-09-26
      • 1970-01-01
      • 2022-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多