https://console.cloud.google.com/google/maps-apis/overview?pli=1
Google Cloud Platform
하나의 계정으로 모든 Google 서비스를 Google Cloud Platform을 사용하려면 로그인하세요.
accounts.google.com
여기서 api받아와야함.
만들기 눌러줌
이거사용한대
사용설정누르고
사용자인증정보생성
api키 생성
-> 키제한
-> android앱 선택
->항목추가 , 안드로이드 프로젝트 패키지 이름 작성 com.example.googlemapexample
-> sha는 컴퓨터마다 다름 cmd가서
"C:\Program Files\Android\Android Studio\jre\bin\keytool" -list -v -keystore "%USERPROFILE%\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android
이거입력
ssha복사
해서 저장하고
menifest가서 meta- data추가해줘서 api등록해줘야함.
강의내용이 최신버전이 아니라서 참고한 사이트
https://webnautes.tistory.com/647
Google Maps Android API 사용 방법 및 예제
Google Maps Android API를 사용하는 기본적인 방법과 사용시 발생할 수 있는 문제점에 대해 다룹니다. 1. 간단한 안드로이드 구글맵 예제 동작시키기 2. Google Maps Android API 예제 코드 설명 3. 관련 포스
webnautes.tistory.com
원하는 위치 찍고싶을때 위도 경도 찾아서 복사해서 onMapReady에 넣으면됨
xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
//프래그먼트 추가함
<fragment
android:id="@+id/googleMap"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment"/>
</LinearLayout>
main.java
package com.example.googlemapexample;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.app.FragmentManager;
import android.os.Bundle;
import com.google.android.gms.maps.CameraUpdate;
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.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
private FragmentManager fragmentManager;
private MapFragment mapFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//임포트가 잘못되어있다는 오류뜰때 위에 임포트 지워주고 alt_enter해서 해결하면됨
fragmentManager = getFragmentManager();
mapFragment = (MapFragment)fragmentManager.findFragmentById(R.id.googleMap);
mapFragment.getMapAsync(this);
}
//구글맵 준비되면 호출하는거
@Override
public void onMapReady(@NonNull GoogleMap googleMap) {
//지도핀을 꼽는 이미지 리소스
//위도 경도 의미
LatLng location = new LatLng(37.240204, 131.869422); //독도의 위치
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.title("독도");
markerOptions.snippet("독도는 한국 땅이다.");
markerOptions.position(location);
googleMap.addMarker(markerOptions);
//줌설정, 숫자 크면 줌 올라감,딱딱하게 딱 화면만보여줌
//googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location,16));
//마커가 움직이면서 애니메이션 효과 일어나면서 카메라가 위치를 향해 달려감
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(location,16));
}
}
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.googlemapexample">
<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/Theme.GoogleMapExample">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyAZw3GwEnWo1KU3tDZMwaSAoWS5ViUqd1o"/>
</application>
</manifest>
gradle.app
plugins {
id 'com.android.application'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.googlemapexample"
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'com.google.android.gms:play-services-maps:17.0.1'
implementation 'com.google.android.gms:play-services-location:18.0.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
결과
moveCamera일때
AnimateCamera일때
좀더 스무스 하군..
'✍2021,2022 > app(android studio)' 카테고리의 다른 글
앱만들기.25(startActivityForresult) (0) | 2021.07.29 |
---|---|
앱만들기.24(인스타 하단바 bottom navigation view) (0) | 2021.07.29 |
앱만들기.22(뒤로가기 두번눌러 앱종료) (0) | 2021.07.21 |
앱만들기.21(MP3) (0) | 2021.07.21 |
앱만들기.19 (spinner드롭다운) (0) | 2021.07.20 |