- μ΄νμκ°
- μλλ‘μ΄λ μ€νλμ€
- λ‘κ·ΈμΈ νλ©΄
- Firebase μ°λ
- νμκ°μ νλ©΄ ꡬμ±
- λ‘κ·ΈμΈ νλ©΄ ꡬμ±
- μ¬μ©μ μ 보 λͺ¨λΈ ν΄λμ€
- 맡λ°μ€ SDK μ€μΉ
- getRoute λ©μλ
- λͺ©μ μ§ μ€μ λ°©λ²
- λ‘κ·ΈμΈ νλ©΄
- κ°μ λ°©μ
DK GUIDE λ νκ΅μ§λ¦¬μ μ΅μμΉ μμ μ¬μ©μλ₯Ό μν λ€λΉκ²μ΄μ κΈ°λ° μ±μΌλ‘μ¨ μ¦κ°νμ€(AR) κΈ°μ μ νμ©νμ¬ μ¬μ©μκ° νλ©΄ μ§λλ₯Ό νμ©ν κΈΈμ°ΎκΈ° λ°©μλ³΄λ€ λ³΄λ€ μ½κ² κΈΈμ μ°Ύμ μ μλλ‘ λμμ£Όλ κ°μ΄λμ±μ΄λ€.
λν μ¬μ©μλ ν΄λΉ λͺ©μ μ§μ λμ°©μ ν΄λΉ μμΉλ 건물μ λν 짧μ μ 보λ₯Ό AR μ λλ©μ΄μ μΊλ¦ν°λ₯Ό ν΅ν΄ μμ±μΌλ‘ μ»μ μ μλ€.
μμ°μμ : https://youtu.be/Nrat-Xba-c8
AR-Navigation μμ° μμ : https://youtu.be/BZWZdZ82R8w
μ΄ν리μΊμ΄μ
μκ° pdf : μ΅μμ ν΄λ poster.pdf
맡λ°μ€μμ μ 곡νλ νν 리μΌμ μ€μ¬μΌλ‘ μ μλμλ€. μ°Έκ³ : https://docs.mapbox.com/help/tutorials/android-navigation-sdk/
λ¨κ΅λ 2021 Echo+ κ²½μ§λν μ°μμ
Firebase λ₯Ό μ΄μ©νμ¬ μ΄λ©μΌ νμκ°μ & λ‘κ·ΈμΈ κΈ°λ₯ ꡬν
λ‘κ·ΈμΈμ΄λ νμκ°μ μ 보λ₯Ό κ΄λ¦¬νκΈ° μν΄ μλ² κ΅¬μΆμ ν΄μΌνμ§λ§ Firebase λ μλ² κ΅¬μΆ κ³Όμ μ 건λλκ³ μ체μ μΌλ‘ λ§λ€μ΄μ§ ν΄λΌμ°λ μλ²λ₯Ό λΉλ €μ κ·Έκ³³μ λ°μ΄ν°λ² μ΄μ€λ‘ νμκ°μ κ³Ό λ‘κ·ΈμΈ κ³Όμ μ κ°νΈνκ² ν΄μ£Όλ μΌλ ¨μ κ³Όμ μ μν¬ν ν΄μ£Όλ ν΄λΌμ°λ νλ«νΌμ΄λ€.
- μλλ‘μ΄λ μ€νλμ€ λ΄ Tools - firebase - Authenticate λ₯Ό ν΅ν΄ Authenticate using a custom authenticate system μ ν΄λ¦ν ν μ νλ‘μ νΈλ₯Ό μμ±νλ€.
- μλλ‘μ΄λ μ€νλμ€λ‘ λμμμ Add the Firebase Authentication SDK to your app ν΄λ¦ ν Firebase κ΄λ ¨ μ’ μμ±μ μΆκ°ν΄μ€λ€.
- Realtime database μμ database κ΄λ ¨ ν μ΄λΈ 곡κ°μ μμ±ν΄μ£ΌκΈ° μν΄ μ¬μ§κ³Ό κ°μ΄ κ·μΉμμ λ€μκ³Ό κ°μ΄ μ½κΈ° κΆνκ³Ό μ°κΈ° κΆνμ true μ€μ μ ν΅ν΄ κ°μ Έμ¨λ€.
- Realtime database μ’ μμ±μ μΆκ°νκΈ° μν΄ Add the realtime Database to your app μ ν΄λ¦νμ¬μ€λ€.
- RegisterActivity λΌλ μ΄λ¦μ empty activity λ₯Ό μμ±ν΄μ€ ν activity_register.xml μμμ΄λ©μΌκ³Ό λΉλ°λ²νΈλ₯Ό μ λ ₯ν μ μλ EditText μ κ°μ μ μ 보λ₯Ό λ°μ΄ν°λ² μ΄μ€μ μ λ¬ν΄μ£Όλ 'κ°μ νκΈ°' λ²νΌμ νλ©΄μ μΆκ°ν΄μ€λ€.
<?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=".RegisterActivity">
<EditText android:id="@+id/et_email"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_marginTop="240dp"
android:ems="10"
android:hint="μ΄λ©μΌμ μ
λ ₯νμΈμ"
android:inputType="textEmailAddress"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText android:id="@+id/et_pwd"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_marginTop="32dp"
android:ems="10"
android:hint="λΉλ°λ²νΈλ₯Ό μ
λ ₯νμΈμ.(6μ리 μ΄μ)"
android:inputType="textPassword"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et_email" />
<Button android:id="@+id/btn_register"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="νμκ°μ
"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
- RegisterActivity μμ νμκ°μ κ³Ό κ΄λ ¨ν Firebase μΈμ¦μ²λ¦¬ μλ²μ μ°λμμΌμ€ μ μλ κ°μ²΄ νμκ°μ λ²νΌμ΄ ν΄λ¦λ λμ μ΅μ μ²λ¦¬ λ±μ μ½λλ₯Ό λ€μκ³Ό κ°μ΄ μΆκ°ν΄μ€λ€. νμκ°μ μ μ±κ³΅ν μ λ‘κ·ΈμΈ νλ©΄μΌλ‘ λμ΄κ°λλ‘ μ€μ λμλ€.
private FirebaseAuth mFirebaseAuth; //Firebase μΈμ¦μ²λ¦¬
private DatabaseReference mDatabaseRef; //μ€μκ° λ°μ΄ν°λ² μ΄μ€ -> μλ²μ μ°λμν¬μμλ κ°μ²΄
private EditText mEtEmail, mEtPwd; //νμκ°μ
μ
λ ₯νλ
private Button mBtnRegister; //νμκ°μ
λ²νΌ
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mFirebaseAuth = FirebaseAuth.getInstance(); //firebase μ΄μ©μ€λΉ λ
mDatabaseRef = FirebaseDatabase.getInstance().getReference("mapbox");
mEtEmail = findViewById(R.id.et_email);
mEtPwd = findViewById(R.id.et_pwd);
mBtnRegister = findViewById(R.id.btn_register);
}
- νμκ°μ λ²νΌ ν΄λ¦ μ μ΄λ²€νΈ μ²λ¦¬λ₯Ό μν λ©μλλ₯Ό onCreate λ©μλμ μΆκ°ν΄μ€λ€.
//νμκ°μ
λ²νΌμ΄ ν΄λ¦λ λμ μ΅μ
μ²λ¦¬
mBtnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String strEmail = mEtEmail.getText().toString();
String strPwd = mEtPwd.getText().toString();
//μλ¬΄λ° μ λ³΄κ° μ
λ ₯λμ§ μμ μνμμ λ²νΌ ν΄λ¦ μ μ€λ₯κ° λ¨λ κ²μ λ°©μ§
if(strEmail.getBytes().length <=0 || strPwd.getBytes().length <=0){
Toast.makeText(RegisterActivity.this ,"κ°μ
μ 보λ₯Ό μ
λ ₯νμΈμ.", Toast.LENGTH_SHORT).show();
}else{
//FirebaseAuth μ§ν
mFirebaseAuth.createUserWithEmailAndPassword(strEmail, strPwd).addOnCompleteListener(RegisterActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NotNull Task<AuthResult> task) {
//νμκ°μ
μ΄ μ΄λ£¨μ΄μ‘μλ μ²λ¦¬
if(task.isSuccessful()){
FirebaseUser firebaseUser = mFirebaseAuth.getCurrentUser();
UserAccount account = new UserAccount();
account.setIdToken(firebaseUser.getUid());
account.setEmailId(firebaseUser.getEmail());
account.setPassword(strPwd);
//setValue λ databaseμ μ½μ
νλ νμ
mDatabaseRef.child("UserAccount").child(firebaseUser.getUid()).setValue(account);
Toast.makeText(RegisterActivity.this, "νμκ°μ
μ μ±κ³΅νμ
¨μ΅λλ€.", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
startActivity(intent);
} else {
Toast.makeText(RegisterActivity.this, "νμκ°μ
μ μ€ν¨νμ
¨μ΅λλ€.", Toast.LENGTH_SHORT).show();
}
}
});}
}
});
- LoginActivity λΌλ μ΄λ¦μ empty activity λ₯Ό μμ±ν΄μ€ ν activity_login.xml μμ μ΄λ©μΌκ³Ό λΉλ°λ²νΈλ₯Ό μ λ ₯ν μ μλ EditText μ λ‘κ·ΈμΈ λ²νΌκ³Ό νμκ°μ νλ©΄μΌλ‘ λ겨주λ λ²νΌμ ꡬνν΄μ€λ€.
<?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=".LoginActivity">
<EditText android:id="@+id/et_email"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_marginTop="200dp"
android:ems="10"
android:hint="μ΄λ©μΌμ μ
λ ₯νμΈμ"
android:inputType="textEmailAddress"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText android:id="@+id/et_pwd"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_marginTop="32dp"
android:ems="10"
android:hint="λΉλ°λ²νΈλ₯Ό μ
λ ₯νμΈμ"
android:inputType="textPassword"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et_email" />
<Button android:id="@+id/btn_login"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginEnd="150dp"
android:text="λ‘κ·ΈμΈ"
app:layout_constraintEnd_toEndOf="@+id/et_pwd"
app:layout_constraintStart_toStartOf="@+id/et_pwd"
app:layout_constraintTop_toBottomOf="@+id/et_pwd" />
<Button android:id="@+id/btn_register"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="150dp"
android:layout_marginTop="24dp"
android:text="νμκ°μ
"
app:layout_constraintEnd_toEndOf="@+id/et_pwd"
app:layout_constraintStart_toStartOf="@+id/et_pwd"
app:layout_constraintTop_toBottomOf="@+id/et_pwd" />
</androidx.constraintlayout.widget.ConstraintLayout>
- LoginActivity μ λ‘κ·ΈμΈκ³Ό κ΄λ ¨ν Firebase μΈμ¦μ²λ¦¬, λ‘κ·ΈμΈ λ²νΌ ν΄λ¦μ MainActivity νλ©΄μΌλ‘ λ겨주λ μ΅μ μ²λ¦¬μ νμκ°μ λ²νΌ ν΄λ¦ μ νμκ°μ νλ©΄μΌλ‘ λ겨주λ μ΅μ μ²λ¦¬ λ±μ μ½λλ₯Ό μΆκ°ν΄μ€λ€.
private FirebaseAuth mFirebaseAuth; //Firebase μΈμ¦μ²λ¦¬
private DatabaseReference mDatabaseRef; //μ€μκ° λ°μ΄ν°λ² μ΄μ€ -> μλ²μ μ°λμν¬μμλ κ°μ²΄
private EditText mEtEmail, mEtPwd; //λ‘κ·ΈμΈ μ
λ ₯νλ
EditText editText1, editText2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mFirebaseAuth = FirebaseAuth.getInstance(); //firebase μ΄μ©μ€λΉ λ
mDatabaseRef = FirebaseDatabase.getInstance().getReference("mapbox");
mEtEmail = findViewById(R.id.et_email);
mEtPwd = findViewById(R.id.et_pwd);
Button btn_login = findViewById(R.id.btn_login);
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//λ‘κ·ΈμΈ μμ²
String strEmail = mEtEmail.getText().toString();
String strPwd = mEtPwd.getText().toString();
if(strEmail.getBytes().length <= 0 || strPwd.getBytes().length <= 0){
Toast.makeText(LoginActivity.this, "λ‘κ·ΈμΈ μ 보λ₯Ό μ
λ ₯νμΈμ", Toast.LENGTH_SHORT).show();
}else {
mFirebaseAuth.signInWithEmailAndPassword(strEmail, strPwd).addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete( @NotNull Task<AuthResult> task) {
if(task.isSuccessful()){
//λ‘κ·ΈμΈ μ±κ³΅μ MainAcitivityλ‘ μ°κ²°
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
//finish(); //νμ¬μ‘ν°λΉν° νκ΄΄
} else {
Toast.makeText(LoginActivity.this , "λ‘κ·ΈμΈ μ€ν¨", Toast.LENGTH_SHORT).show();
}
}
});}
}
});
Button btn_register = findViewById(R.id.btn_register);
btn_register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//νμκ°μ
λ²νΌμ λλ μ λμ μ²λ¦¬ -> νμκ°μ
μ°½μΌλ‘ μ΄λ
Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(intent);
}
});
}
}
- λ‘κ·ΈμΈ μλ£ ν λ€λ‘κ°κΈ° λ²νΌμ λλ μ λ λ€μ λ‘κ·ΈμΈ νλ©΄μΌλ‘ λμ΄κ°λ κ²μ λ§μμ£ΌκΈ° μν΄ MainActivity μ λ€μκ³Ό κ°μ μ½λλ₯Ό μΆκ°νμ¬ μ€λ€.
//λ€λ‘κ°κΈ° λ²νΌμ λ§μμ λ‘κ·ΈμΈ νλ©΄μΌλ‘ λμ΄κ°λ μ€λ₯λ₯Ό ν΄κ²°
@Override
public void onBackPressed(){
//super.onBackPressed();
}
- μ± μ€νμ LoginActivity κ° κ°μ₯ λ¨Όμ μ€νλλλ‘ νμ¬ λ‘κ·ΈμΈ νλ©΄μ΄ κ°μ₯ λ¨Όμ νλ©΄μ λμμ§λλ‘ μ€μ νκΈ° μν΄ manifests μ intent-filter λ₯Ό λ€μκ³Ό κ°μ΄ LoginActivity μμΌλ‘ μ°κ²°μμΌμ€λ€.
<activity android:name=".LoginActivity"
android:theme="@style/Theme.AppCompat">
<intent-filter> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter></activity>
- MainActivity νλ©΄μ μ΅μ λ©λ΄μμ λ‘κ·Έμμ λ²νΌ ν΄λ¦ μ λ‘κ·ΈμΈ νλ©΄μΌλ‘ λλμ κ°κ² ν΄μ£ΌκΈ° μν΄ menuoptions.xml κ³Ό MainActivity μ λ€μκ³Ό κ°μ΄ μ½λλ₯Ό μΆκ°ν΄μ€λ€.
<item
android:title="λ‘κ·Έμμ"
android:id="@+id/logout"/>
//μ± μνλ° λ©λ΄λ₯Ό λ©μΈμ΄λ²€νΈμ μ°λμμΌμ£Όλ λ©μλ
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.menuoptions, menu);
return true;}
//ν΄λ¦ μ΄λ²€νΈ μ²λ¦¬
//λ©λ΄ ν΄λ¦μ μ νν λ©λ΄λ₯Ό νΈμΆνκ² νλ λ©μλ
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.logout:
mFirebaseAuth.signOut();
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
Toast.makeText(MainActivity.this,"λ‘κ·Έμμ λμμ΅λλ€", Toast.LENGTH_SHORT).show();
}
return false;
}
- UserAccount λΌλ μ΄λ¦μ μ¬μ©μ κ³μ μ 보λ₯Ό κ°μ Έμ€λ ν΄λμ€ μμ± ν getter μ setter λ₯Ό μ΄μ©νμ¬ λ€μκ³Ό κ°μ΄ μ½λλ₯Ό μμ±νλ€.
package com.example.mapbox;
// νμκ°μ
μ¬μ©μ κ³μ μ 보 λͺ¨λΈ ν΄λμ€
public class UserAccount {
private String idToken; //Firebase Uid(κ³ μ ν ν° μ 보)
private String emailId; // μ΄λ©μΌμμ΄λ
private String password; // λΉλ°λ²νΈ
//firebase λ λΉ μμ±μλ₯Ό λ§λ€μ΄μ€μΌ μ€λ₯κ° λμ§ μλλ€.
public UserAccount() {
}
public String getIdToken() {
return idToken;
}
public void setIdToken(String idToken) {
this.idToken = idToken;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
맡λ°μ€λ 3μ°¨μ λμ μ§λ μΉμλΉμ€ κ°λ°μ μ§μνκ³ λ§μΆ€ν λμμΈ λ§΅μ μν μ€ν μμ€ λ§€ν νλ«νΌμ΄λ€. 맡λ°μ€λ₯Ό μ΄μ©ν΄ λμ μ§λ, 3μ°¨μ μ§λ, λ€λΉκ²μ΄μ , μ¦κ°νμ€ λ€μν κ²μ λ§λ€ μ μλ€.
맡λ°μ€λ₯Ό νμ©νμ¬ λ€λΉκ²μ΄μ κΈ°λ₯μ ꡬννκΈ° μν΄μλ μλλ‘μ΄λ μ€νλμ€μ 맡λ°μ€ SDK λ° λ§΅λ°μ€ λ€λΉκ²μ΄μ SDK λκ°μ§λ₯Ό μ€μΉν΄μΌνλ€.
μ°Έκ³ : https://docs.mapbox.com/help/tutorials/android-navigation-sdk/ https://docs.mapbox.com/android/maps/guides/install/ (맡λ°μ€ SDK μ€μΉ λ° λ§΅λ°μ€ λ€λΉκ²μ΄μ νν 리μΌ)
-
λ€μκ³Ό κ°μ΄ MAPBOX Account νμ΄μ§μ μ μνμ¬ λ‘κ·ΈμΈ ν λ€μκ³Ό κ°μ΄ ACCESS ν ν°μ 볡μ¬νλ€.
-
λͺ¨λμμ€μ build.gradle μ dependencies μ λ€μκ³Ό κ°μ΄ μ΅μ λ²μ μ SDK λκ°λ₯Ό μΆκ°ν΄μ€λ€.
implementation 'com.mapbox.mapboxsdk:mapbox-android-sdk:9.6.1'
implementation 'com.mapbox.mapboxsdk:mapbox-android-navigation-ui:0.42.6'
- νλ‘μ νΈ μμ€μ build.gradle μ repositories μ λ€μμ μΆκ°ν΄μ€λ€.
maven { url 'https://mapbox.bintray.com/mapbox' }
- string.xml μ 볡μ¬ν access ν ν°μ λΆμ¬λ£λλ€.
<string name="mapbox_access_token" translatable="false">MAMBOX_ACCESS_TOKEN</string>
- Manifest.xml μ 맡λ°μ€ κ΄λ ¨ κΆνμ μΆκ°ν΄μ£ΌκΈ° μν΄μ λ€μμ μΆκ°ν΄μ€λ€.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
- μ ν리μΌμ΄μ μ΄ μ΄κΈ°ν λ λ μ¬μ©λ μ§λ μ€νμΌμ ν¬ν¨νμ¬ MapView μ μμ±κ³Ό λ€λΉκ²μ΄μ μ€ννΈ λ²νΌμ μΆκ°ν΄μ£ΌκΈ° μν΄ activity_main.xml μ λ€μμ μΆκ°ν΄μ€λ€.
<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
mapbox:mapbox_cameraTargetLat="38.9098"
mapbox:mapbox_cameraTargetLng="-77.0295"
mapbox:mapbox_cameraZoom="15.5" />
//2dλ€λΉκ²μ΄μ
μ€ννΈ λ²νΌ
<Button
android:id="@+id/startButton"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:layout_marginBottom="20dp"
android:background="@color/mapboxGrayLight"
android:enabled="false"
android:text="2D"
android:textColor="@color/mapboxWhite"
mapbox:layout_constraintRight_toRightOf="parent"
mapbox:layout_constraintBottom_toBottomOf="parent" />
- 맡λ°μ€ μ§λλ₯Ό μ¬μ©νκΈ° μν΄ MainActivity μ λ€μμ μΆκ°ν΄μ€λ€.
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import com.mapbox.geojson.Feature;
import com.mapbox.geojson.Point;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.maps.Style;
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback, MapboxMap.OnMapClickListener, PermissionsListener {
private MapView mapView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Mapbox.getInstance(this, getString(R.string.access_token));
setContentView(R.layout.activity_main);
mapView = findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(@NonNull final MapboxMap mapboxMap) {
mapboxMap.setStyle(Style.LIGHT, new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
// Map is set up and the style has loaded. Now you can add data or make other map adjustments
}
});
}
});
}
// Add the mapView's own lifecycle methods to the activity's lifecycle methods
@Override
public void onStart() {
super.onStart();
mapView.onStart();
}
@Override
public void onResume() {
super.onResume();
mapView.onResume();
}
@Override
public void onPause() {
super.onPause();
mapView.onPause();
}
@Override
public void onStop() {
super.onStop();
mapView.onStop();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
getRoute λ μ¬μ©μμ μμΉμ λͺ©μ μ§ μ¬μ΄μ μ’νλ₯Ό ꡬν΄μ£Όλ λ©μλμ΄λ€. μλλ λ보 μ μ© κΈΈμ°ΎκΈ° λ©μλμ΄λ€. μ°¨λμ© κΈΈμ°ΎκΈ°λ‘ μ€μ μ λ°κΎΈλ €λ©΄ .profile(DirectionsCriteria.PROFILE_WALKING) μ΄ λΆλΆμ μ§μμ£Όκ±°λ μ£Όμμ²λ¦¬νλ€.
//λ΄ μμΉμ λͺ©μ μ§ μ¬μ΄μ 루νΈλ₯Ό ꡬν΄μ£Όλ ν¨μ
//νμ¬ λ보 κΈΈμ°ΎκΈ°λ‘ μ€μ ν΄λμκ±°μ
private void getRoute(Point origin, Point destination) {
NavigationRoute.builder(this)
.accessToken(Mapbox.getAccessToken())
.profile(DirectionsCriteria.PROFILE_WALKING) //λ보 κΈΈμ°ΎκΈ° -> μ°¨λ κΈΈμ°ΎκΈ°μ μ£Όμμ²λ¦¬
.origin(origin)
.destination(destination)
.build()
.getRoute(new Callback<DirectionsResponse>() {
@Override
public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
// You can get the generic HTTP info about the response
Log.d(TAG, "Response code: " + response.code());
if (response.body() == null) {
Log.e(TAG, "No routes found, make sure you set the right user and access token.");
return; } else if (response.body().routes().size() < 1) {
Log.e(TAG, "No routes found");
return; }
currentRoute = response.body().routes().get(0);
if (navigationMapRoute != null){
navigationMapRoute.removeRoute();
} else {
navigationMapRoute = new NavigationMapRoute(null, mapView, mapboxMap);
}
navigationMapRoute.addRoute(currentRoute);
}
@Override
public void onFailure(Call<DirectionsResponse> call, Throwable throwable) {
Log.e(TAG, "Error: " + throwable.getMessage());
}
});
}
MAPBOX νν 리μΌμ 맡μ ν΄λ¦ν κ³³μ μ’νλ₯Ό λͺ©μ μ§λ‘ λ°μμ€κ³ μ¬μ©μμμ 루νΈλ₯Ό ꡬν΄μ£Όλ λ°©μμ΄μλ€. νμ§λ§ DK GIUDE λ λ¨κ΅λ μ μ© κ°μ΄λ μ±μ΄λ―λ‘ νμ λ©λ΄λ₯Ό νμ©νμ¬ μμΉλ₯Ό νμ νμλ€.
-
GeoPlaner λ₯Ό νμ©νμ¬ λ΄κ° μνλ κ³³μ μ’νλ₯Ό 맡ν΄λ¦μ ν΅ν΄ λ³΄λ€ μΈμΈνκ² μ»μ΄ μ¬ μ μλ€.
-
κ° κ±΄λ¬Όμ μ»μ΄μ¨ μ’νλ₯Ό κ°κ° μ§μ μ λ ₯μΌλ‘ λ£μ΄μ€λ€. κ° if λ¬Έμλ 건물μ ν΄λΉνλ μ’νκ°μ destinationPoint λ‘ μ λ¬μ νμκ³ getRoute λ©μλλ₯Ό νμ©νμ¬ μ¬μ©μμ μμΉλ₯Ό λνλ΄λ originPoint μ destinationPoint μ¬μ΄μ λ보경λ‘λ₯Ό ꡬν΄μ€λ€. μλ μ½λλ λ¨κ΅λνκ΅ μ£½μ μΊ νΌμ€μ 건물 μ€μ¬μΌλ‘ μ½λ©μ νκ²μ΄λ€.
//button1 μ λͺ©μ μ§μ€μ λ²νΌ
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
final PopupMenu popupMenu = new PopupMenu(getApplicationContext(), v);
getMenuInflater().inflate(R.menu.popup, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
//μ 1곡νκ΄
if (item.getItemId() == R.id.action_menu1){
destinationPoint = Point.fromLngLat(127.12584,37.32119);
originPoint = Point.fromLngLat(Lo,La);
getRoute(originPoint, destinationPoint);
button.setEnabled(true);
button.setBackgroundResource(R.color.mapboxBlue);
}
//μ 2곡νκ΄
else if (item.getItemId() == R.id.action_menu2){
destinationPoint = Point.fromLngLat(127.12629,37.32080);
originPoint = Point.fromLngLat(Lo,La);
getRoute(originPoint, destinationPoint);
button.setEnabled(true);
button.setBackgroundResource(R.color.mapboxBlue);
}
//μ 3곡νκ΄
else if (item.getItemId() == R.id.action_menu3){
destinationPoint = Point.fromLngLat(127.12676,37.32043);
originPoint = Point.fromLngLat(Lo,La);
getRoute(originPoint, destinationPoint);
button.setEnabled(true);
button.setBackgroundResource(R.color.mapboxBlue);
}
//μ¬νκ³Όνκ΄
else if (item.getItemId() == R.id.action_menu4){
destinationPoint = Point.fromLngLat(127.12564,37.32137);
originPoint = Point.fromLngLat(Lo,La);
getRoute(originPoint, destinationPoint);
button.setEnabled(true);
button.setBackgroundResource(R.color.mapboxBlue);
}
//μννΈμ¨μ΄ ICTκ΄
else if (item.getItemId() == R.id.action_menu5){
destinationPoint = Point.fromLngLat(127.12753,37.32274);
originPoint = Point.fromLngLat(Lo,La);
getRoute(originPoint, destinationPoint);
button.setEnabled(true);
button.setBackgroundResource(R.color.mapboxBlue);
}
//λ―Έλμ΄μΌν°
else if (item.getItemId() == R.id.action_menu6){
destinationPoint = Point.fromLngLat(127.12753,37.32242);
originPoint = Point.fromLngLat(Lo,La);
getRoute(originPoint, destinationPoint);
button.setEnabled(true);
button.setBackgroundResource(R.color.mapboxBlue);
}
//λ²μ κ΄
else if (item.getItemId() == R.id.action_menu7){
destinationPoint = Point.fromLngLat(127.12641,37.32196);
originPoint = Point.fromLngLat(Lo,La);
getRoute(originPoint, destinationPoint);
button.setEnabled(true);
button.setBackgroundResource(R.color.mapboxBlue);
}
//μΈλ¬Έκ΄
else if (item.getItemId() == R.id.action_menu8){
destinationPoint = Point.fromLngLat(127.12896,37.32178);
originPoint = Point.fromLngLat(Lo,La);
getRoute(originPoint, destinationPoint);
button.setEnabled(true);
button.setBackgroundResource(R.color.mapboxBlue);
}
//μκ²½κ΄
else if (item.getItemId() == R.id.action_menu9){
destinationPoint = Point.fromLngLat(127.12894,37.32227);
originPoint = Point.fromLngLat(Lo,La);
getRoute(originPoint, destinationPoint);
button.setEnabled(true);
button.setBackgroundResource(R.color.mapboxBlue);
}
//μ¬λ²κ΄
else if (item.getItemId() == R.id.action_menu10){
destinationPoint = Point.fromLngLat(127.12897,37.32272);
originPoint = Point.fromLngLat(Lo,La);
getRoute(originPoint, destinationPoint);
button.setEnabled(true);
button.setBackgroundResource(R.color.mapboxBlue);
}
//νλΉκ΄
else if (item.getItemId() == R.id.action_menu11){
destinationPoint = Point.fromLngLat(127.12827,37.32047);
originPoint = Point.fromLngLat(Lo,La);
getRoute(originPoint, destinationPoint);
button.setEnabled(true);
button.setBackgroundResource(R.color.mapboxBlue);
}
//ν΄κ³κΈ°λ
μ€μλμκ΄
else if (item.getItemId() == R.id.action_menu12){
destinationPoint = Point.fromLngLat(127.12746,37.32116);
originPoint = Point.fromLngLat(Lo,La);
getRoute(originPoint, destinationPoint);
button.setEnabled(true);
button.setBackgroundResource(R.color.mapboxBlue);
}
//λ²νκ΄, λνμλ
else if (item.getItemId() == R.id.action_menu13){
destinationPoint = Point.fromLngLat(127.12921,37.32107);
originPoint = Point.fromLngLat(Lo,La);
getRoute(originPoint, destinationPoint);
button.setEnabled(true);
button.setBackgroundResource(R.color.mapboxBlue);
}
//κ΅μ κ΄
else if (item.getItemId() == R.id.action_menu14){
destinationPoint = Point.fromLngLat(127.12717,37.31919);
originPoint = Point.fromLngLat(Lo,La);
getRoute(originPoint, destinationPoint);
button.setEnabled(true);
button.setBackgroundResource(R.color.mapboxBlue);
}
//μ’
ν©μ€νλ
else if (item.getItemId() == R.id.action_menu15){
destinationPoint = Point.fromLngLat(127.12577,37.32014);
originPoint = Point.fromLngLat(Lo,La);
getRoute(originPoint, destinationPoint);
button.setEnabled(true);
button.setBackgroundResource(R.color.mapboxBlue);
}
//κ³°μ
else if (item.getItemId() == R.id.action_menu16){
destinationPoint = Point.fromLngLat(127.12892,37.31996);
originPoint = Point.fromLngLat(Lo,La);
getRoute(originPoint, destinationPoint);
button.setEnabled(true);
button.setBackgroundResource(R.color.mapboxBlue);
}
//μ λ¬Έ
else if (item.getItemId() == R.id.action_menu17){
destinationPoint = Point.fromLngLat(127.12547,37.32346);
originPoint = Point.fromLngLat(Lo,La);
getRoute(originPoint, destinationPoint);
button.setEnabled(true);
button.setBackgroundResource(R.color.mapboxBlue);
}
//체μ‘κ΄
else if (item.getItemId() == R.id.action_menu18){
destinationPoint = Point.fromLngLat(127.13213,37.31936);
originPoint = Point.fromLngLat(Lo,La);
getRoute(originPoint, destinationPoint);
button.setEnabled(true);
button.setBackgroundResource(R.color.mapboxBlue);
}
//λμ΄λμ₯
else if (item.getItemId() == R.id.action_menu19){
destinationPoint = Point.fromLngLat(127.13300,37.32080);
originPoint = Point.fromLngLat(Lo,La);
getRoute(originPoint, destinationPoint);
button.setEnabled(true);
button.setBackgroundResource(R.color.mapboxBlue);
}
//무μ©κ΄
else if (item.getItemId() == R.id.action_menu20){
destinationPoint = Point.fromLngLat(127.12724,37.31585);
originPoint = Point.fromLngLat(Lo,La);
getRoute(originPoint, destinationPoint);
button.setEnabled(true);
button.setBackgroundResource(R.color.mapboxBlue);
}
//μ§λ¦¬κ΄
else if (item.getItemId() == R.id.action_menu21){
destinationPoint = Point.fromLngLat(127.12682,37.31479);
originPoint = Point.fromLngLat(Lo,La);
getRoute(originPoint, destinationPoint);
button.setEnabled(true);
button.setBackgroundResource(R.color.mapboxBlue);
}
//μ
λΉν
else if (item.getItemId() == R.id.action_menu22){
destinationPoint = Point.fromLngLat(127.12698,37.31567);
originPoint = Point.fromLngLat(Lo,La);
getRoute(originPoint, destinationPoint);
button.setEnabled(true);
button.setBackgroundResource(R.color.mapboxBlue);
}
//μ§νμ¬
else if (item.getItemId() == R.id.action_menu23){
destinationPoint = Point.fromLngLat(127.12695,37.31667);
originPoint = Point.fromLngLat(Lo,La);
getRoute(originPoint, destinationPoint);
button.setEnabled(true);
button.setBackgroundResource(R.color.mapboxBlue);
}
//μ£½μ μΉκ³Όλ³μ
else if (item.getItemId() == R.id.action_menu24){
destinationPoint = Point.fromLngLat(127.12513,37.32187);
originPoint = Point.fromLngLat(Lo,La);
getRoute(originPoint, destinationPoint);
button.setEnabled(true);
button.setBackgroundResource(R.color.mapboxBlue);
}
//λ
Έμ²λ§λΉ, νμ΄μ₯
else if (item.getItemId() == R.id.action_menu25){
destinationPoint = Point.fromLngLat(127.12724,37.31988);
originPoint = Point.fromLngLat(Lo,La);
getRoute(originPoint, destinationPoint);
button.setEnabled(true);
button.setBackgroundResource(R.color.mapboxBlue);
}
//μμ
κ΄
else if (item.getItemId() == R.id.action_menu26){
destinationPoint = Point.fromLngLat(127.12935,37.31918);
originPoint = Point.fromLngLat(Lo,La);
getRoute(originPoint, destinationPoint);
button.setEnabled(true);
button.setBackgroundResource(R.color.mapboxBlue);
}
//λ―Έμ κ΄
else if (item.getItemId() == R.id.action_menu27){
destinationPoint = Point.fromLngLat(127.13088,37.31983);
originPoint = Point.fromLngLat(Lo,La);
getRoute(originPoint, destinationPoint);
button.setEnabled(true);
button.setBackgroundResource(R.color.mapboxBlue);
}
//νν¬κ³΅μ
else if (item.getItemId() == R.id.action_menu28){
destinationPoint = Point.fromLngLat(127.13065,37.32082);
originPoint = Point.fromLngLat(Lo,La);
getRoute(originPoint, destinationPoint);
button.setEnabled(true);
button.setBackgroundResource(R.color.mapboxBlue);
}
return false;
}
});
//νμ
λ©λ΄λ₯Ό 보μ΄κ² νλκ±°
popupMenu.show();
}
}
GPS λ₯Ό νμ©νμ¬ λμ μμΉλ₯Ό 맡μ νμν΄μ£Όκ³ originPoint μ λ΄ νμ¬ μμΉμ μλ, κ²½λ κ°μ μ λ¬ν΄μ£Όλ λ°©μμ μ΄μ©νμλ€.
- μ¬μ©μμ μμΉλ₯Ό μ§λμ νμ νκΈ° μν λ©μλ
//User μ μμΉλ₯Ό λνλ΄μ£Όλ λ©μλ
@SuppressWarnings( {"MissingPermission"})
private void enableLocationComponent(@NonNull Style loadedMapStyle) {
// Check if permissions are enabled and if not request
if (PermissionsManager.areLocationPermissionsGranted(this)) {
// Activate the MapboxMap LocationComponent to show user location
// Adding in LocationComponentOptions is also an optional parameter // Set the LocationComponent activation options
locationComponent = mapboxMap.getLocationComponent();
locationComponent.activateLocationComponent(LocationComponentActivationOptions.builder(this, loadedMapStyle).build());
locationComponent.setLocationComponentEnabled(true);
// Set the component's camera mode
locationComponent.setCameraMode(CameraMode.TRACKING);
locationComponent.setRenderMode(RenderMode.COMPASS);
initLocationEngine();
} else {
permissionsManager = new PermissionsManager(this);
permissionsManager.requestLocationPermissions(this);
}
}
@SuppressLint("MissingPermission")
private void initLocationEngine() {
Log.e(TAG,"initLocationEngine μ€ν");
locationEngine = LocationEngineProvider.getBestLocationEngine(this);
LocationEngineRequest request = new LocationEngineRequest.Builder(DEFAULT_INTERVAL_IN_MILLISECONDS)
.setPriority(LocationEngineRequest.PRIORITY_HIGH_ACCURACY)
.setMaxWaitTime(DEFAULT_MAX_WAIT_TIME).build();
locationEngine.requestLocationUpdates(request, callback, getMainLooper());
locationEngine.getLastLocation(callback);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
permissionsManager.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
@Override
public void onExplanationNeeded(List<String> permissionsToExplain) {
Toast.makeText(this, R.string.user_location_permission_explanation, Toast.LENGTH_LONG).show();
}
@Override
public void onPermissionResult(boolean granted) {
if (granted) {
enableLocationComponent(mapboxMap.getStyle());
} else {
Toast.makeText(this, R.string.user_location_permission_not_granted, Toast.LENGTH_LONG).show();
finish();
}
}
- μ¬μ©μμ νμ¬μμΉ μλ, κ²½λκ°μ originPoint μ μ λ¬ν΄μ£Όλ λ³μ Lo, La μ λ£μ΄μ£ΌκΈ° μν ν΄λμ€ Lo, La μλ μ¬μ©μμ μλ, κ²½λ κ°μ΄ κ°κ° λ€μ΄κ° μλ€.
class MainActivityLocationCallback implements LocationEngineCallback<LocationEngineResult> {
private final WeakReference<MainActivity> activityWeakReference;
MainActivityLocationCallback(MainActivity activity) {
this.activityWeakReference = new WeakReference<>(activity);
}
/**
* The LocationEngineCallback interface's method which fires when the device's location has changed. * @param result the LocationEngineResult object which has the last known location within it.
*/ @Override
public void onSuccess(LocationEngineResult result) {
Log.e(TAG,"onSuccess μ€ν");
MainActivity activity = activityWeakReference.get();
if (activity != null) {
Location location = result.getLastLocation();
if (location == null) {
return;
}
// Create a Toast which displays the new location's coordinates
//λμ μμΉλ₯Ό λ°μμμ La, Lo μ λ£μ
La = result.getLastLocation().getLatitude();
Lo = result.getLastLocation().getLongitude();
// Pass the new location to the Maps SDK's LocationComponent
if (activity.mapboxMap != null && result.getLastLocation() != null) {
activity.mapboxMap.getLocationComponent().forceLocationUpdate(result.getLastLocation());
}
}
}
/**
* The LocationEngineCallback interface's method which fires when the device's location can not be captured * * @param exception the exception message
*/ @Override
public void onFailure(@NonNull Exception exception) {
Log.e("LocationChangeActivity", exception.getLocalizedMessage());
MainActivity activity = activityWeakReference.get();
if (activity != null) {
Toast.makeText(activity, exception.getLocalizedMessage(),
Toast.LENGTH_SHORT).show();
}
}
}
- onstyleLoaded λ©μλμ λ€μμ μΆκ°νμ¬ λ§΅μ΄ λ‘λλλ©΄ μ¬μ©μμ μμΉκ° 맡μ νμλλλ‘ νλ€.
enableLocationComponent(style);
#κ°μ λ°©μ
- νμμ 보λ₯Ό μΉ νΈμ€ν μ ν΅ν΄ κ΄λ¦¬νλ©΄ ν¨μ¨μ μΌλ‘ κ΄λ¦¬λ₯Ό ν μ μμ κ² κ°μ.
- λͺ©μ μ§ μμΉμ μλ, κ²½λ μ’νλ₯Ό λ°μ΄ν°λ² μ΄μ€λ₯Ό ν΅ν΄ κ΄λ¦¬νλ©΄ λμ± νΈν κ² κ°μ