2014-05-19 77 views
0

我编写了一个HTML文件,其中有一些功能允许我访问最近的街景视图坐标。它在我的桌面Chrome浏览器中完美运行。但是,当我将它连接到Android WebView时,它不允许Google Maps API在注入JavaScript之后进行回调。证明是,在启动函数(getNearestStreetViewLocation(lat,lng))中的调试消息完美显示时,不会显示调试消息,该消息放入回调函数(initialize())中。请帮忙!Android WebView不允许Google Maps API回拨

更新:问题在于WebView似乎不允许Google API访问HTML内部的回调函数。因为我刚测试加载一个本地脚本;它适用于Android;因此不是脚本没有被追加,但回调函数从不被调用。

HTML代码

<html> 
    <head> 

    <!-- 
     <script type="text/javascript" src="js/script.js"></script> 
     --> 

    <script type="text/javascript"> 
    var finalLat; 
    var finalLng; 
    var streetViewService; 
    var thisLat; 
    var thisLng; 
    var latlng; 
    var radius = 1; 


    function initialize() { 
     document.writeln("google service initialized" + "<br>"); 
     JSInterface.JSDebug("google service initialized"); 

     streetViewService = new google.maps.StreetViewService(); 
     latLng = new google.maps.LatLng(thisLat, thisLng, false); 

     streetViewService.getPanoramaByLocation(latLng, radius, panoramaHandler); 
    } 

    function panoramaHandler(data, status) { 
     if (status == google.maps.StreetViewStatus.OK) { 
      var nearestLocation = data.location.latLng; 

      finalLat = nearestLocation.lat(); 
      finalLng = nearestLocation.lng(); 

      document.writeln(finalLat + "<br>"); 
      document.writeln(finalLng + "<br>"); 

      JSInterface.returnLocation(finalLat); 

     } else { 
      document.writeln("in vicinity of " + radius + ", status is " + status + "<br>"); 

      radius += 50; 
      streetViewService.getPanoramaByLocation(latLng, radius, panoramaHandler); 
     } 
    } 


    //javascript:getNearestStreetViewLocation(32.870804,-117.230495) 

    function getNearestStreetViewLocation(lat,lng) { 
     thisLat = lat; 
     thisLng = lng; 

var js = document.createElement('script'); 
    js.src = 'https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&callback=initialize'; 
    //js.src = "externalTest.js"; 
    var first = document.getElementsByTagName('script')[0]; 
    first.parentNode.insertBefore(js, first); 

     document.writeln("main function executed" + "<br>"); 
     JSInterface.JSDebug("main function executed"); 
    } 


    </script> 
    </head> 
    <body> 
     <h3>TEST ONGOING...</h3> 
    </body> 
    </html> 

的Android侧

// Initialize JavaScript. 
     myWebView = (WebView)this.findViewById(R.id.streetFunctionWebView); 
     myWebView.getSettings().setJavaScriptEnabled(true); 
     myWebView.addJavascriptInterface(new JavaScriptHandler(), "JSInterface"); 
     myWebView.loadUrl(HTML_FUNCTION_PATH); 

     // Run JavaScript 
     this.getNearestLocation(32,-117); 

的Android功能getNearestLocation(纬度,经度)

public void getNearestLocation(final int lat, final int lng){ 
     myWebView.setWebChromeClient(new WebChromeClient(){ 
      public void onProgressChanged (WebView view, int newProgress){ 
       //myWebView.loadUrl("javascript:debugMan()"); 
       if(newProgress == 100) 
        myWebView.loadUrl("javascript:getNearestStreetViewLocation("+ lat + "," + lng + ")"); 
      }   
     }); 

     //Log.v("MY_DEBUG", "getNearestLocation() in main executed!"); 
    } 

回答

2

我忘了打开手机上的Wifi。

+1

请只删除你的问题,而不是张贴无用的答案。没有人离开这个答案或这个问题。 – meagar

相关问题