简体   繁体   中英

Android: Street view shows only black screen

I am trying to show Street View in my android application but I got a black screen on my android device instead of street view when I run my application.I use StreetViewPanoramaView. Any Help My XML Code is here in XML I use this ...

<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 is my java activity where I am trying to show the street view.

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

}

I checked out your code, made a demo App with your same code and it worked perfectly for me. I just removed the fragment code. I guess you have an issue with your Google Maps API key. Have you added it? If not please follow following instructions :

  1. Get Google API key from console. Follow this link for steps https://developers.google.com/maps/documentation/android-api/signup

  2. Add your Google API key in AndroidManifest.xml inside application tag

for eg

<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. Run it, it will work.

Hope that works for you!

This work.

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" />

gradle

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

Activity

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

}

Layout(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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM