步骤
- 在 Android 项目中添加 WebView
- 创建一个 HTML 文件来加载 Leaflet
- 在
MapActivity
中加载该 HTML 文件
1. 在 Android 项目中添加 WebView
创建你的 activity_map.xml
文件并添加一个 WebView:
<!-- res/layout/activity_map.xml -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
2. 创建一个 HTML 文件来加载 Leaflet
在 assets
文件夹中创建一个 map.html
文件。确保在 app/src/main/assets
路径下创建 assets
文件夹。下面是示例代码:
<!DOCTYPE html>
<html>
<head>
<title>Leaflet Map</title>
<meta name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<link rel="stylesheet" href="leaflet.css" />
<style>
body {
padding: 0;
margin: 0;
}
html, body, #map {
height: 100%;
width: 100vw;
}
</style>
</head>
<body>
<div id="map"></div>
<script src="leaflet.js" ></script>
<script src="leaflet.ChineseTmsProviders.js"></script>
<script>
var map = L.map('map').fitWorld