首页 > 解决方案 > 使用android更改html代码的值

问题描述

我有一个应用程序,它通过使用以下 WebView 并从我的资产文件夹中读取 HTML 文件来显示地图,如下所示:

WebView myWebView = (WebView) findViewById(R.id.map);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("file:///android_asset/test.html");

HTML文件是:

<!DOCTYPE html>
<html>
<head>

    <title>Quick Start - Leaflet</title>

    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <link rel="shortcut icon" type="image/x-icon" href="docs/images/favicon.ico" />

    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.4.0/dist/leaflet.css" integrity="sha512-puBpdR0798OZvTTbP4A8Ix/l+A4dHDD0DGqYW6RQ+9jxkRFclaxxQb/SJAWZfWAkuyeQUytO7+7N4QKrDh+drA==" crossorigin=""/>
    <script src="https://unpkg.com/leaflet@1.4.0/dist/leaflet.js" integrity="sha512-QVftwZFqvtRNi0ZyCtsznlKSWOStnDORoefr1enyq5mVL4tmKB3S/EnC3rRJcxCPavG10IcrVGSmPh6Qw5lwrg==" crossorigin=""></script>



</head>
<body>



<div id="mapid" style="width: 600px; height: 400px;"></div>
<script>

var mymap = L.map('mapid').setView([Lat_Coordinate, Lon_Coordinate], 13);

    L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
    maxZoom: 18,
    attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, ' +
    '<a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
    'Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
    id: 'mapbox.streets'
    }).addTo(mymap);

L.marker([Lat_Coordinate, Lon_Coordinate]).addTo(mymap)
    .bindPopup("<b>My Location</b>").openPopup();

L.circle([Lat_Coordinate, Lon_Coordinate], 5000, {
    color: 'red',
    fillColor: '#f03',
    fillOpacity: 0.5
}).addTo(mymap);


var popup = L.popup();

</script>

</body>
</html>

现在,我有另一个代码使用谷歌服务来获取用户的Lat坐标Lon。我得到的坐标是通过使用谷歌服务:

Latitude_Coor = location.getLatitude();
Longitude_Coor = location.getLongitude();

现在,一旦我在我的 java 代码中获得了它们,我想将它们作为值插入到HTML上面的代码中[Lat_Coordinate, Lon_Coordinate]

如何将变量传递给我assets文件夹中的 HTML?

谢谢!

标签: androidhtmllocation

解决方案


推荐阅读