Example usage for android.location Geocoder Geocoder

List of usage examples for android.location Geocoder Geocoder

Introduction

In this page you can find the example usage for android.location Geocoder Geocoder.

Prototype

public Geocoder(Context context, Locale locale) 

Source Link

Document

Constructs a Geocoder whose responses will be localized for the given Locale.

Usage

From source file:com.instiwork.RegistrationActivity.java

private void enableMyLocation() {
    if (ContextCompat.checkSelfPermission(this,
            android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        PermissionUtils.requestPermission(this, MY_PERMISSIONS_REQUEST_READ,
                android.Manifest.permission.ACCESS_FINE_LOCATION, false);
    } else {// ww  w.java  2 s .c om
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if (mLastLocation != null) {
            LOCATION_LATITUDE = "" + mLastLocation.getLatitude();
            LOCATION_LONGITUDE = "" + mLastLocation.getLongitude();

            try {
                Geocoder geocoder = new Geocoder(RegistrationActivity.this, Locale.getDefault());
                List<Address> addresses = geocoder.getFromLocation(mLastLocation.getLatitude(),
                        mLastLocation.getLongitude(), 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5

                currentCountry = "" + addresses.get(0).getCountryName();

            } catch (Exception e) {
                e.printStackTrace();
            }

            getCountryList();
        }
    }
}

From source file:com.instiwork.RegistrationActivity.java

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode != MY_PERMISSIONS_REQUEST_READ) {
        return;/*from ww  w . j  a  v  a2 s . c o  m*/
    }

    try {
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if (mLastLocation != null) {
            LOCATION_LATITUDE = "" + mLastLocation.getLatitude();
            LOCATION_LONGITUDE = "" + mLastLocation.getLongitude();

            try {
                Geocoder geocoder = new Geocoder(RegistrationActivity.this, Locale.getDefault());
                List<Address> addresses = geocoder.getFromLocation(mLastLocation.getLatitude(),
                        mLastLocation.getLongitude(), 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5

                currentCountry = "" + addresses.get(0).getCountryName();

            } catch (Exception e) {
                e.printStackTrace();
            }

            getCountryList();
        }
    } catch (SecurityException e) {
        e.printStackTrace();
    }
}

From source file:me.ububble.speakall.fragment.ConversationChatFragment.java

private void muestraPosicion(final Location location) {
    milocManager.removeUpdates(this);
    Geocoder geoCoder = new Geocoder(activity, Locale.getDefault());
    List<Address> addresses = null;
    StringBuilder sb = new StringBuilder();
    try {//from ww w. j  av  a  2 s.  c om
        addresses = geoCoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
        if (addresses != null && addresses.size() > 0) {
            for (Address address : addresses) {
                for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
                    sb.append(address.getAddressLine(i)).append("\n");
                }
            }
        }
        Log.w("UBICACION", sb.toString());
        Message msjNew = new Message();
        JSONObject locationSend = new JSONObject();
        locationSend.put("direccion", sb.toString());
        locationSend.put("latitud", location.getLatitude());
        locationSend.put("longitud", location.getLongitude());
        Calendar fecha = Calendar.getInstance();
        String id = u.id + contact.idContacto + fecha.getTimeInMillis()
                + Settings.Secure.getString(activity.getContentResolver(), Settings.Secure.ANDROID_ID);
        msjNew.translation = true;
        msjNew.mensajeId = id;
        msjNew.emisor = u.id;
        msjNew.receptor = contact.idContacto;
        msjNew.mensaje = locationSend.toString();
        msjNew.emisorEmail = u.email;
        msjNew.receptorEmail = contact.email;
        msjNew.emisorLang = u.lang;
        msjNew.receptorLang = contact.lang;
        msjNew.emitedAt = fecha.getTimeInMillis();
        msjNew.tipo = Integer.parseInt(getString(R.string.MSG_TYPE_LOCATION));
        msjNew.delay = 0;
        if (SpeakSocket.mSocket != null)
            if (SpeakSocket.mSocket.connected()) {
                msjNew.status = 1;
            } else {
                msjNew.status = -1;
            }
        msjNew.save();
        Chats chat = new Select().from(Chats.class).where("idContacto = ?", msjNew.receptor).executeSingle();
        if (chat == null) {
            Contact contact = new Select().from(Contact.class).where("id_contact = ?", msjNew.receptor)
                    .executeSingle();
            Chats newChat = new Chats();
            newChat.mensajeId = msjNew.mensajeId;
            newChat.idContacto = msjNew.receptor;
            newChat.isLockedConversation = false;
            newChat.lastStatus = msjNew.status;
            newChat.email = msjNew.receptorEmail;
            if (contact != null) {
                newChat.photo = contact.photo;
                newChat.fullName = contact.fullName;
                newChat.lang = contact.lang;
                newChat.screenName = contact.screenName;
                newChat.photoload = true;
                newChat.phone = contact.phone;
            } else {
                newChat.photo = null;
                newChat.photoload = false;
                newChat.fullName = msjNew.receptorEmail;
                newChat.lang = msjNew.receptorLang;
                newChat.screenName = msjNew.receptorEmail;
                newChat.phone = null;
            }
            newChat.emitedAt = msjNew.emitedAt;
            newChat.notRead = 0;
            newChat.lastMessage = "send Location";
            newChat.show = true;
            newChat.save();
        } else {
            if (!chat.photoload) {
                Contact contact = new Select().from(Contact.class).where("id_contact = ?", msjNew.emisor)
                        .executeSingle();
                if (contact != null) {
                    chat.photo = contact.photo;
                    chat.photoload = true;
                } else {
                    chat.photo = null;
                    chat.photoload = false;
                }
            }
            chat.mensajeId = msjNew.mensajeId;
            chat.lastStatus = msjNew.status;
            chat.emitedAt = msjNew.emitedAt;
            chat.notRead = 0;
            chat.lastMessage = "send Location";
            chat.save();
        }
        showNewMessage(msjNew);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

From source file:me.ububble.speakall.fragment.ConversationGroupFragment.java

private void muestraPosicion(final Location location) {
    milocManager.removeUpdates(this);
    Geocoder geoCoder = new Geocoder(activity, Locale.getDefault());
    List<Address> addresses = null;
    StringBuilder sb = new StringBuilder();
    try {// w w  w .j  a  va 2 s. c o m
        addresses = geoCoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
        if (addresses != null && addresses.size() > 0) {
            for (Address address : addresses) {
                for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
                    sb.append(address.getAddressLine(i)).append("\n");
                }
            }
        }
        Log.w("UBICACION", sb.toString());
        MsgGroups msgGroup = new MsgGroups();
        JSONObject locationSend = new JSONObject();
        locationSend.put("direccion", sb.toString());
        locationSend.put("latitud", location.getLatitude());
        locationSend.put("longitud", location.getLongitude());
        long fecha = Calendar.getInstance().getTimeInMillis();
        JSONArray targets = new JSONArray();
        JSONArray contactos = new JSONArray(grupo.targets);
        String contactosId = null;
        if (SpeakSocket.mSocket != null) {
            if (SpeakSocket.mSocket.connected()) {
                for (int i = 0; i < contactos.length(); i++) {
                    JSONObject contacto = contactos.getJSONObject(i);
                    JSONObject newContact = new JSONObject();
                    Contact contact = new Select().from(Contact.class)
                            .where("id_contact = ?", contacto.getString("name")).executeSingle();
                    if (contact != null) {
                        if (!contact.idContacto.equals(u.id)) {
                            if (translate)
                                newContact.put("lang", contact.lang);
                            else
                                newContact.put("lang", u.lang);
                            newContact.put("name", contact.idContacto);
                            newContact.put("screen_name", contact.screenName);
                            newContact.put("status", 1);
                            contactosId += contact.idContacto;
                            targets.put(targets.length(), newContact);
                        }
                    }
                }
            } else {
                for (int i = 0; i < contactos.length(); i++) {
                    JSONObject contacto = contactos.getJSONObject(i);
                    JSONObject newContact = new JSONObject();
                    Contact contact = new Select().from(Contact.class)
                            .where("id_contact = ?", contacto.getString("name")).executeSingle();
                    if (contact != null) {
                        if (!contact.idContacto.equals(u.id)) {
                            if (translate)
                                newContact.put("lang", contact.lang);
                            else
                                newContact.put("lang", u.lang);
                            newContact.put("name", contact.idContacto);
                            newContact.put("screen_name", contact.screenName);
                            newContact.put("status", -1);
                            contactosId += contact.idContacto;
                            targets.put(targets.length(), newContact);
                        }
                    }
                }
            }
        }
        msgGroup.grupoId = grupo.grupoId;
        msgGroup.mensajeId = u.id + grupo.grupoId + fecha
                + Settings.Secure.getString(activity.getContentResolver(), Settings.Secure.ANDROID_ID);
        msgGroup.emisor = u.id;
        msgGroup.receptores = targets.toString();
        msgGroup.mensaje = locationSend.toString();
        msgGroup.emisorEmail = u.email;
        msgGroup.emisorLang = u.lang;
        msgGroup.translation = translate;
        msgGroup.emitedAt = fecha;
        msgGroup.tipo = Integer.parseInt(getString(R.string.MSG_TYPE_GROUP_LOCATION));
        if (tiempoMensaje) {
            msgGroup.delay = temporizadorSeek.getValue();
        } else {
            msgGroup.delay = 0;
        }
        msgGroup.save();
        messageText.setText("");
        showNewMessage(msgGroup);
        messageClock.setTextColor(getResources().getColor(R.color.speak_all_gray));
        messageClock.setText(Finder.STRING.ICN_TEMPORIZER.toString());
        tiempoMensaje = false;
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
}