【问题标题】:Unable to implement Visualization:Geomap in android webview无法实现可视化:android webview 中的 Geomap
【发布时间】:2014-12-26 12:26:06
【问题描述】:

我正在尝试实现 Visualization: Geomap in android Webview。

对于可视化:Geomap,我参考了 developers.google.com /chart/interactive/docs/gallery/geomap

在这里,我实现了第一个示例来在地图上显示区域。在 android webview 中,当我触摸特定区域时,它会显示带有国家名称的弹出窗口,当我触摸地图的外部区域时,它会显示相同的弹出窗口。

问题:在 android webview 中,当我触摸 Google 地图之外时,弹出窗口不会消失。它应该像在桌面网络浏览器上一样工作

桌面版:

我在桌面上创建了 demo.html 文件。它在网络浏览器中正常运行。

    <html>
  <head>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load("visualization", "1", {packages:["geomap"]});
      google.setOnLoadCallback(drawMap);

      function drawMap() {
        var data = google.visualization.arrayToDataTable([
          ['Country', 'Popularity'],
          ['Germany', 200],
          ['United States', 300],
          ['Brazil', 400],
          ['Canada', 500],
          ['France', 600],
          ['RU', 700]
        ]);

        var options = {};
        options['dataMode'] = 'regions';

        var container = document.getElementById('regions_div');
        var geomap = new google.visualization.GeoMap(container);

        geomap.draw(data, options);
      };
    </script>
  </head>
  <body>
    <div id="regions_div" style="width: 900px; height: 500px;"></div>
  </body>
</html>

安卓版:

DOWNLOAD APP

http://i62.tinypic.com/edb9w.jpg

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.Toast;

public class WebActivity extends ActionBarActivity {

    WebView webView;
    StringBuilder build = new  StringBuilder();

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);

        webView=(WebView)findViewById(R.id.webView1);

        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setLoadWithOverviewMode(true);

        int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        if (currentapiVersion >= android.os.Build.VERSION_CODES.HONEYCOMB)
            webView.setLayerType(View.LAYER_TYPE_SOFTWARE,null);

        webView.setInitialScale(getScale());

        webView.setWebChromeClient(new WebChromeClient());

        build.append("['Germany', 200],");
        build.append("['United States', 700],");
        build.append("['Brazil', 300],");
        build.append("['Canada', 400],");
        build.append("['France', 500]");

        drawMap();
        //webView.loadUrl("http://www.stalwarttech.net/dummy/demo3.html");

    }



    void drawMap()
    {
        if(build.length() > 0)
        {
            String js = "<html><head>" +
                    "<script type='"+"text/javascript"+"' src='"+"https://www.google.com/jsapi"+"'></script>"+
                    "<script type='"+"text/javascript"+"'>" +
                    "google.load('"+"visualization"+"', '"+"1"+"', {packages:['"+"geochart"+"']});" +
                    "google.setOnLoadCallback(drawRegionsMap);" +
                    " function drawRegionsMap() {" +
                    "  var data = google.visualization.arrayToDataTable([" +
                    "['Country', 'Popularity']," + build +
                    "]);" +
                    "var options = {colors: ['#CB96CE', '#871F7B']};" +
                    "var chart = new google.visualization.GeoChart(document.getElementById('regions_div'));" +
                    "chart.draw(data, options);" +
                    "}" +
                    "</script>" +
                    "</head>" +
                    "<body>" +
                    "<div id='"+"regions_div"+"' style='"+"width:100%; height: 100%;"+"'></div>" +
                    "</body>" +
                    "</html>";

            Log.d("tag",js);

            webView.loadDataWithBaseURL("file:///android_asset/", js, "text/html","UTF-8",  null);
        }
        else
        {
            Toast.makeText(this, "No data found", Toast.LENGTH_LONG).show();
        }

    }

    private int getScale(){
        Display display=((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        int width=display.getWidth();
        Double val=new Double(width)/new Double(800);
        val=val*100d;

        return val.intValue();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.web, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

【问题讨论】:

    标签: android webview google-visualization


    【解决方案1】:
    <html> <head>
    
      <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"> 
     </script>
      <script type="text/javascript">
         function loadGeoChart(mapsApiKey, dataList) {
             google.charts.load('current', {
                 'packages': ['geochart'],
                 // Note: you will need to get a mapsApiKey for your project.
                 // See: https://developers.google.com/chart/interactive/docs/basic_load_libs#load-settings
                 'mapsApiKey': mapsApiKey
             });
             google.charts.setOnLoadCallback(function() {
                 drawRegionsMapAll(dataList)
             });
         }
    
         function drawRegionsMapAll(datalist) {
             var chart_data = [
                 ['Country', 'Hits']
             ];
    
             chart_data = chart_data.concat(datalist);
             var data = google.visualization.arrayToDataTable(chart_data);
             var options = {
                 colors: ['#81A9FF', '#2558C6']
             };
             var chart = new google.visualization.GeoChart(document.getElementById('regions_div'));
             chart.draw(data, options);
    
    
         }
    
         function onLoad() {
             Android.getMapData();
         }
         onLoad();
         </script>
      </head>
       <body>
         <div id="regions_div" style="width: 900px; height: 500px;"></div>
       </body>
    </html>
    

    将上面的 geomap.html 文件保存在 assets 文件夹中。

    在 Activity 中,在 webview 中加载 html 文件,启用以下 Websettings。

        WebSettings webSettings = mapweb_view.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setDisplayZoomControls(false);
        webSettings.setSupportZoom(true);
        webSettings.setDefaultTextEncodingName("utf-8");
        mapweb_view.addJavascriptInterface(new MapViewJSInterface(getContext(), this), "Android");
    
        mapweb_view.loadUrl("file:///android_asset/geomap.html");
    

    安卓代码:

     @JavascriptInterface
    public void getMapData() {
        getMapHitsAll();//custom method to load data to show on map
    
    }
    

    调用该方法初始化地图并设置数据

    getMapHitsAll() 将返回 'mapkey' 和所需的 'dataString' 以显示在 geochart 上,然后将调用 javascript 方法,如下所示:

     mapweb_view.loadUrl("javascript:loadGeoChart('" + mapkey + "', " + dataString + ")");
    

    【讨论】:

      【解决方案2】:

      所有平台支持

      您需要做的只是以编程方式创建“.html”文件并将 html 内容存储到该文件中,然后在 webview 中使用该文件的绝对地址打开同一个文件......就这么简单:)

      给你,开箱即用

      private void loadWorldMap() {
          try {
      
      
              String htmlDocument = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n" +
                      "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><meta http-equiv=\"Content-Type\" content=\"text/html,width=device-width,user-scalable=yes; charset=windows-1252\">\n" +
                      "    <script type=\"text/javascript\" src=\"https://www.google.com/jsapi\"></script>\n" +
                      "    <script type=\"text/javascript\">\n" +
                      "     google.load('visualization', '1', {'packages': ['geochart']});\n" +
                      "     google.setOnLoadCallback(drawRegionsMap);\n" +
                      "\n" +
                      "      function drawRegionsMap() {\n" +
                      "        var data = google.visualization.arrayToDataTable([\n" +
                      "          ['Country', ''],\n"
      
                      //add your city data buffer [List of countries] ;)
      
                      + build +
      
      
                      "        ]);\n" +
                      "\n" +
                      "        var options = {};\n" +
                      "\t\toptions = { \n" +
                      "                    datalessRegionColor: '#29ABE2',\n" +
                      "\t\t\t\t    backgroundColor: '#F3F3F3',\n" +
                      "\t\t\t\t\tcolorAxis: {colors: ['#0071BC']},\n" +
                      "                    keepAspectRatio: false,                    \n" +
                      "                    legend: false,\n" +
                      "                    tooltip: { textStyle: { color: '#0099CB', fontName: 'Arial', fontSize: '10'} }\n" +
                      "                };\n" +
                      "\t\t\n" +
                      "        var chart = new google.visualization.GeoChart(document.getElementById('chart_div'));\n" +
                      "        chart.draw(data, options);\n" +
                      "    };\n" +
                      "    </script>\n" +
                      "  </head>\n" +
                      "  <body>\n" +
                      "  <div id=\"chart_div\" style=\"width: 100%; height: 100%;\"><div style=\"position: relative;\"><div dir=\"ltr\" style=\"position: relative; width: 100%; height: 100%;\"><div style=\"position: absolute; left: 0px; top: 0px; width: 100%; height: 100%;\"></div></div></div></div>\n" +
                      "  \n" +
                      "</body></html>";
      
      
              // Creates a trace file in the primary external storage space of the
              // current application.
              // If the file does not exists, it is created.
      
              File traceFile = new File((this).getCacheDir(), "TraceFile.html");
              if (!traceFile.exists())
                  traceFile.createNewFile();
              // Adds a line to the trace file
              BufferedWriter writer = new BufferedWriter(new FileWriter(traceFile, false /*append*/));
              writer.write(htmlDocument);
              writer.close();
      
               
              MediaScannerConnection.scanFile(getApplicationContext(),
                      new String[]{traceFile.toString()},
                      null,
                      null);
              Log.d("file created", "Success.");
              Log.d("absolute path ", traceFile.getAbsolutePath());
          
      
              absoluteDir = traceFile.getAbsolutePath();
      
               // Load world map 
              mapView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
      
              mapView.getSettings().setJavaScriptEnabled(true);
              mapView.getSettings().setBuiltInZoomControls(false);
              mapView.getSettings().setUseWideViewPort(true);
              mapView.requestFocusFromTouch();
              mapView.requestFocus(View.FOCUS_DOWN);
              mapView.getSettings().setLoadWithOverviewMode(true);
      
              //webView.setWebViewClient(new WebViewClient());
              mapView.setWebChromeClient(new WebChromeClient() {
                  public void onProgressChanged(WebView view, int progress) {
                      llLoadingMap.setVisibility(View.VISIBLE);
                      if (progress == 100)
                          llLoadingMap.setVisibility(View.GONE);
                  }
              });
      
              mapView.loadUrl("file:///" + absoluteDir);
              mapView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
              int screenWidth = getWindowManager()
                      .getDefaultDisplay().getWidth();
              mapView.setLayoutParams(new LinearLayout.LayoutParams(
                      screenWidth, LinearLayout.LayoutParams.MATCH_PARENT, 1f));
      
      
          } catch (IOException e) {
              Log.e("error file", "Unable to write to the TraceFile.html file.");
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 2017-08-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-04
        • 1970-01-01
        • 2012-07-07
        • 1970-01-01
        相关资源
        最近更新 更多