Search code examples
androidandroid-studioosmdroidgeopoints

Android studio cannot resolve GeoPoint and MapView


I was trying this example code to view maps offline in Android Studio. First I got the error "Cannot resolve symbol 'osmdroid'". I imported an external library which solved the error but then i got the errors "Cannot resolve symbol 'GeoPoint'" and "Cannot resolve symbol 'MapView'". Is there another external library for this or can i use this code with some changes?

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.

    }

}

my build.gradle file:

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()
   }
}

other gradle file:

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'
}

main\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>

debug\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 build messages:

Error:(16) No resource identifier found for attribute 'configchanges' in package 'android'

Error:(16) No resource identifier found for attribute 'hardwareaccelerated' in package 'android'

Error:(16) No resource identifier found for attribute 'hardwareaccelerated' in package 'android'

Error:(16) No resource identifier found for attribute 'launchmode' in package 'android' Error:(16) No resource identifier found for attribute 'nohistory' in package 'android'

Error:Execution failed for task ':app:processDebugResources'. com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Users\HP PAVILION 15\AppData\Local\Android\sdk\build-tools\23.0.0\aapt.exe'' finished with non-zero exit value 1 Information:BUILD FAILED Information:Total time: 1.754 secs Information:5 errors Information:0 warnings Information:See complete output in console


Solution

  • Since you're using osmdroid, you'll need to add osmdroid as a dependency to the application module gradle file (the second one). See the readme at https://github.com/osmdroid/osmdroid/wiki/HowToMaven for more info

    dependencies {
     compile fileTree(dir: 'libs', include: ['*.jar'])
     compile 'com.android.support:appcompat-v7:23.0.0'
     compile 'org.osmdroid:osmdroid-android:4.3'
    }
    

    that should get you going.

    edit: here's an example manifest. source Try adding these to your manifest.

    <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" />