Java Code Examples for android.location.Geocoder#isPresent()

The following examples show how to use android.location.Geocoder#isPresent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GeocoderGlobal.java    From PocketMaps with MIT License 6 votes vote down vote up
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;
}
 
Example 2
Source File: LocationGeofenceEditorActivity.java    From PhoneProfilesPlus with Apache License 2.0 6 votes vote down vote up
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));
}
 
Example 3
Source File: Bridge.java    From tlplib with MIT License 6 votes vote down vote up
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;
}
 
Example 4
Source File: EventsActivity.java    From apigee-android-sdk with Apache License 2.0 6 votes vote down vote up
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();
        }
    }
}
 
Example 5
Source File: LocationService.java    From FineGeotag with GNU General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 6
Source File: ComprehensiveCountryDetector.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
protected boolean isGeoCoderImplemented() {
    return Geocoder.isPresent();
}
 
Example 7
Source File: AndroidLocationService.java    From GeometricWeather with GNU Lesser General Public License v3.0 4 votes vote down vote up
@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;
}
 
Example 8
Source File: GMSLocationService.java    From GeometricWeather with GNU Lesser General Public License v3.0 4 votes vote down vote up
@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;
}
 
Example 9
Source File: Api9Adapter.java    From mytracks with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isGeoCoderPresent() {
  return Geocoder.isPresent();
}
 
Example 10
Source File: GeocoderEx.java    From BackPackTrackII with GNU General Public License v3.0 4 votes vote down vote up
public static boolean isPresent() {
    return Geocoder.isPresent();
}
 
Example 11
Source File: MapFragment.java    From mage-android with Apache License 2.0 4 votes vote down vote up
@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;
}