JavaScriptInterface()在android中不起作用

时间:2011-02-14 07:39:19

标签: javascript android html

我已经通过引用此链接http://code.google.com/intl/en/apis/maps/articles/android_v3.html#why

编写了android代码和地图(html)代码

我出去了相同的代码,但当我改变这样的HTML代码时,

这是我的HTML代码

<!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>

这是我的安卓代码

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

点击我的html代码中的“获取路线”按钮后,我没有获得路线指示,请帮助我的代码出错。

2 个答案:

答案 0 :(得分:0)

也许您应该在directionsService.route的响应处理函数中添加提醒,以查看Google地图的回复情况?

我的猜测是“华盛顿”和“纽约”不足以解析谷歌地图。

如果我在谷歌地图方向网页上输入这些条款,除了让我澄清哪个'华盛顿'和'纽约'是什么意思(我没有从两个下拉菜单中选择任何东西,'让它骑')回来的结果是一个标题为“你的意思是?”的专栏。以及“纽约”名单。

答案 1 :(得分:-1)

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