1

I'm using JavaScript interface for checking if Google's StreetView is available. My problem is that from android 3.0 code stopped working, and I am unnable to find why. Problem is that methods from "JavascriptCheck" interface are never called and Logcat doesn't show any errors.

Java code:

public void showStreetView(GeoPoint geoPoint) {
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webView.addJavascriptInterface(new JavascriptCheck(), "Android");

    lat = geoPoint.getLatitudeE6()/1E6;
    lon = geoPoint.getLongitudeE6()/1E6;

    webView.loadDataWithBaseURL("", context.getString(R.string.html_streetview, lat, lon), "text/html", "UTF-8", "");

}

public class JavascriptCheck {

    public void hasStreetview(boolean hasStreetview) {
        if (hasStreetview) {
            openStreetView();
        } else {
            Toast.makeText(context, context.getString(R.string.loc_no_street_view), Toast.LENGTH_SHORT).show();
        }
    }

}

WebView in layout file:

<WebView android:id="@+id/webView" 
    android:layout_height="wrap_content"
    android:layout_width="wrap_content" 
    android:visibility="gone">
</WebView>

JavaScript string:

 <string name="html_streetview">
     &lt;html>
         &lt;head>
             &lt;script src=\"http://maps.google.com/maps?file=api&amp;v=2&amp;  sensor=false\" type=\"text/javascript\"/>
         &lt;/head>
         &lt;body>
             &lt;script type=\"text/javascript\">
                 var testPoint = new GLatLng(%1$s, %2$s);
                 var svClient = new GStreetviewClient();

                 svClient.getNearestPanoramaLatLng(testPoint, function (nearest) {
                     if ((nearest !== null) &amp;&amp; (testPoint.distanceFrom(nearest) &lt;= 100)) {
                         Android.hasStreetview(true);
                     } else {
                         Android.hasStreetview(false);
                     }
                 });
             &lt;/script>
         &lt;/body>
     &lt;/html>
</string>
1
  • 1
    You should probably not keep the html in strings.xml. Put them in assets instead. Commented Aug 22, 2011 at 14:34

2 Answers 2

2

Solved my problem long ago, just wanted to share with others. Honeycomb and later Android versions require that you use full html <script> tags. Also it is better to keep script string in assets folder. My assets/index.html looks like this now:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<script src="http://maps.googleapis.com/maps/api/js?sensor=false" type="text/javascript"></script>
<script type="text/javascript">

  var sv = new google.maps.StreetViewService();

  function hasStreet(lat, lon) {
    var point = new google.maps.LatLng(lat, lon);
    sv.getPanoramaByLocation(point, 50, isSVAvailable);
  }

  function isSVAvailable(data, status) {
    if (status == google.maps.StreetViewStatus.OK) {
      Android.hasStreetview(true);
    } else {
      Android.hasStreetview(false);
    }
  }
</script>
</head>
<body></body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

0

I too was using this function and have seen it broken since I tried upgrading my app for ICS. It seems that the Javascript won't execute if you have an external src link. If you take out the javascript src link and add some logging you'll see that the script will run (and obviously return false all the time).

I know in the docs they recommend not using javascript that calls into your native code unless you control all elements in the javascript but perhaps now they explicitly stop code from running that references an external resource?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.