我正在尝试使用此示例代码在Android Studio中离线查看地图。首先我得到错误“无法解析符号'osmdroid'”。我导入了一个外部库来解决错误,但后来我得到错误“无法解析符号'GeoPoint'”和“无法解析符号'MapView'”。是否有其他外部库或我可以使用此代码进行一些更改?
package com.example.hppavilion15.osmoffline1;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import android.app.Activity;
import android.os.Bundle;
public class OSMDroidMapActivity extends Activity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
MapView mapView = new MapView(this, 256); //constructor
mapView.setClickable(true);
mapView.setBuiltInZoomControls(true);
setContentView(mapView); //displaying the MapView
mapView.getController().setZoom(15); //set initial zoom-level, depends on your need
mapView.getController().setCenter(new GeoPoint(52.221, 6.893)); //This point is in Enschede, Netherlands. You should select a point in your map or get it from user's location.
mapView.setUseDataConnection(false); //keeps the mapView from loading online tiles using network connection.
}
}
我的build.gradle文件:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
其他gradle文件:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.0"
defaultConfig {
applicationId "com.example.hppavilion15.osmoffline1"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.0'
}
主要\ AndroidManifest.xml中:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hppavilion15.osmoffline1">
<application android:allowBackup="true" android:label="@string/app_name"
android:icon="@mipmap/ic_launcher" android:theme="@style/AppTheme">
<activity
android:name="com.example.hppavilion15.osmoffline1.OSMDroidMapActivity"
android:nohistory="false" android:theme="@android:style/Theme.NoTitleBar"
android:launchmode="singleTask" android:hardwareaccelerated="false"
android:configchanges="keyboardHidden|orientation">
</activity>
</application>
调试\ AndroidManifest.xml中:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hppavilion15.osmoffline1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="23" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.hppavilion15.osmoffline1.OSMDroidMapActivity"
android:configchanges="keyboardHidden|orientation"
android:hardwareaccelerated="false"
android:launchmode="singleTask"
android:nohistory="false"
android:theme="@android:style/Theme.NoTitleBar" >
</activity>
</application>
Gradle构建消息:
错误:(16)在'android'包中找不到属性'configchanges'的资源标识符
错误:(16)在'android'包中找不到属性'hardwareaccelerated'的资源标识符
错误:(16)在'android'包中找不到属性'hardwareaccelerated'的资源标识符
错误:(16)在'android'包中找不到属性'launchmode'的资源标识符 错误:(16)在'android'包中找不到属性'nohistory'的资源标识符
错误:任务':app:processDebugResources'的执行失败。 com.android.ide.common.process.ProcessException:org.gradle.process.internal.ExecException:处理'命令'C:\ Users \ HP PAVILION 15 \ AppData \ Local \ Android \ sdk \ build-tools \ 23.0.0 \ aapt.exe''以非零退出值1结束 信息:建筑失败 信息:总时间:1.754秒 信息:5个错误 信息:0警告 信息:请参阅控制台中的完整输出
答案 0 :(得分:0)
由于您使用的是osmdroid,因此您需要将osmdroid添加为应用程序模块gradle文件(第二个)的依赖项。有关详细信息,请参阅https://github.com/osmdroid/osmdroid/wiki/HowToMaven的自述文件
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'org.osmdroid:osmdroid-android:4.3'
}
应该让你去。
编辑:这是一个示例清单。 source 尝试将这些添加到您的清单中。
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.location.network" android:required="false" />
<uses-feature android:name="android.hardware.location.gps" android:required="false" />
<uses-feature android:name="android.hardware.telephony" android:required="false" />
<uses-feature android:name="android.hardware.wifi" android:required="false" />