본문 바로가기

플밍 is 뭔들/안드로이드_인강

17-1. 맵

※ 맵을 이용하기 위한 설정
 - 구글맵을 이용하기 위해서는 인증을 포함한 몇 가지 설정을 해야 한다.
 - 인증방법
  1. Eclipse 실행 -> Widnow메뉴 -> Preferences -> Android -> Build -> SHA1 fingerprint의 값을 구글서버에 등록해야 하므로 복사한다.
  2. 구글서버 주소는 http://code.google.com/apis/console 이다.
  3. 위의 주소에 접속하여 Google Map Android API 클릭
  4. API 키 생성
  5. Eclipse로 돌아와 SDK Manager 실행
  6. Extras의 Google play service 설치 or 업데이트
  7. import -> Android의 Existing Android Code Into Workspace 선택
  8. AppData폴더 -> Local -> android-sdk -> extras -> google -> google play service폴더를선택하여 임포트 하기.
  9. 적용하려는 프로젝트를 우클릭하여 Properties 를 들어간 후 Android 를 클릭한 후 라이브러리를 추가한다.
  10. AndroidMenifest.xml에 등록
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<permission android:name="패키지명.permission.MAPS_RECEIVE" android:protectionLevel="signature"/>

<meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="Key값"/>
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>

※ 예시

MainActivity
public class MainActivity extends ActionBarActivity {
       GoogleMap gMap = null;
       Marker makerUser = null;
       boolean mFirstLoc = true;
       LocationManager locationManager = null;
       
       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              
              //api 이용하기
              GooglePlayServicesUtil.isGooglePlayServicesAvailable(MainActivity.this);
              
              //맵 불러오기
              MapFragment mapFragment =  (MapFragment) getFragmentManager().findFragmentById(R.id.fm_map_01);
              gMap = mapFragment.getMap();
              
              //맵이동
              LatLng latLng = new LatLng(33.459, 126.942); //성산일출봉     
              gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15));
              
              //마커설정
              MarkerOptions markerOptions = new MarkerOptions();
              markerOptions.position(latLng);
              markerOptions.title("User");
              makerUser = gMap.addMarker(markerOptions);
              makerUser.showInfoWindow();
              
              //시스템서비스 받아오기
              locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
       }
       @Override
       //어플실행중인 상태에 locationManager를 올려놔야 어플실행중 상태변화와 위치변화를를 감지할 수 있다.
       protected void onResume() {
              super.onResume();
              //위치받기
//            String provider = LocationManager.GPS_PROVIDER;
              String provider = LocationManager.NETWORK_PROVIDER;
              //위치업데이트
              locationManager.requestLocationUpdates(provider, (long)3000, 3.f, locationListener);
       }
       
       @Override
       public boolean onCreateOptionsMenu(Menu menu) {
              // Inflate the menu; this adds items to the action bar if it is present.
              getMenuInflater().inflate(R.menu.main, menu);
              return true;
       }
       
       LocationListener locationListener = new LocationListener() {
              
              @Override
              //상태 변화가 있을 때
              public void onStatusChanged(String provider, int status, Bundle extras) {
                     // TODO Auto-generated method stub
                     Toast.makeText(MainActivity.this, "onStatusChanged()", Toast.LENGTH_SHORT).show();
              }
              
              @Override
              //상태를 사용할 수 있는 상태일 때
              public void onProviderEnabled(String provider) {
                     // TODO Auto-generated method stub
                     Toast.makeText(MainActivity.this, "onProviderEnabled()", Toast.LENGTH_SHORT).show();
                     
              }
              
              @Override
              //상태를 이용하지 못할 때 ex)인터넷이 연결이 안됐을 때
              public void onProviderDisabled(String provider) {
                     // TODO Auto-generated method stub
                     Toast.makeText(MainActivity.this, "onProviderDisabled()", Toast.LENGTH_SHORT).show();
                     
              }
              
              @Override
              //위치가 변했을 때
              public void onLocationChanged(Location location) {
                     // TODO Auto-generated method stub
                     Toast.makeText(MainActivity.this, "onLocationChanged()", Toast.LENGTH_SHORT).show();
                     LatLng uPos = new LatLng(location.getLatitude(), location.getLongitude());
                     makerUser.setPosition(uPos);
                     gMap.moveCamera(CameraUpdateFactory.newLatLng(uPos));
              }
       };
       @Override
       public boolean onOptionsItemSelected(MenuItem item) {
              // Handle action bar item clicks here. The action bar will
              // automatically handle clicks on the Home/Up button, so long
              // as you specify a parent activity in AndroidManifest.xml.
              int id = item.getItemId();
              if (id == R.id.action_settings) {
                     return true;
              }
              return super.onOptionsItemSelected(item);
       }
}

activity_main
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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="com.example.android_28_1_ex1.MainActivity" >
    <fragment
        android:id="@+id/fm_map_01"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.google.android.gms.maps.MapFragment" />
</RelativeLayout>







'플밍 is 뭔들 > 안드로이드_인강' 카테고리의 다른 글

19-1. 마켓  (0) 2016.11.28
18-1. 네트워크  (0) 2016.11.28
16-1. 데이터 베이스  (0) 2016.11.28
15-1. 데이터  (0) 2016.11.28
14-1. 브로드캐스트 리시버  (0) 2016.11.28