首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android:街景只显示黑屏

问Android:街景只显示黑屏
EN

Stack Overflow用户
提问于 2017-09-13 14:21:21
回答 2查看 1.3K关注 0票数 0

我试图在我的安卓应用程序中显示街景,但当我运行我的应用程序时,我的安卓设备上出现了黑屏,而不是街景。我使用StreetViewPanoramaView。任何帮助我的XML代码都在这里,我使用这个...

代码语言:javascript
复制
<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活动,我想在这里展示街景。

代码语言:javascript
复制
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);
}

}
EN

回答 2

Stack Overflow用户

发布于 2017-09-13 15:01:45

我检查了你的代码,用你同样的代码做了一个演示应用程序,它对我来说工作得很好。我刚删除了片断代码。我猜您的Google Maps API密钥有问题。你添加了吗?如果没有,请按照以下说明操作:

  1. 从控制台获取Google API密钥。请使用此链接,了解在AndroidManifest.xml inside application tag

中使用Google API密钥的步骤

例如

代码语言:javascript
复制
<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. 运行它,它就会工作。

希望这对你有用!

票数 0
EN

Stack Overflow用户

发布于 2019-04-04 10:10:57

这项工作。

AndroidManifest.xml

代码语言:javascript
复制
    <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" />

gradle

代码语言:javascript
复制
   implementation 'com.google.android.gms:play-services-maps:16.1.0'

活动

代码语言:javascript
复制
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)

代码语言:javascript
复制
   <?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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46190167

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档