2013년 7월 18일 목요일

[Android] GPS를 이용해 현재 위치 지도에서 보여주기(심화)

1. 사전 준비과정 및 지식

1.1 맵뷰와 맵액티비티

- 애플리케이션에 지도를 넣기 위해서는 화면 영역을 할당 받을 수 있도록 맵뷰(MapView)사용한다.
- 맵뷰를 사용할 때는 맵액티비티(MapActivity)를 같이 사용한다.

1.2 지도 API 키

- 구글맵을 사용할 때는 지도 API키를 발급받아 등록해야 구글서버에서 지도 데이터를 받을 수 있다.
- 지도키 받는 절차 링크 : http://ilililililililililili.blogspot.kr/2013/07/android-googlemap-api.html

1.3 구글 라이브러리의 사용

- 구글맵은 Google API가 포함된 플랫폼으로 에뮬레이터를 실행해야 한다.

1.4 매니페스트 수정

-지도를 추가하게 되면 크게 두 가지 권한(인터넷 접속 권한, GPS 위치정보 확인 권한)이 필요하다.
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
-구글 라이브러리를 사용할때는 application 태그 안에 아래와같은 태그를 넣어주어야한다
    <uses-library android:name="com.google.android.maps"/>

1.5 레이아웃에 맵뷰 추가하기

-xml 코드
    <com.google.android.maps.MapView
        android:id="@+id/mapview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/scrollview"
        android:layout_below="@+id/button01"
        android:clickable="true"
        android:apikey="xxx" />
2. 예제
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

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

public class MyLocationMapActivity extends MapActivity {

 TextView text01;
 LocationManager manager;
 MapView mapview;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  
  text01 = (TextView) findViewById(R.id.text01);
  mapview = (MapView) findViewById(R.id.mapview);
  
  // 줌인 줌아웃 버튼이 나오게 하는 설정
  mapview.setBuiltInZoomControls(true);
  
  Button button01 = (Button) findViewById(R.id.button01);
  button01.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    // LocationManager 객체 초기화 , LocationListener 리스너 설정
    getMyLocation();
   }
  });
  

 }

 // LocationManager 객체 초기화 , LocationListener 리스너 설정
 private void getMyLocation() {
  if (manager == null) {
   manager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
  }
  // provider 기지국||GPS 를 통해서 받을건지 알려주는 Stirng 변수
  // minTime 최소한 얼마만의 시간이 흐른후 위치정보를 받을건지 시간간격을 설정 설정하는 변수
  // minDistance 얼마만의 거리가 떨어지면 위치정보를 받을건지 설정하는 변수
  // manager.requestLocationUpdates(provider, minTime, minDistance, listener);

  // 10초
  long minTime = 10000;
  
  // 거리는 0으로 설정 
  // 그래서 시간과 거리 변수만 보면 움직이지않고 10초뒤에 다시 위치정보를 받는다
  float minDistance = 0;

  MyLocationListener listener = new MyLocationListener();

  manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, minDistance, listener);

  appendText("내 위치를 요청 했습니다.");
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.my_location, menu);
  return true;
 }

 private void appendText(String msg) {
  text01.append(msg + "\n");
 }

 class MyLocationListener implements LocationListener {

  // 위치정보는 아래 메서드를 통해서 전달된다.
  @Override
  public void onLocationChanged(Location location) {
   appendText("onLocationChanged()가 호출되었습니다");

   double latitude = location.getLatitude();
   double longitude = location.getLongitude();

   appendText("현재 위치:" + latitude + "," + longitude);
   
   showMyLocation(latitude,longitude);
  }

  @Override
  public void onProviderDisabled(String provider) {

  }

  @Override
  public void onProviderEnabled(String provider) {

  }

  @Override
  public void onStatusChanged(String provider, int status, Bundle extras) {

  }

 }
 
 private void showMyLocation(double latitude,double longitude){
  int intlatitude = new Double(latitude).intValue();
  int intlongitude = new Double(longitude).intValue();
  
  // GeoPoint - 경도와 위도를 한점으로 표시하는 객체
  GeoPoint myPoint = new GeoPoint(intlatitude, intlongitude);
  
  // 맵뷰의 속성등을 컨트롤하는 클래스(맵뷰 어뎁터) 
  MapController controller = mapview.getController();
  
  // 애니메이션 효과가 나면서 점으로 이동
  controller.animateTo(myPoint);
  
  // 줌에는 단계가 있는데 정수값으로 표기한다
  controller.setZoom(17);
  
 }

 @Override
 protected boolean isRouteDisplayed() {
  // TODO Auto-generated method stub
  return false;
 }
}

댓글 1개: