Search in sources :

Example 21 with Geocoder

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);
            }
        }
    });
}
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 22 with Geocoder

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;
}
Also used : GoogleApiClient(com.google.android.gms.common.api.GoogleApiClient) HandlerThread(android.os.HandlerThread) Handler(android.os.Handler) Geocoder(android.location.Geocoder)

Example 23 with Geocoder

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

Example 24 with Geocoder

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();
        }
    });
}
Also used : ProgressDialog(android.app.ProgressDialog) Geocoder(android.location.Geocoder) SearchView(android.support.v7.widget.SearchView) View(android.view.View)

Example 25 with Geocoder

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

Aggregations

Geocoder (android.location.Geocoder)27 Address (android.location.Address)22 IOException (java.io.IOException)16 Locale (java.util.Locale)6 ArrayList (java.util.ArrayList)4 Location (android.location.Location)3 Handler (android.os.Handler)2 View (android.view.View)2 GoogleApiClient (com.google.android.gms.common.api.GoogleApiClient)2 List (java.util.List)2 SuppressLint (android.annotation.SuppressLint)1 ProgressDialog (android.app.ProgressDialog)1 Intent (android.content.Intent)1 TypedArray (android.content.res.TypedArray)1 ContentObserver (android.database.ContentObserver)1 Point (android.graphics.Point)1 LocationManager (android.location.LocationManager)1 Uri (android.net.Uri)1 HandlerThread (android.os.HandlerThread)1 SearchView (android.support.v7.widget.SearchView)1