alshapton alshapton - 6 months ago 34
Java Question

Ascertain Google Streetview function existence

Is there a way of ascertaining if a Google Streetview panorama is available from an Android application (i.e. using Java).

No alternatives seem to exist for PHP or Python or other server-side technologies.

The impact of calling Google Streetview where no panorama exists is simply a black screen and a "spinning thing".

Answer

I created a little hack for this. :)

strings.xml

<string name="html_streetview">    <![CDATA[
<html>
<head>
   <script src="http://maps.google.com/maps/api/js?v=3&amp;sensor=false" type="text/javascript"></script>
 </head>
<body>
<script type="text/javascript">
 Android.echo();
 var testPoint = new google.maps.LatLng(%1$s, %2$s,true);
 var svClient = new google.maps.StreetViewService();
 svClient.getPanoramaByLocation(testPoint, 50,function (panoramaData, status) {
   if (status == google.maps.StreetViewStatus.OK) {
     Android.hasStreetview();
   } else {
     Android.hasNotStreetview();
   }
 });
</script>
</body>
</html>
]]>
</string>

now add a button for streetview on the activity and put this following code into the onclick method:

    if (webView == null) {
      webView = new WebView(this);
      webView.setVisibility(View.INVISIBLE);
      webView.getSettings().setJavaScriptEnabled(true);
      webView.addJavascriptInterface(new JavascriptCheck(this), "Android");
      webView.setWebViewClient(new WebViewClient() {
          public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
               Toast.makeText(this, "Streetview loading", Toast.LENGTH_SHORT).show();
               super.onReceivedError(view, errorCode, description, failingUrl);
                }
      });
    }

    Toast.makeText(this, "Streetview loading", Toast.LENGTH_SHORT).show();

    webView.loadDataWithBaseURL(baseurl, 
      getString(R.string.html_streetview, latitude, longitude), "text/html", "UTF-8", baseurl);

And now the inner Class of the activity:

public class JavascriptCheck {
   private final Context context;

   public JavascriptCheck(Context context) {
      this.context = context;
   }

   public void echo() {
       Log.d("JavascriptChecker", "javascript called");
   }

   public void hasStreetview() {
       pushStreetviewState(true);
   }

   public void hasNotStreetview() {
      pushStreetviewState(false);
   }

   private void pushStreetviewState(final boolean hasStreetview) {
       Log.d("JavascriptChecker", hasStreetview);
       // TODO do your stuff needed here
   }
}

this a a rather bad workaround but probably can help. :)