547
No
1. 현재 좌표 알아내기 ( 이전 강좌 )
2. 구글 등록 및 commd 로 나의 인증서 지문(SHA1) 알아내기
강좌 내용이 구버전이라 코드를 변경 하였다.
class MainActivity
package kr.co.braverokmc.locationmap;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
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 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.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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// SupportMapFragment를 객체를 생성하면 사용할 준비가되면 알림을받습니다
SupportMapFragment fragment = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map);
//구 버전 map=fragment.getMap();
fragment.getMapAsync(this);
//내장된 위치 참조
manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
listener = new MyLocationListenre();
}
@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;
}
manager.removeUpdates(listener);
}
}
@Override
protected void onResume() {
super.onResume();
requestMyLocation();
}
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);
}
// 호출해 주면서 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("Marker in Sydney"));
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/activity_main"
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
<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" />
<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"/>
<?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="@mipmap/ic_launcher"
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>
121강
122강
댓글 ( 4)
댓글 남기기