本博文是关于采用ArcGIS来构建Android地图
目录
在android studio上配置ArcGIS
将地图视图添加到布局中
在mapview中设置地图
License your Runtime app
在地图上添加一个图层
参考资料
(参考资料:https://developers.arcgis.com/android/latest/guide/develop-your-first-map-app.htm)
首先创建一个新的工程。
更新项目的Gradle存储库
allprojects {
repositories {
google()
jcenter()
//***ADD***
maven {
url 'https://esri.bintray.com/arcgis'
}
}
}
再配置APP下的build.gradle (Module: app)
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation project(path: ':opencv430')
// *** ADD ***
implementation 'com.esri.arcgisruntime:arcgis-android:100.8.0'
}
并且再android 模块中添加Java 8 compile options
android {
...
// *** ADD ***
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
成功了
更新android清单文件以允许网络访问,同时也表明应用程序使用opengl2.0或更高版本。
先修改layout文件如下:
默认情况下,所添加的Mapview不会显示任何东西,故此下一步就算定义一个地图来显示。
package com.example.arcgistest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.view.MapView;
public class MainActivity extends AppCompatActivity {
private MapView mMapView;
private void setupMap() {
if (mMapView != null) {
// ArcGISRuntimeEnvironment.setLicense(getResources().getString(R.string.arcgis_license_key));
Basemap.Type basemapType = Basemap.Type.STREETS_VECTOR;
double latitude = 34.0270;
double longitude = -118.8050;
int levelOfDetail = 13;
ArcGISMap map = new ArcGISMap(basemapType, latitude, longitude, levelOfDetail);
mMapView.setMap(map);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMapView = findViewById(R.id.mapView);
setupMap();
}
@Override
protected void onPause() {
if (mMapView != null) {
mMapView.pause();
}
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
if (mMapView != null) {
mMapView.resume();
}
}
@Override
protected void onDestroy() {
if (mMapView != null) {
mMapView.dispose();
}
super.onDestroy();
}
}
有几点细节需要注意的:
首先开始建立工程时,API需要是大于19的
其次,运行到手机时,可能会出现minSdkVersion版本号不对的问题,此时改一下即可。
最后,手机应该是需要连接网络才可以显示地图。
运行结果如下图所示:
从上图可以看到,有水印的存在,因此如果需要将这个功能开发的自己的app中需要授权
首先登录自己的account(https://developers.arcgis.com/dashboard)
复制自己的Runtime Lite license key
然后创建一个app_settings.xml 源文件
首先添加一个源文件。如下图所示
设置如下(除了名字都是默认)
然后添加代码
YOUR_LICENSE_KEY
添加下面模块为trailheads(跟踪头)数据创建特征图层,并且将其写入map中
package com.example.arcgistest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.data.ServiceFeatureTable;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.view.MapView;
public class MainActivity extends AppCompatActivity {
private MapView mMapView;
private void setupMap() {
if (mMapView != null) {
ArcGISRuntimeEnvironment.setLicense(getResources().getString(R.string.arcgis_license_key));//去除水印之类的
Basemap.Type basemapType = Basemap.Type.STREETS_VECTOR;
double latitude = 34.0270;
double longitude = -118.8050;
int levelOfDetail = 13;
ArcGISMap map = new ArcGISMap(basemapType, latitude, longitude, levelOfDetail);
mMapView.setMap(map);
}
}
//为trailheads(跟踪头)数据创建特征图层
private void addTrailheadsLayer() {
String url = "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0";
ServiceFeatureTable serviceFeatureTable = new ServiceFeatureTable(url);
FeatureLayer featureLayer = new FeatureLayer(serviceFeatureTable);
ArcGISMap map = mMapView.getMap();
map.getOperationalLayers().add(featureLayer);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMapView = findViewById(R.id.mapView);
setupMap();
addTrailheadsLayer();
}
@Override
protected void onPause() {
if (mMapView != null) {
mMapView.pause();
}
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
if (mMapView != null) {
mMapView.resume();
}
}
@Override
protected void onDestroy() {
if (mMapView != null) {
mMapView.dispose();
}
super.onDestroy();
}
}
The app should run and show features displayed on top of a basemap
https://developers.arcgis.com/labs/browse/?product=android&topic=any
https://blog.csdn.net/Gary__123456/article/details/64503424
https://www.cnblogs.com/gis-luq/p/4923508.html