Search in sources :

Example 26 with Address

use of android.location.Address 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);
            }
        }
    });
}
Also used : Address(android.location.Address) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) Geocoder(android.location.Geocoder) Location(android.location.Location)

Example 27 with Address

use of android.location.Address 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;
}
Also used : Address(android.location.Address) IOException(java.io.IOException) Geocoder(android.location.Geocoder)

Example 28 with Address

use of android.location.Address in project HumaneApp by Ganesh1010.

the class MapActivity method onMapSearch.

public void onMapSearch(String locations) {
    System.out.println("location searched");
    if (addresses.size() > 0)
        addresses.clear();
    //String locations = locationSearch.getText().toString();
    try {
        addresses = geocoder.getFromLocationName(locations, 1);
        Address address = addresses.get(0);
        if (mCurrLocationMarker != null) {
            mCurrLocationMarker.remove();
        }
        latitude = address.getLatitude();
        longitude = address.getLongitude();
        location = new LatLng(latitude, longitude);
        mCurrLocationMarker = mGoogleMap.addMarker(new MarkerOptions().position(location).title(getAaddress(latitude, longitude)));
        mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 17));
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : MarkerOptions(com.google.android.gms.maps.model.MarkerOptions) Address(android.location.Address) LatLng(com.google.android.gms.maps.model.LatLng) IOException(java.io.IOException)

Example 29 with Address

use of android.location.Address in project HumaneApp by Ganesh1010.

the class GPSTracker method getPostalCode.

/**
	 * Try to get Postal Code
	 * @return null or postalCode
	 */
public String getPostalCode(Context context) {
    List<Address> addresses = getGeocoderAddress(context);
    if (addresses != null && addresses.size() > 0) {
        Address address = addresses.get(0);
        String postalCode = address.getPostalCode();
        return postalCode;
    } else {
        return null;
    }
}
Also used : Address(android.location.Address)

Example 30 with Address

use of android.location.Address in project HumaneApp by Ganesh1010.

the class GPSTracker method getCountryName.

/**
	 * Try to get CountryName
	 * @return null or postalCode
	 */
public String getCountryName(Context context) {
    List<Address> addresses = getGeocoderAddress(context);
    if (addresses != null && addresses.size() > 0) {
        Address address = addresses.get(0);
        String countryName = address.getCountryName();
        return countryName;
    } else {
        return null;
    }
}
Also used : Address(android.location.Address)

Aggregations

Address (android.location.Address)34 Geocoder (android.location.Geocoder)22 IOException (java.io.IOException)18 Locale (java.util.Locale)6 Location (android.location.Location)4 ArrayList (java.util.ArrayList)4 List (java.util.List)3 SuppressLint (android.annotation.SuppressLint)1 ContentValues (android.content.ContentValues)1 Intent (android.content.Intent)1 TypedArray (android.content.res.TypedArray)1 Cursor (android.database.Cursor)1 Point (android.graphics.Point)1 LocationManager (android.location.LocationManager)1 ExifInterface (android.support.media.ExifInterface)1 View (android.view.View)1 AdapterView (android.widget.AdapterView)1 ImageButton (android.widget.ImageButton)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1