Please Enable JavaScript!
Gon[ Enable JavaScript ]

반응형

안드로이드(Android) Seekbar 를 이용하여 지도 배율조절과 마커이동

 

개발환경 : window 7 64bit, Eclipse Mars, Android 4.2.2

 

구글 맵을 표현하면서 Seekbar 로 배율을 조절 합니다.

초기 셋팅하는 코드는 없어서 조금 어색하지만

상단에 Seekbar 를 이리저리 움직이면 그 배율이

메시지로 뜨고 맵도 조절이 됩니다.

 

 

Seekbar 조절할때 마다 이벤트가 발생하게 되는

이벤트 캡쳐를 위해 OnSeekbarChangeListener 객체를

생성해서 해당함수들을 오버라이드 합니다.

그중 onProgressChanged() 함수는 Seekbar

움직일 때 발생하는 함수 이므로 이곳에

맵 크기를 조절하는 함수를 넣습니다.

 

private SeekBar.OnSeekBarChangeListener seekBarChangeListener = 
		new OnSeekBarChangeListener() {
	
	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		setZoomLevel(progress + 1);
	}
};

 

맴에 줌레벨을 조절하는 함수 입니다. animateCamert()

사용하고 있으며 Toast 를 이용해 크기 값을 메시지로

보여줍니다.

 

/**
 * 맵의 줌레벨을 조절합니다.
 * 
 * @param level
 */
private void setZoomLevel(int level) {
	mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(level));
	Toast.makeText(this, "Zoom Level : " + String.valueOf(level),
			Toast.LENGTH_LONG).show();
}

 

Activity 의 시작함수인 onCreate 에서는 SeekBar ClickListener

등록합니다.

 

mLongitude = (TextView) findViewById(R.id.longitude);
mLatitude = (TextView) findViewById(R.id.latitude);
mZoomBar = (SeekBar) findViewById(R.id.zoombar);

// 이벤트 등록
mZoomBar.setOnSeekBarChangeListener(seekBarChangeListener);

 

메인 activity 에 대한 전체 레이아웃 xml 입니다.

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
        
        <TextView
            android:id="@+id/longitude"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Longitude:" />

        <TextView
            android:id="@+id/latitude"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Latitude:" />

        <SeekBar
            android:id="@+id/zoombar"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:max="20"
            android:progress="0" />
    </LinearLayout>

    <fragment
        android:id="@+id/map"
        android:name="com.example.GoogleMapVersion2.Fragment"
        class="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </fragment>

</LinearLayout>

 

다음은 메인 activity 의 전체 소스 입니다.

맵표시하는 부분에 대한 설명은 이전 아티클을

참조하시기 바랍니다.

 

import android.graphics.Point;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class ZoomSeekbarActivity extends FragmentActivity implements
		OnMapClickListener {

	private TextView mLongitude, mLatitude;
	private GoogleMap mGoogleMap;
	private SeekBar mZoomBar;
	private CheckBox mSatellite;

	private int DEFAULT_ZOOM_LEVEL = 5;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_seekbar_safe);

		mLongitude = (TextView) findViewById(R.id.longitude);
		mLatitude = (TextView) findViewById(R.id.latitude);
		mZoomBar = (SeekBar) findViewById(R.id.zoombar);
		mSatellite = (CheckBox) findViewById(R.id.satellite);

		// 이벤트 등록
		mZoomBar.setOnSeekBarChangeListener(seekBarChangeListener);
		mSatellite.setOnClickListener(satelliteOnClickListener);

		// BitmapDescriptorFactory 생성하기 위한 소스
		MapsInitializer.initialize(getApplicationContext());

		GooglePlayServicesUtil
				.isGooglePlayServicesAvailable(ZoomSeekbarActivity.this);
		mGoogleMap = ((SupportMapFragment) getSupportFragmentManager()
				.findFragmentById(R.id.map)).getMap();

		// GPS 맵이동
		this.setGpsCurrent();
	}

	private CheckBox.OnClickListener satelliteOnClickListener = 
			new CheckBox.OnClickListener() {
		
		public void onClick(View v) {
			setSatellite();
		}
	};

	private void setSatellite() {
		if (mSatellite.isChecked()) {
			mGoogleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
		} else {
			mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
		}
	};

	private SeekBar.OnSeekBarChangeListener seekBarChangeListener = 
			new OnSeekBarChangeListener() {
		
		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			setZoomLevel(progress + 1);
		}
	};

	private void setGpsCurrent() {

		GpsInfo gps = new GpsInfo(ZoomSeekbarActivity.this);
		// GPS 사용유무 가져오기
		if (gps.isGetLocation()) {

			double latitude = 35.0950;
			double longitude = 127.7077;

			mLongitude.setText(String.valueOf(latitude));
			mLatitude.setText(String.valueOf(longitude));

			// Creating a LatLng object for the current location
			LatLng latLng = new LatLng(latitude, longitude);

			// Showing the current location in Google Map
			mGoogleMap.moveCamera(CameraUpdateFactory
					.newLatLng(latLng));

			// Map 을 zoom 합니다.
			this.setZoomLevel(DEFAULT_ZOOM_LEVEL);

			// 마커 설정.
			MarkerOptions optFirst = new MarkerOptions();
			optFirst.position(latLng);// 위도 · 경도
			optFirst.title("Current Position");// 제목 미리보기
			optFirst.snippet("Snippet");
			optFirst.icon(BitmapDescriptorFactory
					.fromResource(R.drawable.ic_launcher));
			mGoogleMap.addMarker(optFirst).showInfoWindow();
		}
	}

	/**
	 * 맵의 줌레벨을 조절합니다.
	 * 
	 * @param level
	 */
	private void setZoomLevel(int level) {
		mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(level));
		Toast.makeText(this, "Zoom Level : " + String.valueOf(level),
				Toast.LENGTH_LONG).show();
	}

	/** Map 클릭시 터치 이벤트 */
	public void onMapClick(LatLng point) {

		// 현재 위도와 경도에서 화면 포인트를 알려준다
		Point screenPt = mGoogleMap.getProjection().toScreenLocation(
				point);

		Log.d("맵좌표", "좌표: 위도(" + String.valueOf(point.latitude)
				+ "), 경도(" + String.valueOf(point.longitude) + ")");
		Log.d("화면좌표", "화면좌표: X(" + String.valueOf(screenPt.x)
				+ "), Y(" + String.valueOf(screenPt.y) + ")");
	}
}

 

안드로이드(Android) Seekbar 를 이용하여 지도 배율조절과 마커이동

반응형
Posted by 녹두장군1
,