use of android.location.Geocoder in project NewXmPluginSDK by MiEcosystem.
the class XmPluginHostApi method reportGPSInfo.
/**
* ApiLevel:17 同步设备gps信息
*/
public void reportGPSInfo(final String did, final Callback<Void> callback) {
if (TextUtils.isEmpty(did)) {
if (callback != null) {
callback.onFailure(-1, "");
}
return;
}
final DeviceStat deviceStat = getDeviceByDid(did);
if (deviceStat == null) {
if (callback != null) {
callback.onFailure(-1, "");
}
return;
}
requestLocation(new Callback<Location>() {
@Override
public void onSuccess(Location location) {
if (location == null) {
if (callback != null) {
callback.onFailure(-1, "");
}
return;
}
Address lastAddress = null;
Geocoder geoCoder = new Geocoder(context());
try {
List<Address> addressList = geoCoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
if (addressList != null && addressList.size() > 0) {
lastAddress = addressList.get(0);
}
} catch (IOException e) {
}
if (lastAddress == null) {
if (callback != null) {
callback.onFailure(-1, "");
}
return;
}
String adminArea = "";
String countryCode = "";
String locality = "";
String thoroughfare = "";
String subLocality = "";
adminArea = lastAddress.getAdminArea();
countryCode = lastAddress.getCountryCode();
locality = lastAddress.getLocality();
thoroughfare = lastAddress.getThoroughfare();
subLocality = lastAddress.getSubLocality();
reportGPSInfo(deviceStat.model, did, location.getLongitude(), location.getLatitude(), adminArea, countryCode, locality, thoroughfare, subLocality, callback);
}
@Override
public void onFailure(int error, String errorInfo) {
if (callback != null) {
callback.onFailure(error, errorInfo);
}
}
});
}
use of android.location.Geocoder in project wire-android by wireapp.
the class LocationFragment method onCreate.
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (isGooglePlayServicesAvailable()) {
googleApiClient = new GoogleApiClient.Builder(getContext()).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build();
} else {
locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
}
mainHandler = new Handler();
handlerThread = new HandlerThread("Background handler");
handlerThread.start();
backgroundHandler = new Handler(handlerThread.getLooper());
geocoder = new Geocoder(getContext(), Locale.getDefault());
zoom = true;
}
use of android.location.Geocoder in project WordPress-Android by wordpress-mobile.
the class GeocoderUtils method getAddressFromCoords.
public static Address getAddressFromCoords(Context context, double latitude, double longitude) {
Address address = null;
List<Address> addresses = null;
Geocoder gcd = getGeocoder(context);
if (gcd == null) {
return null;
}
try {
addresses = gcd.getFromLocation(latitude, longitude, 1);
} catch (IOException e) {
// may get "Unable to parse response from server" IOException here if Geocoder
// service is hit too frequently
AppLog.e(AppLog.T.UTILS, "Unable to parse response from server. Is Geocoder service hitting the server too frequently?", e);
}
// addresses may be null or empty if network isn't connected
if (addresses != null && addresses.size() > 0) {
address = addresses.get(0);
}
return address;
}
use of android.location.Geocoder in project HumaneApp by Ganesh1010.
the class MapActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
this.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
progressDialog = new ProgressDialog(this);
geocoder = new Geocoder(this, Locale.getDefault());
mapFrag = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFrag.getMapAsync(this);
setLocation = (Button) findViewById(R.id.setLocation);
setLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new getLocation().execute();
}
});
}
use of android.location.Geocoder in project android_frameworks_base by ResurrectionRemix.
the class LocationBasedCountryDetector method getCountryFromLocation.
/**
* @return the ISO 3166-1 two letters country code from the location
*/
protected String getCountryFromLocation(Location location) {
String country = null;
Geocoder geoCoder = new Geocoder(mContext);
try {
List<Address> addresses = geoCoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
if (addresses != null && addresses.size() > 0) {
country = addresses.get(0).getCountryCode();
}
} catch (IOException e) {
Slog.w(TAG, "Exception occurs when getting country from location");
}
return country;
}
Aggregations