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

View File

@ -1,26 +1,25 @@
package com.example.oplogy;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
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.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.Map;
public class GeoCoding {
private GeoApiContext geoApiContext;
public void processData(Map<String, Object> data) {
public class GeoCoder {
private Context context;
public void processData(Map<String, Object> data, Context context) {
try {
// Google Cloud Platformで作成したAPIキーを設定します
geoApiContext = new GeoApiContext.Builder()
.apiKey("AIzaSyBQ1Ak-I2NL5TP4K59ZI0VgzKk6HNZuusw")
.build();
this.context = context;
//家庭訪問先の住所
List<String> address = (List<String>) data.get("address");
//家庭訪問の第一希望日(配列0が希望時間帯のはじめ配列1がおわり)
@ -41,18 +40,20 @@ public class GeoCoding {
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);
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocationName(address, 1);
if (addresses != null && !addresses.isEmpty()) {
Address addressResult = addresses.get(0);
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);
}
return null;
}
}
}

View File

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