※ 구글 공식 홈페이지를 참고하여 진행하였습니다.
링크주소 : https://developers.google.com/maps/documentation/android-sdk/config
Set up an Android Studio project | Maps SDK for Android | Google for Developers
New map styling is coming soon to Google Maps Platform. This update to map styling includes a new default color palette and improvements to map experiences and usability. All map styles will be automatically updated in March 2025. For more information on a
developers.google.com
Step 1: Set up Android Studio
안드로이드가 설치되어 있다면, Step2로 넘어가서 환경 설정을 해줘야 한다.
Step 2. Set up the SDK
1. Gradle Scripts > settings.gradle.kts
pluginManagement{ } 안에 gradlePluginPortal(), google(), mavenCentral() 이 있는지 확인하고, 없으면 넣어주기
pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}
2. Gradle Scripts > settings.gradle.kts
dependencyResolutionManagement{ } 안에 google(), mavenCentral() 있는지 확인하고, 없으면 넣어주기
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
3. Gradle Scripts > build.gradle.kts(Module :app)
상단에 있는 plugins{ } 안에 아래 코드를 넣어준다.
implementation("com.google.android.gms:play-services-maps:18.2.0")
4. Gradle Scripts > build.gradle.kts(Module :app)
내 안드로이드 compileSdk = 34, minSdk = 19 레벨을 확인해준다.
5. Gradle Scripts > build.gradle.kts(Module :app)
안드로이드 모듈을 나타내는 영역안에 buildFeatures 코드를 넣어준다.
android {
// ...
buildFeatures {
buildConfig = true
// ...
}
}
Step 3: Add your API key to the project
1. Gradle Scripts > build.gradle.kts(Project: 이름)
plugins 하단에 아래 코드를 넣어준다.
buildscript {
dependencies {
classpath("com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.1")
}
}
2. Gradle Scripts > build.gradle.kts(Module :app)
plugins{ } 안에 아래 코드를 넣어준다.
id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin")
3. Gradle Scripts > build.gradle.kts(Module :app)
targetSdk 와 compileSdk 가 34로 되어 있는지 확인해 준다.
4. 우측 상단의 Sync Now 를 하여, 지금까지 설정한 환경을 저장해 준다.
5. secrets.properties 폴더에 API 키를 넣어준다.
MAPS_API_KEY=YOUR_API_KEY
secrets.properties 폴더가 없다면 만들어 준다.
폴더는 Project Files 경로에 가서 만들어 주는 것이 좋다.
좌측 상단의 Android 글자 옆에 꺽쇠를 클릭해 주면 모드 변경을 할 수 있다.
나의 프로젝트 저장 경로 선택 후 오른쪽 마우스 > New > File 선택
파일 이름 : secrets.properties
6. 폴더가 생성되었다. 이 폴더 안에 내 API 키를 지정해주는 코드를 넣어 준다.
참고) API 키는 Maps SDK for Android 권한이 있는 키로 넣어줘야 한다.
7. local.defaults.properties 폴더 안에 아래 코드 넣어주고, 8. 저장하기
MAPS_API_KEY=DEFAULT_API_KEY
해당 파일이 없다면, secrets.properties 폴더만들 때와 동일하게 폴더를 생성해 주면 된다.
9. manifests > AndroidManifest.xml
메니페스트 파일에서 <application.....> 과 <activity...> 사이에 아래 코드를 넣어준다.
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="${MAPS_API_KEY}" />
10. Gradle Scripts > build.gradle.kts(Module :app)
plugins {...} 아래에 아래 코드를 넣어준다.
secrets {
// Optionally specify a different file name containing your secrets.
// The plugin defaults to "local.properties"
propertiesFileName = "secrets.properties"
// A properties file containing default secret values. This file can be
// checked in version control.
defaultPropertiesFileName = "local.defaults.properties"
// Configure which keys should be ignored by the plugin by providing regular expressions.
// "sdk.dir" is ignored by default.
ignoreList.add("keyToIgnore") // Ignore the key "keyToIgnore"
ignoreList.add("sdk.*") // Ignore all keys matching the regexp "sdk.*"
}
Step 4: Update the app manifest
manifests > AndroidManifest.xml 파일에 권한 요청을 추가해 준다.
위에서 넣어준 <meta-data.../> 아래에 아래 코드를 넣어준다.
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
권한 요청을 추가해 준다.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
완성된 Manifest.xml 파일 코드
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Map2"
tools:targetApi="31">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="${MAPS_API_KEY}" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java 파일에 아래 코드를 넣어주고 에뮬레이터를 실행시키면 지도가 표시된다.
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(@NonNull GoogleMap googleMap) {
}
});
}
}
'Android Studio' 카테고리의 다른 글
[Android Studio] Fragment 설정 하기, MainActivity에 연결하기 (0) | 2024.06.18 |
---|---|
[Android Studio] 카메라와 앨범 사용하기 위한 xml 파일, 메니페스트파일, 라이브러리 환경설정 (0) | 2024.06.14 |
[Android Studio] 앱 아이콘 만드는 방법 (0) | 2024.06.04 |
[Android Studio] 앱의 액션바(Action Bar)를 없애는 방법 (0) | 2024.06.04 |
[Android Studio] 프로젝트 구조 살펴보기, 프로젝트 생성하기, MainActivity 생성하기 (0) | 2024.06.04 |