简体   繁体   中英

JavaScriptInterface() is not working in android

i have written android code and map(html) code by referring this link http://code.google.com/intl/en/apis/maps/articles/android_v3.html#why

i got out put for the same code, but when i changed the html code like this,

HERE IS MY HTML Code

<!DOCTYPE html>
 <html>
<head> 
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/> 
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
<title>Google Maps JavaScript API v3 Example: Directions Simple</title> 
<link href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css" /> 
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 
<script type="text/javascript"> 

var directionDisplay;
var directionsService = new google.maps.DirectionsService();
var map;

function initialize() {
    directionsDisplay = new google.maps.DirectionsRenderer();
    var chicago = new google.maps.LatLng(41.850033, -87.6500523);
    var myOptions = {
      zoom:7,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      center: chicago
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    directionsDisplay.setMap(map);
}

function calcRoute() {
    var start ;
    var end ;
    if (window.android){
      start= window.android.getsource();
     end = window.android.getdestination();
    }
    var request = {
        origin:start, 
        destination:end,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };
    directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);
    }
 });
}
</script> 
</head> 
<body onload="initialize()"> 
<div> 
<input id="latlng" type="textbox" value="Chicago"> 
<input id="latlng1" type="textbox" value="Kingman">
</div> 
<div> 
<input type="button" value="Get Route" onclick="calcRoute()"> 
</div> 

<div id="map_canvas"></div> 
</body> 
</html>

HERE IS MY ANDROID Code

package com.example.test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class tset extends Activity {
private static final String MAP_URL = "http://192.168.1.119:81/fulfed1.html";
  private WebView webView;

     public static String disp;
  @Override
  /** Called when the activity is first created. */
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);



    webView = (WebView) findViewById(R.id.webview);
    webView.getSettings().setJavaScriptEnabled(true);
    //Wait for the page to load then send the location information
    webView.setWebViewClient(new WebViewClient());
    webView.loadUrl(MAP_URL);
    /** Allows JavaScript calls to access application resources **/
    webView.addJavascriptInterface(new JavaScriptInterface(), "android");
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  }
  /** Sets up the WebView object and loads the URL of the page **/
  public class JavaScriptInterface
  {
    public String getsource(){
        return "Washington";
    }
    public String getdestination(){
        return  "Newyork";
    }
    }
  }

i am not getting route direction upon click on "Get route" button in my html code,please help what's wrong in my code.

Maybe you should stick an alert in your response handler function in directionsService.route to see what Google Maps is returning?

My guess is that "Washington" and "Newyork" aren't specific enough to resolve for Google Maps.

If I enter those terms in the Google Maps Directions web page, besides asking me to clarify which 'Washington' and which 'Newyork' I mean (I didn't select anything from the two dropdowns and 'let it ride'), the results that came back were a column titled "Did you mean?" and a list of "Newyork" places.

我找到了答案,实际上我已将ascii转换为字符串。

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