package com.stylefeng.guns.modular.system.utils;
|
|
import com.google.maps.GeoApiContext;
|
import com.google.maps.GeocodingApi;
|
import com.google.maps.model.AddressComponent;
|
import com.google.maps.model.GeocodingResult;
|
|
public class AddressLookup {
|
|
public static String getAddress(String administrativeCode) {
|
GeoApiContext context = new GeoApiContext.Builder()
|
.apiKey("AIzaSyBBW0XxW1FK7IXmmS7KFtAjX3o99eFPsss") // REPLACE WITH YOUR API KEY
|
.build();
|
|
GeocodingResult[] results = GeocodingApi.geocode(context, "EN " + administrativeCode).awaitIgnoreError();
|
|
if (results == null || results.length == 0) {
|
return null;
|
}
|
|
AddressComponent[] components = results[0].addressComponents;
|
String province = getComponent(components, "administrative_area_level_1");
|
String city = getComponent(components, "locality");
|
String district = getComponent(components, "administrative_area_level_3");
|
String street = getComponent(components, "route");
|
String number = getComponent(components, "street_number");
|
|
StringBuilder builder = new StringBuilder();
|
|
if (province != null) {
|
builder.append(province);
|
}
|
|
if (city != null && !city.equals(province)) {
|
builder.append(city);
|
}
|
|
if (district != null && !district.equals(city)) {
|
builder.append(district);
|
}
|
|
if (street != null) {
|
builder.append(street);
|
}
|
|
if (number != null) {
|
builder.append(number);
|
}
|
|
return builder.toString();
|
}
|
|
private static String getComponent(AddressComponent[] components, String type) {
|
for (AddressComponent component : components) {
|
if (component.types[0].equals(type)) {
|
return component.longName;
|
}
|
}
|
return null;
|
}
|
}
|