How to add/integrate Google Street View Panorama View in our Android application

Many times in Android location aware apps we need to integrate Google Maps for better location utilization. But Google Street View Panorama View provides more detailed location functionality utilization with a sweet touch of virtual reality and augmented reality in our application with 360° View.

There are two way of integrating Google Street View Panorama View in our application.

1. Using google.streetview Intent to launch Google Street View Panorama View explicitly.

Using this approach we just need to pass
1. Either Latitude, Longitude(cbll) or Panorama ID(panoid)
2. Bearing{0 or 90 or 180 or 270}, Zoom(0 or 1), Tilt(-90 through 0 to 90) (cbp) //Ideal value for both three is 0,0,0

Check below two example intents Strings.

"google.streetview:cbll=latitude,longitude&cbp=0,bearing,0,zoom,tilt"
"google.streetview:panoid=id&cbp=0,bearing,0,zoom,tilt"

If you have Panorama ID available I would suggest you to go for second intent option otherwise option one is more general approach.

One more thing to notice that the values of cbp should be in same order as mentioned in above intent examples. (first and third value should be 0 as not supported via Intent action, second, fourth and fifth values should be bearing, zoom and tilt)

Check below examples for uses for above example street views.

// Displays an image of the Taj Mahal India
Uri gsvIntentUri = Uri.parse("google.streetview:cbll=27.1739919,78.041993");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gsvIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
if (mapIntent.resolveActivity(getPackageManager()) != null) {
startActivity(mapIntent);
}else{
// Use below second approach
}
//or
// Uses a PanoramaID to show an image from Ladakh, India
Uri gsvIntentUri = Uri.parse("google.streetview:panoid=AF1QipNL4ATF7M9FYDoQ_1Fa7JAIDNZir5BKoY8sFLCX");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gsvIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
if (mapIntent.resolveActivity(getPackageManager()) != null) {
startActivity(mapIntent);
}else{
// Use below second approach
}
//or
// Opens Street View of Kumbhal Garh Fort View Point. The values passed to the
// cbp parameter will angle the camera slightly up, and towards the east.
Uri gsvIntentUri = Uri.parse("google.streetview:cbll=25.1444952,73.5807512&cbp=0,30,0,0,-15");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gsvIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
if (mapIntent.resolveActivity(getPackageManager()) != null) {
startActivity(mapIntent);
}else{
// Use below second approach
}

Many times it may possible that current device running our application may not have that Google Street View Panorama View feature. In that case we need to check it first and then we need to show it in our app by integrating our own Google Street View Panorama View in our application as per below steps. This approach will be the more suitable as, in real application environment some devices which supports Google Street View Panorama View feature will use this feature using Intent and others will show below custom integration of Google Street View Panorama View feature in our application.

2. By integrating our own Google Street View Panorama View using XML designing and then implementing in our Activity.

First add below dependency in your application’s build.gradle as Google Street View Panorama View feature requires, Google Play Services MAP dependency.

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

Then just add it to your Activity/Fragment layout.

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

And use it in your Activity/Fragment: (Activity in this case)

package in.quicktips.google.streetviewpanoramaview;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.google.android.gms.maps.OnStreetViewPanoramaReadyCallback;
import com.google.android.gms.maps.StreetViewPanorama;
import com.google.android.gms.maps.StreetViewPanoramaView;
import com.google.android.gms.maps.model.LatLng;
public class MainActivity extends AppCompatActivity {
private StreetViewPanoramaView mStreetViewPanoramaView;
private StreetViewPanorama mPanorama;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mStreetViewPanoramaView = (StreetViewPanoramaView) findViewById(R.id.street_view_panorama);
mStreetViewPanoramaView.onCreate(savedInstanceState);
mStreetViewPanoramaView.getStreetViewPanoramaAsync(new OnStreetViewPanoramaReadyCallback() {
@Override
public void onStreetViewPanoramaReady(StreetViewPanorama panorama) {
//Here you can pass either latitude, longitude or panorama id for position.
panorama.setPosition(new LatLng(27.1739919,78.041993));
// or panorama.setPosition("AF1QipNL4ATF7M9FYDoQ_1Fa7JAIDNZir5BKoY8sFLCX");
mPanorama = panorama;
}
});
}
@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);
}
}

It is mandatory to implement the life cycle methods of Google Street View Panorama View in Activity/Fragment to avoid crash in application.

Let us know in comments below if you have any problem implementing Google Street View Panorama View or you have any suggestion.

 

(Visited 14 times, 2 visits today)