[안드로이드,Android] map 주소 가저오기

2013. 1. 14. 17:30Mobile/Android



반응형

 

 

안드로이드 지오코딩 주소가저오기

 

Map01Activity.java

package kr.co.test.map01;

import java.io.IOException;
import java.util.List;
import java.util.Locale;


import android.content.Intent;
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.net.Uri;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;s
import android.view.View.OnClickListener;
import android.widget.EditText;
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.Overlay;
import com.google.android.maps.OverlayItem;

public class Map01Activity extends MapActivity {
	/** Called when the activity is first created. */

	Location location;
	EditText editTextLat, editTexLng, editTextAddress;overlay
	Geocoder coder;
	MapView mapview;
	MapController controller;

	// 색상 핀꽂기
	MyItemizedOverlay itemizedOverlay;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		mapview = (MapView) findViewById(R.id.mapView);

		controller = mapview.getController();

		// 이상적인사이즈 14~16
		controller.setZoom(16);

		MapOverlay mapOverlay = new MapOverlay();
		List <Overlay> listOfOverlays = mapview.getOverlays();
		listOfOverlays.add(mapOverlay);

		// 생상 핀 꽂기 ******************

		Drawable drawable = getResources().getDrawable(R.drawable.marker);
		itemizedOverlay = new MyItemizedOverlay(drawable);
		listOfOverlays.add(itemizedOverlay);
		// *************************

		coder = new Geocoder(this, Locale.KOREA);
		editTextLat = (EditText) findViewById(R.id.EditTextLat);
		editTexLng = (EditText) findViewById(R.id.EditTextLng);
		editTextAddress = (EditText) findViewById(R.id.EditTextAddress);

		// 위치정보를 취득하기 위한 매니져
		LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.NO_REQUIREMENT);
		criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
		String provider = locationManager.getBestProvider(criteria, true);

		location = locationManager.getLastKnownLocation(provider);
		updateLocation(location.getLatitude(), location.getLongitude());

		locationManager.requestLocationUpdates(provider, 2000, 10,
				mLocationListener);


		// &sll=36.430122 , 128.056641 위도 :경도 점을 없앤다
		/*
		 * GeoPoint point = new GeoPoint(37579617, 126977041);
		 * mControl.setCenter(point);
		 * 
		 * 
		 * mMap.setBuiltInZoomControls(true); //줌사진
		 * 
		 * mMap.setSatellite(true); //위성사진
		 */
	}

	private void updateLocation(double lat, double lng) {
		String sLocationInfo = "";
		if (location != null) {
			editTextLat.setText(String.valueOf(lat));
			editTexLng.setText(String.valueOf(lng));

			GeoPoint point = new GeoPoint((int) (lat * 1E6), (int) (lng * 1e6));
			controller.animateTo(point);

			// ********색상 핀 꽂기***********
			OverlayItem overlayItem = new OverlayItem(point, null, null);
			itemizedOverlay.addOverlay(overlayItem);
			// ********************************

			try {
				List <Address> addresses = coder.getFromLocation(lat, lng, 1);
				if (addresses != null) {
					Address addr = addresses.get(0);
					for (int i = 0; i <= addr.getMaxAddressLineIndex(); i++) {
						String addLine = addr.getAddressLine(i);
						sLocationInfo += String.format("%s", addLine);
					}
				}
			} catch (IOException e) {
				Toast.makeText(getBaseContext(), "찾지못함", Toast.LENGTH_SHORT)
						.show();
			}

			location.setLatitude(lat);
			location.setLongitude(lng);
		} else {
			sLocationInfo = "위치 를 찾을수 업음";
		}
		editTextAddress.setText(sLocationInfo);
	}

	
	 LocationListener mLocationListener = new LocationListener() {

			public void onLocationChanged(Location location) {
				// TODO Auto-generated method stub
				updateLocation(location.getLatitude(), location.getLongitude());
			}

			public void onProviderDisabled(String provider) {
				// TODO Auto-generated method stub
				
			}

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

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

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

		@Override
		public boolean onTouchEvent(MotionEvent e, MapView mapView) {
			// TODO Auto-generated method stub
			if(e.getAction()==1){
				GeoPoint point = mapView.getProjection().fromPixels((int)e.getX(), (int)e.getY());
				updateLocation(point.getLatitudeE6()/1E6, point.getLongitudeE6()/1E6);
			}
			return false;
		}

	}
}


MyItemizedOverlay.java 
package kr.co.test.map01;
import java.util.ArrayList;

import android.graphics.drawable.Drawable;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;


public class MyItemizedOverlay extends ItemizedOverlay <OverlayItem>  {

	private ArrayList <OverlayItem>mOverlays  =new ArrayList <OverlayItem>();
	
	public MyItemizedOverlay(Drawable defaultMarker) {
		super(boundCenterBottom(defaultMarker));
		// TODO Auto-generated constructor stub
	}

	@Override
	protected OverlayItem createItem(int i) {
		// TODO Auto-generated method stub
		return mOverlays.get(i);
	}

	@Override
	public int size() {
		// TODO Auto-generated method stub
		return mOverlays.size();
	}
	
	public void addOverlay(OverlayItem overlay){
		mOverlays.add(overlay);
		populate();
	}

}
 
main.xml 


    <EDITTEXT android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/EditTextLat" android:hint="위도" />

    <EDITTEXT android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/EditTextLng" android:hint="경도" />

    <EDITTEXT android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/EditTextAddress" android:hint="주소" />

    <COM.GOOGLE.ANDROID.MAPS.MAPVIEW android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/mapView" android:apiKey="사용자키" android:clickable="true" android:enabled="true" />


AndroidManifest.xml 
   <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />

   <uses-library android:name="com.google.android.maps" />




반응형