안드로이드 개발 질문/답변
(글 수 45,052)
class MypositionOverlay extends Overlay {
private final int mRadius = 5;
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
Projection projection = mapView.getProjection();
if (shadow == false && location != null) {
// 현재 위치를 얻어옴
Double latitude = location.getLatitude()*1E6;
Double longitude = location.getLongitude()*1E6;
GeoPoint geoPoint;
geoPoint = new GeoPoint(latitude.intValue(), longitude.intValue());
// 현재 위치를 화면 픽셀로 변환함
Point point = new Point();
projection.toPixels(geoPoint, point);
RectF oval = new RectF(point.x - mRadius, point.y - mRadius,
point.x + mRadius, point.y + mRadius);
// 페인트를 설정함
Paint paint = new Paint();
paint.setARGB(250, 255, 255, 255);
paint.setAntiAlias(true);
paint.setFakeBoldText(true);
Paint backPaint = new Paint();
backPaint.setARGB(175, 50, 50, 50);
backPaint.setAntiAlias(true);
RectF backRect = new RectF(point.x + 2 + mRadius,
point.y - 3*mRadius,
point.x + 65,
point.y + mRadius);
// 마커를 그림
canvas.drawOval(oval, paint);
canvas.drawRoundRect(backRect, 5, 5, backPaint);
canvas.drawText("현재 위치", point.x + 2*mRadius, point.y, paint);
}
super.draw(canvas, mapView, shadow);
}
@Override
public boolean onTap(GeoPoint point, MapView mapView) {
return false;
}
Location location;
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
}
class Whereami extends MapActivity {
/** Called when the activity is first created. */
MapController mapController;
MypositionOverlay positionOverlay;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// MapView의 레퍼런스를 얻어옴
MapView myMapView = (MapView) findViewById(R.id.myMapView);
// MapView의 컨트롤러를 얻어옴
mapController = myMapView.getController();
// 지도 표시 옵션을 구성함
myMapView.setSatellite(true); // 위성 사진 레이어를 표시
myMapView.setStreetView(true); // 스트리트뷰 레이어를 표시
myMapView.displayZoomControls(false);
//myMapView.setBuiltInZoomControls(true); // 확대/축소 버튼 표시 여부를 지정
//myMapView.setTraffic(false); // 교통량 레이어를 표시
// 지도를 확대함
mapController.setZoom(17);
// MypositionOverlay를 추가함
positionOverlay = new MypositionOverlay();
List<Overlay> overlays = myMapView.getOverlays();
overlays.add(positionOverlay);
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(context);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
updateWithNewLocation(location);
// 2000 = 밀리초(2초), 10 = 미터
locationManager.requestLocationUpdates(provider, 2000, 10, locationListener);
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
public void onProviderDisabled(String provider) {
updateWithNewLocation(null);
}
public void onProviderEnabled(String provider) {
// text값 입력해보기
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}; private void updateWithNewLocation(Location location){
String latLongString;
TextView myLocationText;
myLocationText = (TextView) findViewById(R.id.myLocationText);
String addressString = "주소를 찾을 수 없습니다.";
if (location != null) {
// 내 위치 마커를 업데이트함
positionOverlay.setLocation(location);
// 지도 위치를 업데이트함
Double geoLat = location.getLatitude()*1E6;
Double geoLng = location.getLongitude()*1E6;
GeoPoint point = new GeoPoint(geoLat.intValue(),
geoLng.intValue());
mapController.animateTo(point);
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "위도;" + lat + "\n경도;" + lng;
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Geocoder gc = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = gc.getFromLocation(latitude, longitude, 1); // 주어진 위도, 경도의 지리 좌표로부터 Address 타입의 배열을 돌려줌
StringBuilder sb = new StringBuilder();
if (addresses.size() > 0) {
Address address = addresses.get(0);
for(int i = 0; i < address.getMaxAddressLineIndex(); i++)
sb.append(address.getAddressLine(i)).append("\n");
sb.append(address.getLocality()).append("\n"); // 시/도의 이름을 반환
sb.append(address.getPostalCode()).append("\n"); // 우편번호를 반환
sb.append(address.getCountryName()); // 국가 이름을 반환
}
addressString = sb.toString();
} catch (IOException e) {}
} else {
latLongString = "위치를 찾을 수 없습니다.";
}
myLocationText.setText("현재 위치 : \n" +
latLongString + "\n" + addressString);
}
}
GPS와 mapview를 이용하여 현재장소에서 GPS를키면 위도와경도가표시되고 구글맵에 현재위치가나타나는 프로젝트입니다.
소스코딩은 위와같구요.. 로그캣을 볼줄몰라서.. 도대체 무슨 오류인지 잘 모르겠네요 작은 답변이나마 도움이 될것같습니다.
로그캣내용은 첨부로 올리겠습니다.




올려주신 로그를 보았는데요, 중요한 로그의 내용은 2번째 라인인데, 이게 짤린 상태로 스크린 샷 찍어 올리셔서 무슨 에러인지 알 수가 없네요.. ㅠㅠ