548
No
class MainActivity
package kr.co.braverokmc.locationmap;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
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 MainActivity extends AppCompatActivity implements OnMapReadyCallback {
private static final String TAG = "MainActivity";
private GoogleMap map;
LocationManager manager;
MyLocationListenre listener;
SupportMapFragment fragment;
//나침반 표시하기
SensorManager sensorManager;
MySensorListner mySensorListner;
private CompassView mCompassView;
private boolean mCompassEnabled;
private RelativeLayout mainLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// SupportMapFragment를 객체를 생성하면 사용할 준비가되면 알림을받습니다
fragment = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map);
//구 버전 map=fragment.getMap();
fragment.getMapAsync(this);
//내장된 위치 참조
manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
listener = new MyLocationListenre();
//나침반넣기
// 나침판을 위한 메인 레이아웃 객체 참조
mainLayout = (RelativeLayout) findViewById(R.id.mainLayout);
sensorManager =(SensorManager)getSystemService(Context.SENSOR_SERVICE);
mySensorListner=new MySensorListner();
// 나침반을 표시할 뷰 생성
boolean sideBottom = true;
mCompassView = new CompassView(this);
mCompassView.setVisibility(View.VISIBLE);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params.addRule(sideBottom ? RelativeLayout.ALIGN_PARENT_BOTTOM : RelativeLayout.ALIGN_PARENT_TOP);
mainLayout.addView(mCompassView, params);
mCompassEnabled = true;
}
@Override
protected void onPause() {
super.onPause();
if (manager != null) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
// pause 와 resume 되면서 자신의 위치 깜박
if(map!=null){
map.setMyLocationEnabled(false);
}
if(manager!=null){
manager.removeUpdates(listener);
}
//나침판 표시하기
if(mCompassEnabled) {
sensorManager.unregisterListener(mySensorListner);
}
}
}
@Override
protected void onResume() {
super.onResume();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
requestMyLocation();
//이코드만 적어주면 onResume 되면서 자신의 위치가
// 자동으로 깜바깜 거린다.
if(map!=null){
map.setMyLocationEnabled(true);
}
//나침반 표시하기
if(mCompassEnabled) {
sensorManager.registerListener(mySensorListner,
sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_UI);
}
}
public void requestMyLocation(){
//ex) 10초마다 업데이트
long minTime = 10000;
//자신이 움직였을 거리의 차이
//0이면 항상 업데이트를 한다.
float minDistance = 0;
//위치 정보 권한 풍선말 생겼을 시 자동 체크 자동 체크
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
//requestLocationUpdates 나의 위치정보를 업데이트를 해서 가져오는 것으로
// 위성 에서 가져오기때문에 시간차가 크다
//위 if 문은 풍선말 선택시 자동 셋팅 된다.
// 또한 AndroidMainfest 에서 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
// 자동 설정 된다.
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
minTime, minDistance, listener);
// 기지국 기반으로 위치를 알아낸다.
manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
minTime, minDistance, listener);
// 위성 GPS 또는 기지국이든 둘중 하나 선택해서 자동을 위치를 알아낸다.
//GPS 를 통해서 알았던 좌표중에서 가장 최근의 좌표를 가져온다.
Location location =manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location!=null){
Double latitude = location.getLatitude();
Double longitude = location.getLongitude();
Log.i(TAG,"가장 최근의 내 위치 : "+ latitude + ", " + longitude );
// textView1.setText( "가장 최근의 내 위치 : "+ latitude + ", " + longitude);
//텍스트 뷰에 변경이 있으면 업데이트
// textView1.invalidate();
}
}
private void showCurrentMap(Double latitude, Double longitude){
LatLng curPoint =new LatLng(latitude, longitude);
//카메라에 저장
map.animateCamera(CameraUpdateFactory.newLatLngZoom(curPoint, 15));
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
//자신의 위치에 아이콘 표시
MarkerOptions marker =new MarkerOptions();
marker.position(new LatLng(latitude+0.0001, longitude+0.0001));
marker.title("정민주");
marker.snippet("수원입니다.");
marker.draggable(true);
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.a1));
map.addMarker(marker);
}
//나침반 표시하기
class MySensorListner implements SensorEventListener{
private int iOrientation = -1;
// 센서의 값을 받을 수 있도록 호출되는 메소드
@Override
public void onSensorChanged(SensorEvent event) {
//매트릭스 객체를 이용해서 이미지를 변경
Log.d("MySensorListner", "sensor #0 " +event.values[0]);
if (iOrientation < 0) {
iOrientation = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
}
mCompassView.setAzimuth(event.values[0] + 90 * iOrientation);
mCompassView.invalidate();
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
// 호출해 주면서 Location 객체를 던져 주는 역할을 한다.
class MyLocationListenre implements LocationListener {
Double latitude2, longitude2;
@Override
public void onLocationChanged(Location location) {
latitude2 = location.getLatitude();
longitude2 = location.getLongitude();
Log.i( TAG, "나의 위치 : "+ latitude2 + ", " + longitude2);
//텍스트 뷰에 변경이 있으면 업데이트
// textView2.invalidate();
/* textView2.setText( "나의 위치 : "+ latitude + ", " + longitude);
//텍스트 뷰에 변경이 있으면 업데이트
textView2.invalidate();*/
showCurrentMap(latitude2, longitude2);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
map.addMarker(new MarkerOptions().position(sydney).title("정민주 주인백"));
map.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
R.layout.activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="kr.co.braverokmc.locationmap.MainActivity">
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
/>
</RelativeLayout>
AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="kr.co.braverokmc.locationmap">
<permission android:name="kr.co.braverokmc.locationmap.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="kr.co.braverokmc.locationmap.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
<application
android:allowBackup="true"
android:icon="@drawable/a2"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<uses-library android:name="com.google.android.maps"/>
<meta-data android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyAtpVA1OTj-ulVDGvNGeKS93QsBYpK0HC4" />
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
class CompassView
package kr.co.braverokmc.locationmap;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.view.View;
/**
* 나침반을 표시하기 위한 클래스를 정의합니다.
*
* @author Mike
*
*/
public class CompassView extends View {
private Drawable mCompass;
private float mAzimuth = 0;
private int PADDING = 2;
public CompassView(Context ctx) {
super(ctx);
this.mCompass = ctx.getResources().getDrawable(R.drawable.arrow_n);
}
protected void onDraw(Canvas canvas) {
canvas.save();
canvas.rotate(360 - mAzimuth, PADDING + mCompass.getMinimumWidth()
/ 2, PADDING + mCompass.getMinimumHeight() / 2);
mCompass.setBounds(PADDING, PADDING, PADDING
+ mCompass.getMinimumWidth(), PADDING
+ mCompass.getMinimumHeight());
mCompass.draw(canvas);
canvas.restore();
super.onDraw(canvas);
}
public void setAzimuth(float aAzimuth) {
mAzimuth = aAzimuth;
}
}
아이콘 추가부분
private void showCurrentMap(Double latitude, Double longitude){
LatLng curPoint =new LatLng(latitude, longitude);
//카메라에 저장
map.animateCamera(CameraUpdateFactory.newLatLngZoom(curPoint, 15));
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
//자신의 위치에 아이콘 표시
MarkerOptions marker =new MarkerOptions();
marker.position(new LatLng(latitude+0.0001, longitude+0.0001));
marker.title("정민주");
marker.snippet("수원입니다.");
marker.draggable(true);
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.a1));
map.addMarker(marker);
}
onPause()
// pause 와 resume 되면서 자신의 위치 깜박
if(map!=null){
map.setMyLocationEnabled(false);
}
onResume()
//이코드만 적어주면 onResume 되면서 자신의 위치가
// 자동으로 깜바깜 거린다.
if(map!=null){
map.setMyLocationEnabled(true);
}
나침판 추가 부분
onCreate()
//나침반넣기
// 나침판을 위한 메인 레이아웃 객체 참조
mainLayout = (RelativeLayout) findViewById(R.id.mainLayout);
sensorManager =(SensorManager)getSystemService(Context.SENSOR_SERVICE);
mySensorListner=new MySensorListner();
// 나침반을 표시할 뷰 생성
boolean sideBottom = true;
mCompassView = new CompassView(this);
mCompassView.setVisibility(View.VISIBLE);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params.addRule(sideBottom ? RelativeLayout.ALIGN_PARENT_BOTTOM : RelativeLayout.ALIGN_PARENT_TOP);
mainLayout.addView(mCompassView, params);
mCompassEnabled = true;
//나침반 표시하기
class MySensorListner implements SensorEventListener{
private int iOrientation = -1;
// 센서의 값을 받을 수 있도록 호출되는 메소드
@Override
public void onSensorChanged(SensorEvent event) {
//매트릭스 객체를 이용해서 이미지를 변경
Log.d("MySensorListner", "sensor #0 " +event.values[0]);
if (iOrientation < 0) {
iOrientation = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
}
mCompassView.setAzimuth(event.values[0] + 90 * iOrientation);
mCompassView.invalidate();
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
123강 지도에 아이콘 추가
124강 지도에 나침판 추가
댓글 ( 4)
댓글 남기기