반응형
|
안드로이드(Android) 위치값을 입력하여 구글 지도 이동하기 |
|
개발환경 : window 7 64bit, Eclipse Mars, Android 4.2.2 |
|
이번예제도 맵과 관련된 것이며 이전에 작성한 위성사진 전환하는 소스와 거의 동일합니다. 단지 위도와 경도를 입력하게 해서 버튼을 클릭함과 동시에 그 위치로 맵을 이동시키는 것입니다. |
위치이동 버튼의 이벤트를 등록합니다.
버튼클릭시 GPS 값으로 이동하기 위해
만들어 놓은 함수 setGpsCurrent() 를
실행하는데 인수값으로 텍스트 위도, 텍스트 경도
값을 읽어와 넘깁니다.
mMove.setOnClickListener(moveOnClickListener);
/** 버튼 클릭시 이동 */
private Button.OnClickListener moveOnClickListener =
new Button.OnClickListener() {
public void onClick(View v) {
setGpsCurrent(mLongitude.getText().toString(), mLatitude
.getText().toString());
}
};
이렇게 넘긴값을 받는데 텍스트 값에 아무값도
없을 경우 현재 GPS 로 이동하고 만약 값이
있다면 텍스트값에 입력된 GPS 값을
셋팅합니다. 그리고 moveCamera() 로 이동합니다.
if (strLat.equals("") || strLng.equals("")) {
latitude = gps.getLatitude();
longitude = gps.getLongitude();
}else{
latitude = Double.parseDouble(strLat);
longitude = Double.parseDouble(strLng);
}
// 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));
아래는 메인 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" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<CheckBox
android:id="@+id/satellite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Satellite " />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/longitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Longitude:" />
<EditText
android:id="@+id/txtLongutude"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/latitude"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Latitude:" />
<EditText
android:id="@+id/txtLatitude"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" />
</LinearLayout>
<Button
android:id="@+id/btnMove"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="위치 이동" />
</LinearLayout>
<fragment
android:id="@+id/map"
android:name="com.example.GoogleMapVersion2.Fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" >
</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.Button;
import android.widget.CheckBox;
import android.widget.EditText;
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 InsertMapMoveActivity extends FragmentActivity implements
OnMapClickListener {
private EditText mLongitude, mLatitude;
private GoogleMap mGoogleMap;
private CheckBox mSatellite;
private Button mMove;
private int DEFAULT_ZOOM_LEVEL = 13;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_insert_mapmove);
mLongitude = (EditText) findViewById(R.id.txtLongutude);
mLatitude = (EditText) findViewById(R.id.txtLatitude);
mSatellite = (CheckBox) findViewById(R.id.satellite);
mMove = (Button) findViewById(R.id.btnMove);
// 이벤트 등록
mSatellite.setOnClickListener(satelliteOnClickListener);
mMove.setOnClickListener(moveOnClickListener);
// BitmapDescriptorFactory 생성하기 위한 소스
MapsInitializer.initialize(getApplicationContext());
GooglePlayServicesUtil
.isGooglePlayServicesAvailable(InsertMapMoveActivity.this);
mGoogleMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
// GPS 맵이동
this.setGpsCurrent(mLongitude.getText().toString(), mLatitude
.getText().toString());
}
/** 버튼 클릭시 이동 */
private Button.OnClickListener moveOnClickListener =
new Button.OnClickListener() {
public void onClick(View v) {
setGpsCurrent(mLongitude.getText().toString(), mLatitude
.getText().toString());
}
};
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 void setGpsCurrent(String strLat, String strLng) {
double latitude = 0;
double longitude = 0;
GpsInfo gps = new GpsInfo(InsertMapMoveActivity.this);
// GPS 사용유무 가져오기
if (gps.isGetLocation()) {
if (strLat.equals("") || strLng.equals("")) {
latitude = gps.getLatitude();
longitude = gps.getLongitude();
} else {
latitude = Double.parseDouble(strLat);
longitude = Double.parseDouble(strLng);
}
// 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) 간단한 파일 브라우저 만들기 (0) | 2014.11.11 |
|---|---|
| 안드로이드(Android) 지니모션(GenyMotion) 에뮬레이터 파일 옮기기 (0) | 2014.11.10 |
| 안드로이드(Android) 사진의 EXIF 정보 가져오기 (1) | 2014.11.09 |
| 안드로이드(Android) XML 을 이용해 옵션메뉴를 구성하는 방법 (3) | 2014.11.09 |
| 안드로이드(Android) 체크옵션을 사용하여 일반맵에서 위성맵으로 변경하기 (0) | 2014.11.08 |
| 안드로이드(Android) Seekbar 를 이용하여 지도 배율조절과 마커이동 (0) | 2014.11.07 |
| 안드로이드(Android) 현재 GPS 정보를 알아와 구글맵에 마커 표시하기 (20) | 2014.11.07 |
| 안드로이드(Android) IBitmapDescriptorFactory is not initialized 에러 해결 (0) | 2014.11.06 |

