天地图为什么不提供Android API了?我现在要做一个以天地图为底的Android项目,但是在天地图官网找不到Android SDK或者API,该怎么办?
具体代码已上传至gitee代码仓库
根据参考资料中的代码,可以发现这是百度地图 Android SDK 的示例代码,并非天地图的 Android SDK 或 API。因此,天地图官网没有提供针对 Android 的 SDK 或 API 是正常的。如果你需要使用天地图作为底图,可以考虑使用 Web API 或开源的地图库进行集成。具体步骤如下:
天地图提供了 Web API,可以直接在 Android 上使用。具体步骤如下:
(1)在布局文件中添加 Webview 控件:
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
(2)在 MainActivity 的 onCreate() 方法中,使用以下代码加载天地图:
WebView webView = findViewById(R.id.webview);
webView.loadUrl("http://t{s}.tianditu.com/DataServer?T=vec_w&X={x}&Y={y}&L={z}".replace("{s}", "0"));
其中,"http://t{s}.tianditu.com/DataServer?T=vec_w&X={x}&Y={y}&L={z}" 是天地图 Web API 的 URL,"{s}" 对应服务编号,"0" 表示服务编号为 0 的服务,"vec_w" 表示矢量底图(详细参数请参考天地图开发文档)。在代码中使用 replace() 方法将 "{s}" 替换成 "0"。
如果你想要自定义底图、覆盖物等,可以尝试使用基于 OpenStreetMap 的一些开源的 Android 地图库,比如 osmdroid、mapsforge 等。这里以 osmdroid 为例:
(1)在 build.gradle 文件中添加 osmdroid 依赖:
implementation 'org.osmdroid:osmdroid-android:6.1.0'
(2)在布局文件中添加 MapView 控件:
<org.osmdroid.views.MapView
android:id="@+id/mapview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
(3)在 MainActivity 中,使用以下代码初始化地图:
MapView mapView = findViewById(R.id.mapview);
mapView.setTileSource(new XYTileSource("TianDiTu", 0, 18, 256, ".png",
new String[]{"http://t0.tianditu.gov.cn/vec_w/",
"http://t1.tianditu.gov.cn/vec_w/",
"http://t2.tianditu.gov.cn/vec_w/",
"http://t3.tianditu.gov.cn/vec_w/",
"http://t4.tianditu.gov.cn/vec_w/"}));
mapView.setBuiltInZoomControls(true);
mapView.setMultiTouchControls(true);
其中,XYTileSource 表示瓦片地图源,"http://t0.tianditu.gov.cn/vec_w/" 等是天地图的瓦片地图地址。通过 setTileSource() 方法将瓦片地图源设置给 MapView,再通过 setBuiltInZoomControls() 和 setMultiTouchControls() 分别开启缩放控件和多点触控支持。
以上两种方式都不需要天地图的 Android SDK 或 API,仅需要调用天地图的 Web API 或使用开源的地图库即可实现。