구글에서 제공하는 안드로이드 용 지도 앱(단순히 정해준 위치의 지도를 보여주는 기능) 사용하기 예제를 이용하여 테스트 하고 사용 방법을 익혀 봅니다아주 단순하고 간단하게 만들어 볼 수 있습니다. 웹 주소는

https://developers.google.com/maps/documentation/android-sdk/start


이곳으로 여기에 가면 소스를 그대로 복사해서 테스트 해 볼 수 있습니다웹 페이지에 나와있는 순서대로 작업을 해봅니다물론 순서는 편리에 따라 바꾸어도 상관 없습니다.

 

Step 1. Download Android Studio


https://developer.android.com/studio/index.html


android google map sdk


자신의 운영체제에 맞는 패키지를 다운받아 설치 합니다. 현재 3.5버전 이고 다른 버전이라도 설치되어 있으면 패스. 저는 업데이트 했습니다.

 

Step 2. Install the Google Play services SDK


안드로이드 스튜디오 메뉴에서 팝업 창을 열고

Tools > SDK Manager > Appearance & Behavior > System Settings > Android SDK

우측에서 SDK Tools 탭을 선택 합니다.


android google map sdk


Google Play services 를 체크하여 설치 합니다기존에 설치를 했으면 패스.

 

Step 3. Create a Google Maps project


그냥 생각 나는 이름으로 프로젝트를 하나 생성 합니다.

Activityempty를 선택하고 생성 했습니다.


android google map sdk


Package nameAPI 사이트에 등록을 해야 하니 적당하게 입력 합니다저는 copycoding.tistory.mylocation으로 했습니다.


android google map sdk


Step 4. Get a Google Maps API key


구글에서 API를 사용할 수 있는 키를 생성 합니다. 그리고 package name을 이용하여 등록을 합니다.  기존에 생성한 API 키가 있다면 package name만 신규로 등록 합니다키 생성 방법은 검색하면 자세한 설명들이 바글바글 합니다저도 하나 설명한게 있습니다.

[안드로이드] google map 사용을 위한 API 생성(2018.11)


android google map sdk


Step 5. Hello Map! Take a look at the code


이제 프로젝트에 사이트에 있는 소스를 복사해서 붙여 넣기를 합니다사이트에는 두개의 소스가 있습니다.

 

activity_main.xml

<fragment 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:id="@+id/map"
   
tools:context=".MapsActivity"
   
android:name="com.google.android.gms.maps.SupportMapFragment" />

 

MainActivity.java

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
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 MapsActivity extends FragmentActivity implements OnMapReadyCallback {

   
private GoogleMap mMap;

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
       
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(
this);
    }

   
@Override
   
public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

       
// Add a marker in Sydney, Australia, and move the camera.
       
LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(
new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(
CameraUpdateFactory.newLatLng(sydney));
    }
}

 

이 두가지로만 하면 오류가 발생 합니다.

사이트에 없지만 추가를 해야 하는 항목이 두개 있습니다간단해서 생략 했을 수도 있습니다.


AndroidManifest.xml

<meta-data
   
android:name="com.google.android.geo.API_KEY"
   
android:value="API 키 값" >
</
meta-data>

Step 4에서 생성한 API 키를 입력 합니다.

 

build.gradle(Module: app)

implementation 'com.google.android.gms:play-services-maps:10.2.0'

라이브러리도 추가 해줍니다.

 

Step 6. Connect an Android device


테스트를 진행하기 위해 가상 디바이스를 생성 하거나 실제 핸드폰을 연결 합니다


android google map sdk


Step 7. Build and run your app


이제 프로그램을 실행 하기만 하면 끝 입니다.


android google map sdk


GPS를 이용 하는게 아니라서 방구석 처밖혀 인터넷만 연결되면 테스트가 가능 합니다.

줌 기능을 추가하지 않아 그냥 세계지도를 보는 느낌이네요.

 

전체 소스

 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:app="http://schemas.android.com/apk/res-auto"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
tools:context=".MainActivity">

    <fragment
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:id="@+id/map"
       
tools:context=".MapsActivity"
       
android:name="com.google.android.gms.maps.SupportMapFragment" />

</androidx.constraintlayout.widget.ConstraintLayout>

 

 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
   
package="copycoding.tistory.gpsmap">
    <application
       
android:allowBackup="true"
       
android:icon="@mipmap/ic_launcher"
       
android:label="@string/app_name"
       
android:roundIcon="@mipmap/ic_launcher_round"
       
android:supportsRtl="true"
       
android:theme="@style/AppTheme">

        <meta-data
           
android:name="com.google.android.geo.API_KEY"
           
android:value="API Key "/>
        <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>

 

 

build.gradle(Module: app)

apply plugin: 'com.android.application'

android {
    compileSdkVersion
29
   
buildToolsVersion '29.0.2'
   
defaultConfig {
        applicationId
"copycoding.tistory.gpsmap"
       
minSdkVersion 16
        
targetSdkVersion 29
       
versionCode 1
       
versionName "1.0"
       
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
   
}
    buildTypes {
        release {
           
minifyEnabled false
           
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
       
}
    }
}

dependencies {
    implementation fileTree(
dir: 'libs', include: ['*.jar'])
    implementation
'androidx.appcompat:appcompat:1.1.0'
   
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
   
implementation 'com.google.android.gms:play-services-maps:10.2.0'
   
testImplementation 'junit:junit:4.12'
   
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
   
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

 

 

 

 

MainActivity.java

package copycoding.tistory.mylocation;

import
androidx.appcompat.app.AppCompatActivity;
import
androidx.fragment.app.FragmentActivity;

import
android.os.Bundle;

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 FragmentActivity implements OnMapReadyCallback {

   
private GoogleMap mMap;

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
       
setContentView(R.layout.activity_main);

       
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.
map);
       
mapFragment.getMapAsync(this);
   
}

   
@Override
   
public void onMapReady(GoogleMap googleMap) {
       
mMap = googleMap;

       
// Add a marker in Sydney, Australia, and move the camera.
       
LatLng sydney = new LatLng(-34, 151);
       
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
       
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
   
}
}

 

 - copy coding -


+ Recent posts