๊ตฌ๊ธ๋งต ์ฌ์ฉํ๊ธฐ
https://developers.google.com/maps/documentation/android-sdk/secrets-gradle-plugin?hl=ko
- ์ด๊ณณ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค์น์ ํ๊ฒฝ์ค์ ์ ๋ํ ์ ๋ณด๊ฐ ๋์์๋ค.
API ํค ๊ฐ ์์ฑํ๊ณ ์๋๋ก์ด๋ ์คํ๋์ค์ ์์ฑํ๊ธฐ
https://console.cloud.google.com/welcome/new?project=igneous-axiom-426807-f3
Google ํด๋ผ์ฐ๋ ํ๋ซํผ
๋ก๊ทธ์ธ Google ํด๋ผ์ฐ๋ ํ๋ซํผ์ผ๋ก ์ด๋
accounts.google.com
์ฌ์ฉ ํด๋ฆญํ๊ธฐ
์ฌ์ฉ์ API ํค ์์ฑ ํ, API ํค๋ฅผ ํด๋ฆญํด์ ๋ค์ด๊ฐ์
Maps SDK for Android API๋ฅผ ์ ํํ์ฌ ์ ์ฅ ๋๋ฅด๊ณ ,
ํค ํ์ ๋๋ฌ์ ํค ๊ฐ ์ ์ฅํด๋๊ธฐ
Android -> Project Files๋ก ์ด๋ํ๊ธฐ
local.defaults.properties ์ secrets.properties ํ์ผ ๋ง๋ค๊ณ , local.defaults.properties๋ฅผ ์๋์ ๊ฐ์ด ์์ฑํ๋ค.
API ํค ๋ณต์ฌํ ๊ฒ secrets.properties์ ์์ฑํ๊ธฐ
build.gradle (Project)
buildscript {
dependencies {
classpath("com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.1")
}
}
build.gradle (:app)
id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin")
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.*"
}
buildFeatures {
buildConfig = true
}
implementation("com.google.android.gms:play-services-maps:18.2.0")
Sync Now ๋๋ฅด๊ธฐ
Manifest ์ค์ ํ๊ธฐ
<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"/>
<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_main ์์ฑํ๊ธฐ
<?xml version="1.0" encoding="utf-8"?>
<fragment 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:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:name="com.google.android.gms.maps.SupportMapFragment">
</fragment>
MainActivity ์์ฑํ๊ธฐ
package com.~.map;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
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) {
// ๋งต์ด ์ค๋น๋๋ฉด ๋ด ์์น๋ฅผ ์ค์ฌ์ผ๋ก ์ง๋๊ฐ ๋์ค๊ฒ ํด๋ผ ๋ผ๋ ์ฝ๋ ์์ฑ
// 1. ํน์ ์๋, ๊ฒฝ๋ ๊ฐ์ผ๋ก ์ง๋์ ์์น๋ฅผ ์ ์ฉ์ํค๋ ์ฝ๋
LatLng myLocation = new LatLng(37.5428, 126.6772); //ํํํ๊ณ ์ ํ๋ ์์น์ ์๋ ๊ฒฝ๋ ์์ฑ
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myLocation, 17));
// 2. ๋ง์ปค๋ฅผ ๋ง๋ค์ด์, ์ง๋์ ํ์ํ๋ ์ฝ๋
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(myLocation).title("์ฐํฌ์ง์
์ ๋ฌธํ๊ต");
// 3. ๋ง์ปค๋ฅผ ํด๋ฆญํ๋ฉด, ๋์ํ๋ ์ฝ๋ ์์ฑ
// ์ค์! ์์์ MarkerOptions๋ฅผ ๋ง๋ค ๋, ํ๊ทธ๋ฅผ ๋ฌ์์ค์ ๊ตฌ๋ถํด ์ค์ผ ํ๋ค.
googleMap.addMarker(markerOptions).setTag(0);
googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(@NonNull Marker marker) {
int tag = (int) marker.getTag();
Toast.makeText(MainActivity.this,
"์ ๊ฐ ๋๋ฅธ ๋ง์ปค์ ํ๊ทธ๋ : " + tag + "\n ํ์ดํ์ : " + marker.getTitle(),
Toast.LENGTH_SHORT).show();
return false;
}
});
// 4. ์ง๋์ ํ์
์ ์ค์ ํ๋ ์ฝ๋
// googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
}
});
}
}