下面列出了android.location.Geocoder# isPresent ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public List<Address> find_google(Context context, String searchS)
{
stopping = false;
log("Google geocoding started");
Geocoder geocoder = new Geocoder(context, locale);
if (!Geocoder.isPresent()) { return null; }
try
{
List<Address> result = geocoder.getFromLocationName(searchS, 50);
return result;
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
private void refreshActivity(boolean setMapCamera) {
boolean enableAddressButton = false;
if (mLocation != null) {
// Determine whether a geo-coder is available.
if (Geocoder.isPresent()) {
startIntentService(false);
enableAddressButton = true;
}
}
if (addressButton.isEnabled())
GlobalGUIRoutines.setImageButtonEnabled(enableAddressButton, addressButton, getApplicationContext());
String name = geofenceNameEditText.getText().toString();
updateEditedMarker(setMapCamera);
okButton.setEnabled((!name.isEmpty()) && (mLocation != null));
}
public static String countryCodeFromLastKnownLocation() throws IOException {
Activity current = UnityPlayer.currentActivity;
LocationManager locationManager =
(LocationManager) current.getSystemService(Context.LOCATION_SERVICE);
Location location =
locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null && Geocoder.isPresent()) {
Geocoder gcd = new Geocoder(current, Locale.getDefault());
List<Address> addresses;
addresses = gcd.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
if (addresses != null && !addresses.isEmpty()) {
Address address = addresses.get(0);
return address.getCountryCode();
}
}
return null;
}
public EventContainer(Entity entity) {
this.eventName = entity.getStringProperty("eventName");
JsonNode locationObject= (JsonNode) entity.getProperties().get("location");
if( locationObject != null && Geocoder.isPresent() ) {
Geocoder myLocation = new Geocoder(getApplicationContext(), Locale.getDefault());
try {
List<Address> addressList = myLocation.getFromLocation(locationObject.get("latitude").doubleValue(), locationObject.get("longitude").doubleValue(), 1);
if( addressList != null && addressList.size() > 0 ) {
Address locationAddress = addressList.get(0);
this.eventLocation = locationAddress.getLocality() + ", " + locationAddress.getAdminArea();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static List<String> reverseGeocode(Location location, Context context) {
List<String> listLine = new ArrayList<>();
if (location != null && Geocoder.isPresent())
try {
Geocoder geocoder = new Geocoder(context);
List<Address> listPlace = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
if (listPlace != null && listPlace.size() > 0) {
for (int l = 0; l < listPlace.get(0).getMaxAddressLineIndex(); l++)
listLine.add(listPlace.get(0).getAddressLine(l));
}
} catch (IOException ignored) {
}
return listLine;
}
protected boolean isGeoCoderImplemented() {
return Geocoder.isPresent();
}
@WorkerThread
private Result buildResult(@NonNull Location location) {
Result result = new Result((float) location.getLatitude(), (float) location.getLongitude());
result.hasGeocodeInformation = false;
if (!Geocoder.isPresent()) {
return result;
}
List<Address> addressList = null;
try {
addressList = new Geocoder(context, LanguageUtils.getCurrentLocale(context))
.getFromLocation(
location.getLatitude(),
location.getLongitude(),
1
);
} catch (IOException e) {
e.printStackTrace();
}
if (addressList == null || addressList.size() == 0) {
return result;
}
result.setGeocodeInformation(
addressList.get(0).getCountryName(),
addressList.get(0).getAdminArea(),
TextUtils.isEmpty(addressList.get(0).getLocality())
? addressList.get(0).getSubAdminArea()
: addressList.get(0).getLocality(),
addressList.get(0).getSubLocality()
);
String countryCode = addressList.get(0).getCountryCode();
if (TextUtils.isEmpty(countryCode)) {
if (TextUtils.isEmpty(result.country)) {
result.inChina = false;
} else {
result.inChina = result.country.equals("中国")
|| result.country.equals("香港")
|| result.country.equals("澳门")
|| result.country.equals("台湾")
|| result.country.equals("China");
}
} else {
result.inChina = countryCode.equals("CN")
|| countryCode.equals("cn")
|| countryCode.equals("HK")
|| countryCode.equals("hk")
|| countryCode.equals("TW")
|| countryCode.equals("tw");
}
return result;
}
@WorkerThread
private Result buildResult(@NonNull Location location) {
Result result = new Result((float) location.getLatitude(), (float) location.getLongitude());
result.hasGeocodeInformation = false;
if (!Geocoder.isPresent()) {
return result;
}
List<Address> addressList = null;
try {
addressList = new Geocoder(context, LanguageUtils.getCurrentLocale(context))
.getFromLocation(
location.getLatitude(),
location.getLongitude(),
1
);
} catch (IOException e) {
e.printStackTrace();
}
if (addressList == null || addressList.size() == 0) {
return result;
}
result.setGeocodeInformation(
addressList.get(0).getCountryName(),
addressList.get(0).getAdminArea(),
TextUtils.isEmpty(addressList.get(0).getLocality())
? addressList.get(0).getSubAdminArea()
: addressList.get(0).getLocality(),
addressList.get(0).getSubLocality()
);
String countryCode = addressList.get(0).getCountryCode();
if (TextUtils.isEmpty(countryCode)) {
if (TextUtils.isEmpty(result.country)) {
result.inChina = false;
} else {
result.inChina = result.country.equals("中国")
|| result.country.equals("香港")
|| result.country.equals("澳门")
|| result.country.equals("台湾")
|| result.country.equals("China");
}
} else {
result.inChina = countryCode.equals("CN")
|| countryCode.equals("cn")
|| countryCode.equals("HK")
|| countryCode.equals("hk")
|| countryCode.equals("TW")
|| countryCode.equals("tw");
}
return result;
}
@Override
public boolean isGeoCoderPresent() {
return Geocoder.isPresent();
}
public static boolean isPresent() {
return Geocoder.isPresent();
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_map, container, false);
setHasOptionsMenu(true);
staticGeometryCollection = new StaticGeometryCollection();
availableLayerDownloadsIcon = view.findViewById(R.id.available_layer_downloads);
zoomToLocationButton = view.findViewById(R.id.zoom_button);
compassButton = view.findViewById(R.id.compass_button);
compassButton.setOnClickListener(v -> resetMapBearing());
searchButton = view.findViewById(R.id.map_search_button);
if (Geocoder.isPresent()) {
searchButton.setOnClickListener(v -> search());
} else {
searchButton.hide();
}
view.findViewById(R.id.new_observation_button).setOnClickListener(v -> onNewObservation());
searchLayout = view.findViewById(R.id.search_layout);
searchView = view.findViewById(R.id.search_view);
searchView.setIconifiedByDefault(false);
searchView.setIconified(false);
searchView.clearFocus();
MapsInitializer.initialize(context);
ImageButton mapSettings = view.findViewById(R.id.map_settings);
mapSettings.setOnClickListener(this);
mapView = view.findViewById(R.id.mapView);
Bundle mapState = (savedInstanceState != null) ? savedInstanceState.getBundle(MAP_VIEW_STATE): null;
mapView.onCreate(mapState);
mgrsBottomSheet = view.findViewById(R.id.mgrs_bottom_sheet);
mgrsBottomSheetBehavior = BottomSheetBehavior.from(mgrsBottomSheet);
mgrsCursor = view.findViewById(R.id.mgrs_grid_cursor);
mgrsTextView = mgrsBottomSheet.findViewById(R.id.mgrs_code);
mgrsGzdTextView = mgrsBottomSheet.findViewById(R.id.mgrs_gzd_zone);
mgrs100dKmTextView = mgrsBottomSheet.findViewById(R.id.mgrs_grid_zone);
mgrsEastingTextView = mgrsBottomSheet.findViewById(R.id.mgrs_easting);
mgrsNorthingTextView = mgrsBottomSheet.findViewById(R.id.mgrs_northing);
// Initialize the GeoPackage cache with a GeoPackage manager
GeoPackageManager geoPackageManager = GeoPackageFactory.getManager(getActivity().getApplicationContext());
geoPackageCache = new GeoPackageCache(geoPackageManager);
locationProvider = locationPolicy.getBestLocationProvider();
return view;
}