・Firebaseから受け取った住所の緯度経度変換(未達)(エラーはでない)

This commit is contained in:
it232115 2024-06-14 11:12:43 +09:00
parent 87e610d4f4
commit e5f35c6ca4
8 changed files with 102 additions and 37 deletions

View File

@ -36,7 +36,7 @@ android {
} }
dependencies { dependencies {
implementation 'com.google.maps:google-maps-services:0.15.0'
implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.5.0' implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'androidx.constraintlayout:constraintlayout:2.1.4'

View File

@ -2,7 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"> xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET" />
<application <application
android:allowBackup="true" android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules" android:dataExtractionRules="@xml/data_extraction_rules"
@ -44,7 +44,7 @@
android:name=".RootSearchActivity" android:name=".RootSearchActivity"
android:exported="false" /> android:exported="false" />
<activity <activity
android:name=".SetupActivity" android:name=".SetUpActivity"
android:exported="false" /> android:exported="false" />
<activity <activity
android:name=".CreateUUID" android:name=".CreateUUID"
@ -71,8 +71,8 @@

View File

@ -0,0 +1,27 @@
//package com.example.oplogy;
//import com.google.maps.GeoApiContext;
//import com.google.maps.GeocodingApi;
//import com.google.maps.model.GeocodingResult;
//import com.google.type.LatLng;
//
//
//public class AddressConverter {
// public static LatLng convertAddressToLatLng(String address) {
// try {
// // Google MapsのAPIキーを使用してGeoApiContextオブジェクトを作成します
// GeoApiContext context = new GeoApiContext.Builder()
// .apiKey("AIzaSyBQ1Ak-I2NL5TP4K59ZI0VgzKk6HNZuusw") //GoogleMapsAPiのAPiキー
// .build();
// // GeocodingApiを使用して住所を緯度経度に変換します
// GeocodingResult[] results = GeocodingApi.geocode(context, address).await();
// if (results != null && results.length > 0) {
// // 位置情報緯度と経度を返します
// return results[0].geometry.location;
// }
// } catch (Exception e) {
// e.printStackTrace();
// }
// // 結果が存在しない場合やエラーが発生した場合はnullを返します
// return null;
// }
//}

View File

@ -4,13 +4,11 @@ import android.util.Log;
import com.google.android.gms.tasks.OnCompleteListener; import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task; import com.google.android.gms.tasks.Task;
import com.google.firebase.Timestamp;
import com.google.firebase.firestore.CollectionReference; import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.FirebaseFirestore; import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QueryDocumentSnapshot; import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot; import com.google.firebase.firestore.QuerySnapshot;
import java.util.List;
import java.util.Map; import java.util.Map;
public class FirestoreReception { public class FirestoreReception {
@ -35,27 +33,9 @@ public class FirestoreReception {
for (QueryDocumentSnapshot document : task.getResult()) { for (QueryDocumentSnapshot document : task.getResult()) {
Map<String, Object> data = document.getData(); Map<String, Object> data = document.getData();
// デバッグ用のログ出力 // CreateRootクラスのインスタンスを作成しdataを引数として渡す
Log.d("FirestoreReception", "Document ID: " + document.getId()); GeoCoding geoCoding = new GeoCoding();
Log.d("FirestoreReception", "Data: " + data); geoCoding.processData(data);
// ここでデータを取得し必要に応じて処理を行います
// String parentName = (String) data.get("patronName");
// String childName = (String) data.get("childName");
// String studentId = (String) data.get("studentNumber");
//// Timestamp address = (Timestamp) data.get("address");
// List<Timestamp> firstDay = (List<Timestamp>) data.get("firstDay");
// List<Timestamp> secondDay = (List<Timestamp>) data.get("secondDay");
// List<Timestamp> thirdDay = (List<Timestamp>) data.get("thirdDay");
//
// // 取得したデータを使って必要な処理を行う
// Log.d("FirestoreReception", "ParentName: " + parentName);
// Log.d("FirestoreReception", "ChildName: " + childName);
// Log.d("FirestoreReception", "StudentNumber: " + studentId);
//// Log.d("FirestoreReception", "Address: " + address.toDate());
// Log.d("FirestoreReception", "First Day: " + firstDay);
// Log.d("FirestoreReception", "Second Day: " + secondDay);
// Log.d("FirestoreReception", "Third Day: " + thirdDay);
} }
} else { } else {
Log.w("FirestoreReception", "Error getting documents.", task.getException()); Log.w("FirestoreReception", "Error getting documents.", task.getException());

View File

@ -0,0 +1,58 @@
package com.example.oplogy;
import android.util.Log;
import com.google.android.gms.maps.model.LatLng;
import com.google.firebase.Timestamp;
import com.google.maps.GeoApiContext;
import com.google.maps.GeocodingApi;
import com.google.maps.model.GeocodingResult;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
public class GeoCoding {
private GeoApiContext geoApiContext;
public void processData(Map<String, Object> data) {
try {
// Google Cloud Platformで作成したAPIキーを設定します
geoApiContext = new GeoApiContext.Builder()
.apiKey("AIzaSyBQ1Ak-I2NL5TP4K59ZI0VgzKk6HNZuusw")
.build();
//家庭訪問先の住所
List<String> address = (List<String>) data.get("address");
//家庭訪問の第一希望日(配列0が希望時間帯のはじめ配列1がおわり)
List<Timestamp> firstDay = (List<Timestamp>) data.get("firstDay");
//出席番号
Long studentNumber = (Long) data.get("studentNumber");
// 住所を緯度経度に変換
LatLng latLng = geocodeAddress(address.get(0));
Log.d("FirestoreReception", "address: " + address.get(0));
Log.d("FirestoreReception", "firstDay: " + firstDay.get(0));
Log.d("FirestoreReception", "firstDay: " + firstDay.get(1));
Log.d("FirestoreReception", "studentNumber: " + studentNumber);
// 緯度経度をLogに出力
Log.d("緯度経度", "latLng: "+latLng );
} catch (NullPointerException e) {
Log.e("NullPointerException", "getの中身がnull" + e);
}
}
private LatLng geocodeAddress(String address) {
try {
Log.d("Geocodingtry", "tryに入った");
GeocodingResult[] results = GeocodingApi.geocode(geoApiContext, address).await();
Log.d("GeocodingResult", "Results: " + Arrays.toString(results));
if (results != null && results.length > 0) {
return new LatLng(results[0].geometry.location.lat, results[0].geometry.location.lng);
}
} catch (Exception e) {
Log.e("GeocodingException", "Error geocoding address: " + address, e);
}
return null;
}
}

View File

@ -1,18 +1,17 @@
package com.example.oplogy; package com.example.oplogy;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.Button;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import com.google.firebase.firestore.FirebaseFirestore; import com.google.firebase.firestore.FirebaseFirestore;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ public class MainActivity extends AppCompatActivity implements View.OnClickListener{
@ -87,7 +86,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
// セットアップのクリック処理 // セットアップのクリック処理
if(view == setUp){ if(view == setUp){
imageSetup.setImageResource(R.drawable.ischecked_uuid); imageSetup.setImageResource(R.drawable.ischecked_uuid);
Intent toSetup = new Intent(MainActivity.this,SetupActivity.class); Intent toSetup = new Intent(MainActivity.this,SetUpActivity.class);
startActivity(toSetup); startActivity(toSetup);
} }

View File

@ -4,11 +4,11 @@ import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
public class SetupActivity extends AppCompatActivity { public class SetUpActivity extends AppCompatActivity {
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.setup); setContentView(R.layout.activity_set_up);
} }
} }

View File

@ -1,10 +1,11 @@
buildscript { buildscript {
dependencies { dependencies {
classpath 'com.google.gms:google-services:4.3.14' classpath 'com.google.gms:google-services:4.3.14'
} }
}// Top-level build file where you can add configuration options common to all sub-projects/modules. }// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins { plugins {
id 'com.android.application' version '8.0.2' apply false id 'com.android.application' version '8.0.2' apply false
id 'com.android.library' version '8.0.2' apply false id 'com.android.library' version '8.0.2' apply false
id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin' version '2.0.1' apply false id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin' version '2.0.1' apply false
} }