Android WebView loading GoogleMap Data Rendering

眉间皱痕 提交于 2019-12-13 15:26:10

问题


I'm trying to display the div inside the WebView using the follwoing function

String baseUrl = "";
String mimeType = "text/html";
String encoding = "UTF-8";
html = sb.toString();
String historyUrl = "";

webViewDataViewer.loadDataWithBaseURL(baseUrl, html, mimeType, encoding, historyUrl);

here is my html :

 "<html> "+
    " <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />" +
    " <head> <body>  " +
    " <script type='text/javascript' src='http://maps.googleapis.com/maps/api/js?sensor=true' ></script> " +
    " <script type='text/javascript'>" +
    " <div id='map_canvas' style='width:100%;height:100%;'></div>" +
    "  var locations = [ ['<h3>A1 A2</h3><img src='assets/user_26/thumb_0.jpg' alt='img' /><p>S, I</p><p>G</p><p>G, 4343, India</p>amit', 28.6392112, 77.3496616], ['<h3>F</h3><img src='' alt='img' /><p>Test</p><p>Test</p><p>Test, 355441, India</p>amit', 19.5851331, -99.0572702]," +
    " ['<h3>B1</h3><img src='' alt='img' /><p>Test</p><p>Test</p><p>Test, 45665, India</p>am', 19.5851331, -99.0572702]," +
    " ['<h3>C1</h3><img src='assets/user_34/thumb_userImage.png' alt='img' /><p>2134 Lawton st</p><p>San Francisco, 94122, United States</p>amit', 37.757809, -122.485946]," +
    " ['<h3>D1</h3><img src='' alt='img' /><p>La</p><p>La</p><p>Abc, 110031, </p>amit', 30.0041868, -90.2159207]," +
    " ['<h3>E1</h3><img src='assets/user_36/thumb_0.jpg' alt='img' /><p>US</p><p>US, 123456, United States</p>amit', 34.797063, -82.294022]," +
    " ['<h3>G1</h3><img src='assets/user_38/thumb_0.jpg' alt='img' /><p>1 Market St</p><p>San Francisco, 94123, United States</p>amit', 37.7944988, -122.3947446]," +
    " ['<h3>T0</h3><img src='' alt='img' /><p>Sfvf</p><p>Fgf</p><p>Fsv, 33133, India</p>amit', 25.7307897, -80.2377078], " +
    " ['<h3>GT7</h3><img src='assets/user_45/thumb_0.jpg' alt='img' /><p>Newyork</p><p>Us</p><p>Hju, 4556, India</p>amit', 40.760118, -73.830034]," +
    " ['<h3>R2</h3><img src='' alt='img' /><p>Asdfhjkkl</p><p>San Fran, 94104, United States</p>amit', 37.7911148, -122.4021273],];" +
    "  var count = 0; "+
    "  var total_lng = 0;  var total_lat = 0; " +" total_lat += 28.6392112;"+
                                           " total_lng += 77.3496616;"+
                                            "count += 1; "+                                
                                           " total_lat += 19.5851331;"+
                                           " total_lng += -99.0572702;"+
                                           " count += 1; " +                             
                                           "total_lat += 19.5851331;"+
                                           "total_lng += -99.0572702;"+
                                           "count += 1;"+                                       
                                           "total_lat += 37.757809;"+
                                           "total_lng += -122.485946;"+
                                           "count += 1;"  +                                    
                                           "total_lat += 30.0041868;"+
                                           "total_lng += -90.2159207;"+
                                          " count += 1;   total_lat += 34.797063;    total_lng += -82.294022;"+ 
                                         "  count += 1;             total_lat += 37.7944988;  total_lng += -122.3947446;   "+
                                          " count += 1; total_lat += 25.7307897;     total_lng += -80.2377078;" +                           
                                           " count += 1; total_lat += 40.760118;    total_lng += -73.830034;"+                             
                                           " count += 1;   total_lat += 37.7911148;  total_lng += -122.4021273;"+                                    
                                           " count += 1;   "+                                     

                           " if(count) {"+
                           "     avg_lat = total_lat / count;"+
                           "     avg_lng = total_lng / count;  "+                          
                           " } else {"+
                           "     avg_lat = 40;"+
                           "     avg_lng = -96;"+
                           " }"+

                        "var mapOptions = "+
                       "{"+
                         "   center: new google.maps.LatLng(avg_lat, avg_lng), "+
                          "  zoom: 4,"+
                        "    scrollwheel: false,"+
                        "    streetViewControl: false,"+

                      "      mapTypeId: google.maps.MapTypeId.ROADMAP,"+
                        "    navigationControlOptions: {"+
                       "         style: google.maps.NavigationControlStyle.SMALL"+

                          "}"+
                        "};"+

                       " var map = new google.maps.Map(document.getElementById('map_canvas'),  mapOptions);"+
                      "  var infowindow = new google.maps.InfoWindow();"+
                      "  var marker, i;"+
                      "  if(locations.length > 0) {"+
                         "   for (i = 0; i < locations.length; i++) "+
                           " {   "+                   
                          "      marker = new google.maps.Marker({"+
                             "       position: new google.maps.LatLng(locations[i][1], locations[i][2]),"+
                              "      map: map"+
                             "   });"+

                               " google.maps.event.addListener(marker, 'click', (function(marker, i) {"+
                                "    return function() {"+
                                   "     infowindow.setContent(locations[i][0]);"+
                                     "   infowindow.open(map, marker);"+
                                    "}"+
                               " })(marker, i));     "+           
                            "}   "+
                        "} </script>    "
           "  </body> </head>  </html>"

this is displaying correctly in Android Browser but in Android WebView no data is displayed.i'm not able to figure out what is wrong here.


回答1:


Try like that:

String html= "<html><body>You scored <b>192</b> points.</body></html>";
webview.loadData(html, "text/html", null);

Of corse, replace the html example i gave with your html.




回答2:


String html = " ...  your html here ..."
webView = (WebView) findViewById(R.id.your_webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadData(html,"text/html", "UTF-8");

Have you enabled javascript on your webview ?



来源:https://stackoverflow.com/questions/14213614/android-webview-loading-googlemap-data-rendering

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!