CHAPTER 13 GPS 기반 구글맵 출력

1. 구글맵 APIKey 발급 받기

1) cmd 오픈 -> C:\Users\사용자 계정\.android 이동
2) keytool -v -list -alias androiddebugkey -keystore debug.keystore -storepass android -keypass android 입력 후 엔터
3) 출력된 내용 중 인증서 지문 부분의 MD5 복사
4) http://code.google.com/android/maps-api-signup.html 로 이동하여 MD5 인증서 지문을 입력 후 API키 발급

2. 사용 클래스

클래스설명
MapView지도를 출력하는 뷰
MapActivityMapView를 출력하기 위한 기본 클래스
GeoPoint위도와 경도를 나타내는 클래스
MapController지도를 확대, 축소하거나 상하좌우로 움직이는 클래스
LocationManager시스템의 위치 서비스에 접근을 허용함
LocationListener위치가 변경될 때 LocationManager로부터 통지를 받기 위해 사용됨

3. 실습

  • 구글맵 화면 띄우기
  • 내위치 표시
  • 줌 기능 추가
  • 나침반 표시

string.xml


<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">MapTest03</string>
    <string name="action_settings">Settings</string>

</resources>

main.xml


<?xml version="1.0" encoding="utf-8"?>
<com.google.android.maps.MapView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mapview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true"
    android:enabled="true"
    android:apiKey="0eE6ZcSEGyqtdLvHk1qpB_oWb5ve5rqrAe2bvgA"
    />

MainActivity.java


package com.example.maptest02;

import java.util.List;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;

public class MainActivity extends MapActivity implements LocationListener {
	
	MapView mapView;
	MapController mapCtrl;
	LocationManager lm;
	MyLocationOverlay myLocationOverlay;
	 
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		//main.xml의 MapView 클래스를 이용한 지도출력
		mapView = (MapView) findViewById(R.id.mapview);
		//줌컨트롤러 추가
		mapView.setBuiltInZoomControls(true);
		//위치 설정을 위한 컨트롤러 생성
		mapCtrl = mapView.getController();
		//지도 확대
		mapCtrl.setZoom(21);
		
		//현재 위치를 MapView에 출력하기 위한 MyLocationOverlay 클래스의 객체 생성
		myLocationOverlay = new MyLocationOverlay(this, mapView);
		//현재 위치 표시
		myLocationOverlay.enableMyLocation();
		//나침반 표시
		myLocationOverlay.enableCompass();
		//맵뷰로부터 얻어지는 overlay 리스트를 List 인터페이스의 객체에 할당
		List<Overlay> overlays = mapView.getOverlays();
		//overlay에 현재 위치를 추가함
		overlays.add(myLocationOverlay);
	}
	
	//현재 위치의 위도 경도 (위치가 변하면 자동 조정) 
	public void onLocationChanged(Location location){
		GeoPoint geopt = new GeoPoint(
				(int)(location.getLatitude()*1E6),
				(int)(location.getLongitude()*1E6)
				);
		mapCtrl.setCenter(geopt);
	}
	
	@Override
	protected boolean isRouteDisplayed(){
		return false;
	}
	
	protected boolean isLocationDisplayed(){
		return true;
	}
	//액티비티가 보여질 때 실행됨
	@Override
	public void onStart(){
		super.onStart();
		lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
	}
	
	public void OnResume() {
		super.onResume();
	}
	
	public void onPause(){
		super.onPause();
		
		myLocationOverlay.disableMyLocation();
		myLocationOverlay.disableCompass();
	}
	
	public void onStop(){
		super.onStop();
		lm.removeUpdates(this);
	}
	
	//LocationListener 클래스의 추상화 메소드로서 override 되어야 함
	@Override
	public void onProviderEnabled(String provider){	
	}
	
	//LocationListener 클래스의 추상화 메소드로서 override 되어야 함
	@Override
	public void onProviderDisabled(String provider){
	}
	
	//LocationListener 클래스의 추상화 메소드로서 override 되어야 함
	@Override
	public void onStatusChanged(String provider, int status, Bundle bd){
	}
}

AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.maptest02"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
         >
        
        <uses-library android:name="com.google.android.maps" />
        
        <activity
            android:name="com.example.maptest02.MainActivity"
            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>

</manifest>