본문 바로가기

개발이야기

Android Daum Map 띄우기

첫 포스트입니다. 

안드로이드를 오랜만에 다시 시작하며 DaumMap을 띄우려니 많이 해맸네요. 

다른분들께 도움이 됬으면 좋겠습니다.



다음 지도를 안드로이드에서 띄우기 위해서는 몇 가지 작업이 필요합니다.

먼저 정리 합시다.


1. 다음 개발자센터에서 API KEY 발급받기 

2. 다음 지도 라이브러리 파일 프로젝트에 추가하기

3. 메니페스트 파일에서 인터넷 접근권한 추가하기

이중 1. 다음 개발자센터에서 API KEY 발급받기는 내용은 타 블로그에도 많이 나와있고 어렵지않으므로 패스합니다.

제가 2번에서 많이 해매었기 때문에 자세히 설명 드립니다.


2. 다음 지도 라이브러리 파일 프로젝트에 추가하기

1) 안드로이드 스튜디오에서 프로젝트보기 방식으로 변환하여 app>src>main 폴더 밑에 jniLibs라고 디렉터리를 추가합니다.

2) 그 뒤, 다음개발자센터 에 접속 하여 SDK를 다운받습니다.

3) 압축을 풀면 lib폴더 안에 armeabi, armeabi-v7a 폴더가 있는데 이 폴더를 복사하여 1)에서 생성한 폴더안에 삽입합니다.

4)gradle 파일에서 해당 라이브러리 파일을 참조 할 수 있도록 수정해야합니다.

먼저 수정 대상 gradle파일은 이 빨간색 박스 안에 있는 파일입니다.

선택하면 아래 처럼 되어있을 텐데,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
apply plugin: 'com.android.application'
 
android {
    compileSdkVersion 24
    buildToolsVersion "24.0.0"
    defaultConfig {
        applicationId "test.hjkimproject3"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
cs


우린 추가한 라이브러리 파일을 사용 할 수 있게 아래 내용을 추가합니다.

1
2
3
4
5
6
7
8
9
10
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:24.2.1'
    testCompile 'junit:junit:4.12'
    compile files('libs/libDaumMapAndroid.jar')
}
 
cs

완성된 코드는 아래와 같습니다.

<build.gradle>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
apply plugin: 'com.android.application'
 
android {
    compileSdkVersion 24
    buildToolsVersion "24.0.0"
    defaultConfig {
        applicationId "test.hjkimproject3"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
 
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:24.2.1'
    testCompile 'junit:junit:4.12'
    compile files('libs/libDaumMapAndroid.jar')
}
 
cs


5) 이젠 안드로이드 스튜디오의 프로젝트 보기 방식을 Project로 변환하여 app>app>libs안에 다음지도 SDK압축파일에 있는 
libDaumMapAndroid.jar파일을 복사하여 넣어줍니다. (ProjectFiels로 보면 lib안에 내용이 없어, 폴더가 있으나 안보임)



6) 그 후 아래와 같이 라이브러리 파일을 오른쪽 버튼 클릭,


7)아래와 같은 창이 뜨면 OK를 누릅니다. 이젠 라이브러리 파일 추가는 완료되었습니다.


 


3. 메니페스트 파일에서 인터넷 접근권한 추가하기

인터넷 접근권한이 추가 되는 부분은 입니다. 

1
2
<uses-permission android:name="android.permission.INTERNET" />
 
cs

아래를 참고하여 적절한 위치에 삽입합니다.


<AndroidManifast.xml>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="test.hjkimproject3">
 
    <uses-permission android:name="android.permission.INTERNET" />
 
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
           <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
          </intent-filter></activity>
        </activity>
        <activity android:name=".MapActivity"></activity>
    </application>
 
</manifest>
 
cs



이제 준비는 모두 다 했습니다.

저는 Map이라는 Activity를 새로 생성했습니다. MainActivity에서의 이동은 타 블로그를 참고하시거나

아래 코드에서 Activity명만 Main 으로 사용하시면 됩니다.


먼저 MapActivity의 xml입니다.

특별한 내용은 없고, 기본 RelativeLayout 안에 새로운 RelativeLayout을 생성하여 id를 map_view로 할당하고 map_view가 화면에 가득 차게 하기 위해 부모의 padding을 0dp로 변경했습니다. (id가 map_view가 아니어도 실행되는지 자신이 없습니다.)


<activity_map.xml>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="0dp"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingTop="0dp"
    tools:context="hjkimproject3.test.esang.net.hjkimproject3.MapActivity">
    <RelativeLayout
        android:id="@+id/map_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">
 
    </RelativeLayout>
</RelativeLayout>
 
cs


아래는 java파일 입니다. 아래 java코드 까지 입력 하여 넣고 실행 하면 완료입니다. 


<소스코드>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ViewGroup;
 
import net.daum.mf.map.api.MapPoint;
import net.daum.mf.map.api.MapView;
 
public class MapActivity extends AppCompatActivity  {
    MapView mapView;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
 
        //MapView 
        mapView = new MapView(this);
        mapView.setDaumMapApiKey("APIKEY");
        ViewGroup mapViewContainer = (ViewGroup) findViewById(R.id.map_view);
        mapViewContainer.addView(mapView);
 
    }
}
 
cs