나의 위치로 구글맵 띄우기
GPS 를 이용하여 나의 위치찾기 -> 구글맵 띄우기
java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | package com.ghj.ex_004; import android.Manifest; import android.content.Intent; import android.content.pm.PackageManager; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Build; import android.os.Bundle; import android.provider.Settings; import android.support.annotation.NonNull; import android.support.v4.app.ActivityCompat; import android.support.v4.app.FragmentActivity; import android.support.v4.content.ContextCompat; import android.util.Log; import android.view.View; import android.widget.RelativeLayout; import android.widget.Toast; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.MapFragment; import com.google.android.gms.maps.OnMapReadyCallback; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; public class MainActivity extends FragmentActivity implements OnMapReadyCallback { //GoogleMap 객체 GoogleMap googleMap; MapFragment mapFragment; LocationManager locationManager; RelativeLayout boxMap; //나의 위도 경도 고도 double mLatitude; //위도 double mLongitude; //경도 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); boxMap = (RelativeLayout)findViewById(R.id.boxMap); //LocationManager locationManager = (LocationManager)getSystemService(LOCATION_SERVICE); //GPS가 켜져있는지 체크 if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){ //GPS 설정화면으로 이동 Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); intent.addCategory(Intent.CATEGORY_DEFAULT); startActivity(intent); finish(); } //마시멜로 이상이면 권한 요청하기 if(Build.VERSION.SDK_INT >= 23){ //권한이 없는 경우 if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){ ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION , Manifest.permission.ACCESS_FINE_LOCATION} , 1); } //권한이 있는 경우 else{ requestMyLocation(); } } //마시멜로 아래 else{ requestMyLocation(); } } //권한 요청후 응답 콜백 @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { //ACCESS_COARSE_LOCATION 권한 if(requestCode==1){ //권한받음 if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){ requestMyLocation(); } //권한못받음 else{ Toast.makeText(this, "권한없음", Toast.LENGTH_SHORT).show(); finish(); } } } //나의 위치 요청 public void requestMyLocation(){ if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){ return; } //요청 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 10, locationListener); } //위치정보 구하기 리스너 LocationListener locationListener = new LocationListener() { @Override public void onLocationChanged(Location location) { if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){ return; } //나의 위치를 한번만 가져오기 위해 locationManager.removeUpdates(locationListener); //위도 경도 mLatitude = location.getLatitude(); //위도 mLongitude = location.getLongitude(); //경도 //맵생성 SupportMapFragment mapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map); //콜백클래스 설정 mapFragment.getMapAsync(MainActivity.this); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { Log.d("gps", "onStatusChanged"); } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } }; //구글맵 생성 콜백 @Override public void onMapReady(GoogleMap googleMap) { this.googleMap = googleMap; //지도타입 - 일반 this.googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); //나의 위치 설정 LatLng position = new LatLng(mLatitude , mLongitude); //화면중앙의 위치와 카메라 줌비율 this.googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 15)); //지도 보여주기 boxMap.setVisibility(View.VISIBLE); } } | cs |
xml)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:id="@+id/boxMap" android:visibility="invisible" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:id="@+id/map" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="wrap_content" android:name="com.google.android.gms.maps.SupportMapFragment"/> </RelativeLayout> </LinearLayout> | cs |
AndroidManifest.xml)
1 2 3 4 5 | <!-- Google Map --> <meta-data android:name="com.google.android.geo.API_KEY" android:value="" /> ... <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> | cs |
gradle)
1 2 3 4 | dependencies { ... compile 'com.google.android.gms:play-services-maps:9.8.0' } | cs |
'IT > - 프로그래밍' 카테고리의 다른 글
Cocos UserDefault 저장소 (0) | 2017.03.20 |
---|---|
Cocos Vector 사용 (0) | 2017.03.19 |
안드로이드 uiautomatorviewer 실행시 Unable to connect to adb. Check if adb is installed correctly. (0) | 2017.03.18 |
Cocos 타일이미지를 이용한 배경스크롤 하기 (0) | 2017.03.17 |
Cocos ParallaxNode를 이용한 배경스크롤 구현하기 (0) | 2017.03.16 |