android2012.10.11 20:34

* 현재 위치를 지도에 표시하기
1. 레이아웃 - my_map_view.xml 그대로 사용
2. 액티비티 - MyLocMap extends MapActivity implements LocationListener
:onCreate()에서 지도 초기화
1) 멤버변수 선언
LocationManager locationMgr = null;
MapController mapControl;
2) onCreate
setContentView(R.layout.my_map_view);
final MapView map = (MapView) findViewById(R.id.map);
mapControl = map.getController();

3. MyLocMap 액티비티가 실행되면 현재 위치 정보 가져오기
: onCreate()에서 현재 위치정보를 가져기 위한 requestLocationUpdates()함수 호출
locationMgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.NO_REQUIREMENT);
criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
String best = locationMgr.getBestProvider(criteria, true);
locationMgr.requestLocationUpdates(best, 1000, 0, MyLocationView.this);

4. 현재위치 정보 표시하기
: onLocationChanged()에서 GeoPoint 설정
double lat = location.getLatitude();
double lon = location.getLongitude();
GeoPoint newPoint = new GeoPoint((int)(lat * 1E6), (int)(lon*1E6));
mapControl.animateTo(newPoint);
mapControl.setZoom(15);
// 마커표시
MapView.LayoutParams mapMarkerParams = new MapView.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, newPoint,
MapView.LayoutParams.TOP_LEFT);
ImageView mapMarker = new ImageView(
getApplicationContext());
mapMarker.setImageResource(R.drawable.gmarker);
map.addView(mapMarker, mapMarkerParams);

///MyLocMap 액티비티 소스
package kcdi.map;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;

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 MyLocMap extends MapActivity implements LocationListener {
MapController mapControl;
MapView map;
LocationManager locationMgr = null;
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.my_map_view);
map = (MapView) findViewById(R.id.map);
mapControl = map.getController();
locationMgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.NO_REQUIREMENT);
criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
String best = locationMgr.getBestProvider(criteria, true);
locationMgr.requestLocationUpdates(best, 1000, 0, MyLocMap.this);
}
@Override
public void onLocationChanged(Location location) {
double lat = location.getLatitude();
double lon = location.getLongitude();
GeoPoint newPoint = new GeoPoint((int)(lat * 1E6), (int)(lon*1E6));
mapControl.animateTo(newPoint);
mapControl.setZoom(15);
MapView.LayoutParams mapMarkerParams = new MapView.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, newPoint,
MapView.LayoutParams.TOP_LEFT);
ImageView mapMarker = new ImageView(getApplicationContext());
mapMarker.setImageResource(R.drawable.gmarker);
map.addView(mapMarker, mapMarkerParams);
}

@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub

}

}

신고
Posted by 레드오이

티스토리 툴바