【问题标题】:JavaScriptInterface() is not working in androidJavaScriptInterface() 在 android 中不起作用
【发布时间】:2011-02-14 07:39:19
【问题描述】:

我已经通过引用此链接http://code.google.com/intl/en/apis/maps/articles/android_v3.html#why编写了android代码和map(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>

这是我的 ANDROID 代码

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 代码中单击“获取路线”按钮后,我没有获得路线方向,请帮助我的代码中出了什么问题。

【问题讨论】:

  • 嗯.. 为什么你期待另一种行为? calcRoute() 仅绑定到按钮,而不绑定到 onLoad 事件...

标签: javascript android html


【解决方案1】:

也许您应该在directionsService.route 的响应处理函数中添加一个警报,以查看 Google 地图返回的内容?

我的猜测是“Washington”和“Newyork”不够具体,无法为 Google 地图解析。

如果我在 Google 地图路线网页中输入这些术语,除了要求我澄清我的意思是哪个“华盛顿”和哪个“纽约”(我没有从两个下拉列表中选择任何内容并“让它骑”) ,返回的结果是一个标题为“您的意思是?”的列。以及“纽约”地点列表。

【讨论】:

    【解决方案2】:

    我找到了答案,其实我已经把ascii转成字符串了....

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-15
      • 1970-01-01
      • 1970-01-01
      • 2017-11-02
      • 2019-09-28
      • 1970-01-01
      • 2018-06-18
      相关资源
      最近更新 更多