반응형
|
안드로이드(Android) 체크옵션을 사용하여 일반맵에서 위성맵으로 변경하기 |
|
개발환경 : window 7 64bit, Eclipse Mars, Android 4.2.2 |
|
이번예제는 체크옵션을 이용해 일반 구글맵에서 위성사진으로 변경하는 옵션을 구현하였습니다. |
GoogleMap 에서 구성할수 있는 맵의 타입에서
위성사진을 보여주기 위해서는
MAP_TYPE_HYBRID, MAY_TYPE_SATELLITE 두가지가
있는데 차이점은 MAY_TYPE_SATELLITE 은 도로명이
나오지 않습니다.
클릭리스너 객체를 생성하고 클릭시 setSatellite()
함수가 실행 되도록 했습니다.
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);
}
};
Seekbar 를 움직였을 때 확대레벨을
조절하기 위한 Listener 객체 입니다.
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);
}
};
이전에 구현한 Seekbar 확대 레벨 조절하는 소스와
동일하며 차이가 있다면 위성화면을 사용할 것인지에
대한 부분만 부분만 추가 되었습니다.
전체 메인 레이아웃 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" >
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<CheckBox
android:id="@+id/satellite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Satellite "
/>
</LinearLayout>
<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);
// 현재 화면에 찍힌 포인트로 부터 위도와 경도를 알려준다.
LatLng latLng = mGoogleMap.getProjection()
.fromScreenLocation(screenPt);
Log.d("맵좌표", "좌표: 위도(" + String.valueOf(point.latitude)
+ "), 경도(" + String.valueOf(point.longitude) + ")");
Log.d("화면좌표", "화면좌표: X(" + String.valueOf(screenPt.x)
+ "), Y(" + String.valueOf(screenPt.y) + ")");
}
}
반응형
'안드로이드 개발' 카테고리의 다른 글
| 안드로이드(Android) 지니모션(GenyMotion) 에뮬레이터 파일 옮기기 (0) | 2014.11.10 |
|---|---|
| 안드로이드(Android) 사진의 EXIF 정보 가져오기 (1) | 2014.11.09 |
| 안드로이드(Android) XML 을 이용해 옵션메뉴를 구성하는 방법 (3) | 2014.11.09 |
| 안드로이드(Android) 위치값을 입력하여 구글 지도 이동하기 (5) | 2014.11.08 |
| 안드로이드(Android) Seekbar 를 이용하여 지도 배율조절과 마커이동 (0) | 2014.11.07 |
| 안드로이드(Android) 현재 GPS 정보를 알아와 구글맵에 마커 표시하기 (20) | 2014.11.07 |
| 안드로이드(Android) IBitmapDescriptorFactory is not initialized 에러 해결 (0) | 2014.11.06 |
| 안드로이드(Android) Android library projects cannot be launched 에러발생시 처치 (0) | 2014.11.06 |
