안드로이드 개발 질문/답변
(글 수 45,052)
--------------------------------------------Overlay.java-------------------------------------------
package com.Overlay;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
public class Overlay extends MapActivity implements LocationListener {
LocationManager location = null;
boolean bGetteringGPS = false; //GPS 한번만 받아오도록 제어
private LocationManager locationManager = null;
private MapView mapView = null;
private MapController mapController;
private GeoPoint centerGP = null;
private double currentLat = 0;
private double currentLng = 0;
private Button btnConfirm = null;
private Context context = null;
private EditText editAddress = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.context = this;
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Iterator<String> providers = locationManager.getAllProviders().iterator();
// GPS 정보를 얻기위한 프로바이더 검색
while(providers.hasNext()) {
Log.d("Test", "provider " + providers.next());
}
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.NO_REQUIREMENT);
criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
String best = locationManager.getBestProvider(criteria, true);
locationManager.requestLocationUpdates(best, 1000, 0, this);
setContentView(R.layout.map_location_overlay);
registerEventListener();
}
private void registerEventListener() {
mapView = (MapView) findViewById(R.id.map_view);
mapController = mapView.getController();
editAddress = (EditText)findViewById(R.id.edit_address);
btnConfirm = (Button) findViewById(R.id.btn_confirm);
btnConfirm.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("lat", currentLat);
intent.putExtra("lng", currentLng);
Geocoder geocoder = new Geocoder(context);
List<Address> address;
try {
address = geocoder.getFromLocation(currentLat, currentLng, 1);
editAddress.setText(address.get(0).getAddressLine(0).toString() + "\nlat , lng : " + currentLat + " " + currentLng );
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
/**
* 지도 노출
*/
private void setMap() {
centerGP = new GeoPoint(
(int) (currentLat * 1E6),
(int) (currentLng * 1E6)
);
mapView.setBuiltInZoomControls(true);
mapView.setClickable(true);
mapView.setStreetView(true);
mapController.setZoom(19);
mapController.setCenter(centerGP);
mapView.invalidate();
placeMarker((int)(currentLat * 1E6), (int)(currentLng * 1E6));
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
@Override
public void onLocationChanged(Location location) {
if(bGetteringGPS == false) {
currentLat = location.getLatitude();
currentLng = location.getLongitude();
Log.d("Test", "location " + currentLat + " " + currentLng);
locationManager.removeUpdates(this);
bGetteringGPS = true;
setMap();
}
}
@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
}
/**
* 마커를 지도에 위치
*
* @param markerLatitude
* @param markerLongitude
*/
private void placeMarker(int markerLatitude, int markerLongitude)
{
Drawable marker=getResources().getDrawable(android.R.drawable.ic_menu_myplaces);
//Drawable marker=getResources().getDrawable(R.drawable.marker);
marker.setBounds(0, 0, marker.getIntrinsicWidth(),
marker.getIntrinsicHeight());
mapView.getOverlays().add(new InterestingLocations(marker,
markerLatitude, markerLongitude));
}
/**
* 센터로 좌표 이동
*
* @param centerGeoPoint
*/
private void CenterLocation(GeoPoint centerGeoPoint)
{
mapController.animateTo(centerGeoPoint);
placeMarker(centerGeoPoint.getLatitudeE6(),
centerGeoPoint.getLongitudeE6());
currentLat = (double)centerGeoPoint.getLatitudeE6()/1000000;
currentLng = (double)centerGeoPoint.getLongitudeE6()/1000000;
};
/**
* 지도위에 레이어 올리는 클래스
*/
class InterestingLocations extends ItemizedOverlay<OverlayItem>{
private List<OverlayItem> locations =
new ArrayList<OverlayItem>();
private Drawable marker;
private OverlayItem myOverlayItem;
public InterestingLocations(Drawable defaultMarker,
int LatitudeE6, int LongitudeE6) {
super(defaultMarker);
// TODO Auto-generated constructor stub
this.marker=defaultMarker;
// create locations of interest
GeoPoint myPlace = new GeoPoint(LatitudeE6,LongitudeE6);
myOverlayItem = new OverlayItem(myPlace, "My Place", "My Place");
locations.add(myOverlayItem);
populate();
}
@Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return locations.get(i);
}
@Override
public int size() {
// TODO Auto-generated method stub
return locations.size();
}
@Override
public void draw(Canvas canvas, MapView mapView,
boolean shadow) {
// TODO Auto-generated method stub
super.draw(canvas, mapView, shadow);
boundCenterBottom(marker);
}
/**
* onTouchListener 로는 touch up, down, move 가 제대로 구현 안된다. bug...
*/
@Override
public boolean onTap(GeoPoint p, MapView mapView) {
// TODO Auto-generated method stub
mapView.getOverlays().remove(0);
CenterLocation(p);
return true;
}
}
}
------------------------------------------------main.xml------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.google.android.maps.MapView
android:id="@+id/map_view"
android:layout_width="fill_parent"
android:layout_height="600px"
android:apiKey="?"
android:layout_gravity="center"
/>
<EditText
android:id="@+id/edit_address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btn_confirm"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="주소지정"
/>
</LinearLayout>
-----------------------------------------------------------------------------------------------
안드로이드에 대한 공부를 시작한지 얼마 안된 학생입니다.
추가해 놓고 보면 문제가 전혀 없는것 같은데
실행을 하면 "예기치 않게 종료됩니다" 라고 떠서 골머리를 썩고 있습니다
답변 부탁드립니다.
2010.12.06 12:31:50
[2010-12-06 12:29:54 - Overlay] ------------------------------
[2010-12-06 12:29:54 - Overlay] Android Launch!
[2010-12-06 12:29:54 - Overlay] adb is running normally.
[2010-12-06 12:29:54 - Overlay] Performing com.Overlay.Overlay activity launch
[2010-12-06 12:29:54 - Overlay] Automatic Target Mode: launching new emulator with compatible AVD 'GoogleMap'
[2010-12-06 12:29:54 - Overlay] Launching a new emulator with Virtual Device 'GoogleMap'
[2010-12-06 12:29:57 - Overlay] New emulator found: emulator-5554
[2010-12-06 12:29:57 - Overlay] Waiting for HOME ('android.process.acore') to be launched...
[2010-12-06 12:30:27 - Overlay] WARNING: Application does not specify an API level requirement!
[2010-12-06 12:30:27 - Overlay] Device API version is 7 (Android 2.1)
[2010-12-06 12:30:27 - Overlay] HOME is up on device 'emulator-5554'
[2010-12-06 12:30:27 - Overlay] Uploading Overlay.apk onto device 'emulator-5554'
[2010-12-06 12:30:27 - Overlay] Installing Overlay.apk...
[2010-12-06 12:30:47 - Overlay] Success!
[2010-12-06 12:30:47 - Overlay] Starting activity com.Overlay.Overlay on device emulator-5554
실행후 로그입니다




로그를 확인해보시고.. 로그를 올려주시면 도움주기 편할 것 같네요