안드로이드 MapView API 사용에 대해 가이드 형식으로 소개한다.
- Eclipse IDE를 이용한 Android 환경설정이 완료되었음을 가정한다.
프로젝트 생성하기
이클립스에서 Android 프로젝트를 생성한다.
Project name, Package name, Activity name, Application name 을 아래와 같이 입력하고 Finish를 클릭한다.
안드로이드의 기본적인 어플리케이션 프레임이 생성되었다.
AndroidManifest.xml 수정
먼저 Application의 기본정보를 포함하고 있는 AndroidManifest.xml을 수정해 보자.
AndroidManifest.xml를 더블클릭하면 다음과 같은 editor가 표시된다.
여기서 하단의 AndroidManifest.xml 탭을 선택하고 다음과 같이 수정한다.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.mapview"
android:versionCode="1"
android:versionName="1.0.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<uses-library android:name="com.google.android.maps" />
<activity android:name=".MapViewTest"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
<uses-library android:name="com.google.android.maps" /> 는 com.google.android.maps api를 사용하겠다는 선언인것 같다.
그리고 <uses-permission android:name="android.permission.INTERNET" /> 은 개인적으로 애먹은 부분인데.. 이 어플리케이션이 사용할 수 있는 리소스에 대한 권한을 설정해 주는 부분이다. MapView는 Google Maps서비스를 인터넷을 통해 사용하고 있으므로 INTERNET 퍼미션을 제공하여야 한다.
mapview.xml 생성하기
com.test.mapview프로젝트에서 res/layout 디렉토리에 mapview.xml 을 생성한다.
생성된 mapview.xml 에 아래와 같이 코드한다.
- <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.maps.MapView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="Please insert your API_KEY"
/>
</LinearLayout>
여기서 android:apiKey 는 링크 에 표시된 대로 진행하여 등록하여야 한다. 만약 apikey가 정상적이지 않으면 어플리케이션은 실행되지만 지도가 표시되지 않는다.
여기서 mapview.xml 을 생성하면 builder에 의해서 소스디렉토리의 R.java 파일에 자동으로 Static 변수선언이 등록된다.
- /* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/
package com.test.mapview;
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int icon=0x7f020000;
}
public static final class id {
public static final int main=0x7f050000;
}
public static final class layout {
public static final int main=0x7f030000;
public static final int mapview=0x7f030001;
}
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
}
}
Activity 수정하기
프로젝트 생성시 입력한 Activity(안드로이드에서 View를 담당한다.)의 이름이 MapViewTest였다. Eclipse의 Package Explorer에서 com.test.mapview.MapViewTest.java 가 생성되어 있음을 확인할 수 있다. 이 파일을 더블클릭하여 오픈한다. 아래와 같이 수정한다. 수정해야 할 부분은 굵은 폰트로 표시하였다.
- package com.test.mapview;
import android.os.Bundle;
import com.google.android.maps.MapActivity;
public class MapViewTest extends MapActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mapview);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
에뮬레이터에서 실행
이제 MapActivity를 이용해서 기본적인 MapView 를 모두 작성하였다. 결과를 확인하기 위하여 에뮬레이터에서 실행해 보자.
음... 잘 나온다.. 첫작업이라 시행착오가 많았지만 그럭저럭 잘 진행된 것 같다. 다음에는 MapView API를 직접 사용하여 java 코드로 구현해 보아야 겠다.


댓글