Example usage for android.location Address getAddressLine

List of usage examples for android.location Address getAddressLine

Introduction

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

Prototype

public String getAddressLine(int index) 

Source Link

Document

Returns a line of the address numbered by the given index (starting at 0), or null if no such line is present.

Usage

From source file:dynamite.zafroshops.app.MainActivity.java

public LocationBase getAddress(boolean force) {
    Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);
    SharedPreferences preferences = getPreferences(0);
    SharedPreferences.Editor editor = preferences.edit();
    boolean locationToggle = preferences.getBoolean(StorageKeys.LOCATION_TOGGLE_KEY, locationToggleDefault);

    try {//from  ww w. j  a  va2 s .c  o  m
        if (!force && LastLocation != null) {

            if (preferences.contains(StorageKeys.COUNTRY_KEY)
                    && !preferences.getString(StorageKeys.COUNTRY_KEY, "").equals("")) {
                LastLocation.CountryCode = preferences.getString(StorageKeys.COUNTRY_KEY, "");
                LastLocation.Town = preferences.getString(StorageKeys.TOWN_KEY, "");
                LastLocation.Street = preferences.getString(StorageKeys.STREET_KEY, "");
                LastLocation.StreetNumber = preferences.getString(StorageKeys.STREETNUMBER_KEY, "");
            }
        } else if (locationToggle && LastLocation != null) {
            List<Address> addresses = geocoder.getFromLocation(LastLocation.Latitude, LastLocation.Longitude,
                    1);

            if (addresses != null && addresses.size() > 0) {
                Address address = addresses.get(0);

                LastLocation.CountryCode = address.getCountryCode();
                LastLocation.Town = address.getLocality();

                if (address.getMaxAddressLineIndex() > 0) {
                    String line = address.getAddressLine(0);

                    if (line.compareTo(LastLocation.Town) < 0) {
                        LastLocation.Street = line.replaceAll("(\\D+) \\d+.*", "$1");
                        LastLocation.StreetNumber = line.replaceAll("\\D+ (\\d+.*)", "$1");
                    }
                }

                editor.putString(StorageKeys.COUNTRY_KEY, LastLocation.CountryCode);
                editor.putString(StorageKeys.TOWN_KEY, LastLocation.Town);
                editor.putString(StorageKeys.STREET_KEY, LastLocation.Street);
                editor.putString(StorageKeys.STREETNUMBER_KEY, LastLocation.StreetNumber);
                editor.commit();
            }
        } else {
            return null;
        }

        return LastLocation;
    } catch (IOException e) {
        return null;
    }
}

From source file:com.RSMSA.policeApp.OffenceReportForm.java

public String getAddress(double lat, double lng) {
    Geocoder geocoder = new Geocoder(OffenceReportForm.this, Locale.getDefault());
    String address = "";
    try {//from w  w  w  . j ava 2 s .c  o  m
        List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
        Address obj = addresses.get(0);
        String add = "";
        if (obj.getAdminArea() != null) {
            add = add + obj.getAdminArea();
        }
        if (obj.getSubAdminArea() != null) {
            add = add + ", " + obj.getSubAdminArea();
        }
        if (obj.getAddressLine(0) != null) {
            add = add + ", " + obj.getAddressLine(0);
        }
        address = add;

        Log.v("IGA", "Address" + add);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {

    }
    return address;
}

From source file:edu.usf.cutr.opentripplanner.android.fragments.MainFragment.java

/**
 * Returns address in string format.//from w ww  . j  a va  2 s . c  om
 * <p>
 * Lines used are first and second.
 *
 * @param add the address to transform
 */
private String addressToString(Address add) {
    return ((add.getAddressLine(0) != null) ? add.getAddressLine(0) : "") + ", "
            + ((add.getAddressLine(1) != null) ? add.getAddressLine(1) : "");
}

From source file:edu.usf.cutr.opentripplanner.android.fragments.MainFragment.java

private String getStringAddress(Address address, boolean multiline) {
    if (address.getMaxAddressLineIndex() >= 0) {

        String result = address.getAddressLine(0);

        if (multiline) {
            for (int i = 1; i <= address.getMaxAddressLineIndex(); i++) {
                if (i == 1) {
                    result += "\n";
                    if (address.getAddressLine(i) != null) {
                        result += address.getAddressLine(i);
                    }//from w w w .  j  a  v a 2s  .  co m
                } else if (i == 2) {
                    result += "\n";
                    if (address.getAddressLine(i) != null) {
                        result += address.getAddressLine(i);
                    }
                } else {
                    if (address.getAddressLine(i) != null) {
                        result += ", " + address.getAddressLine(i);
                    }
                }
            }
        } else {
            for (int i = 1; i <= address.getMaxAddressLineIndex(); i++) {
                if (address.getAddressLine(i) != null) {
                    result += ", " + address.getAddressLine(i);
                }
            }
        }

        return result;
    } else {
        return null;
    }
}

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 {//from  w w w.j av  a2  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();
    }
}

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   w w w. ja 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());
        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();
    }
}