緯度経度の処理をGeoCodingではなくGeoCoderで実装。緯度経度変換成功

This commit is contained in:
it232115 2024-06-14 12:29:30 +09:00
parent e5f35c6ca4
commit 8c634499ae
3 changed files with 24 additions and 23 deletions

View File

@ -20,7 +20,7 @@ public class FirestoreReception {
} }
//ClassIdを引数にデータの作成を行う //ClassIdを引数にデータの作成を行う
public void getDocumentsByClassId(int classId) { public void getDocumentsByClassId(int classId, MainActivity context) {
CollectionReference collectionRef = db.collection("QuestionnaireForms"); CollectionReference collectionRef = db.collection("QuestionnaireForms");
// classIdが引数のものを取得する // classIdが引数のものを取得する
@ -34,8 +34,8 @@ public class FirestoreReception {
Map<String, Object> data = document.getData(); Map<String, Object> data = document.getData();
// CreateRootクラスのインスタンスを作成しdataを引数として渡す // CreateRootクラスのインスタンスを作成しdataを引数として渡す
GeoCoding geoCoding = new GeoCoding(); GeoCoder geoCoder = new GeoCoder();
geoCoding.processData(data); geoCoder.processData(data, context);
} }
} else { } else {
Log.w("FirestoreReception", "Error getting documents.", task.getException()); Log.w("FirestoreReception", "Error getting documents.", task.getException());

View File

@ -1,26 +1,25 @@
package com.example.oplogy; package com.example.oplogy;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.util.Log; import android.util.Log;
import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.LatLng;
import com.google.firebase.Timestamp; 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.io.IOException;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Map; import java.util.Map;
public class GeoCoding { public class GeoCoder {
private GeoApiContext geoApiContext; private Context context;
public void processData(Map<String, Object> data) {
public void processData(Map<String, Object> data, Context context) {
try { try {
// Google Cloud Platformで作成したAPIキーを設定します this.context = context;
geoApiContext = new GeoApiContext.Builder()
.apiKey("AIzaSyBQ1Ak-I2NL5TP4K59ZI0VgzKk6HNZuusw")
.build();
//家庭訪問先の住所 //家庭訪問先の住所
List<String> address = (List<String>) data.get("address"); List<String> address = (List<String>) data.get("address");
//家庭訪問の第一希望日(配列0が希望時間帯のはじめ配列1がおわり) //家庭訪問の第一希望日(配列0が希望時間帯のはじめ配列1がおわり)
@ -41,18 +40,20 @@ public class GeoCoding {
Log.e("NullPointerException", "getの中身がnull" + e); Log.e("NullPointerException", "getの中身がnull" + e);
} }
} }
private LatLng geocodeAddress(String address) { private LatLng geocodeAddress(String address) {
try { try {
Log.d("Geocodingtry", "tryに入った"); Geocoder geocoder = new Geocoder(context, Locale.getDefault());
GeocodingResult[] results = GeocodingApi.geocode(geoApiContext, address).await(); List<Address> addresses = geocoder.getFromLocationName(address, 1);
Log.d("GeocodingResult", "Results: " + Arrays.toString(results)); if (addresses != null && !addresses.isEmpty()) {
if (results != null && results.length > 0) { Address addressResult = addresses.get(0);
return new LatLng(results[0].geometry.location.lat, results[0].geometry.location.lng); double latitude = addressResult.getLatitude();
double longitude = addressResult.getLongitude();
return new LatLng(latitude, longitude);
} }
} catch (Exception e) { } catch (IOException e) {
Log.e("GeocodingException", "Error geocoding address: " + address, e); Log.e("GeocodingException", "Error geocoding address: " + address, e);
} }
return null; return null;
} }
}
}

View File

@ -66,7 +66,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
db = FirebaseFirestore.getInstance(); db = FirebaseFirestore.getInstance();
firestoreReception = new FirestoreReception(); firestoreReception = new FirestoreReception();
firestoreReception.getDocumentsByClassId(100); firestoreReception.getDocumentsByClassId(100,MainActivity.this);