Example usage for android.location Address getLongitude

List of usage examples for android.location Address getLongitude

Introduction

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

Prototype

public double getLongitude() 

Source Link

Document

Returns the longitude of the address if known.

Usage

From source file:com.mhennessy.mapfly.MainActivity.java

public void setMapLocation(String locationName) {
    try {/*from  w w  w.ja  v  a 2 s  .com*/
        List<Address> possibleLocations = mGeocoder.getFromLocationName(locationName, 1);
        if (possibleLocations != null && !possibleLocations.isEmpty()) {
            Address address = possibleLocations.get(0);
            setTitle(address.getFeatureName());
            CameraPosition position = new CameraPosition.Builder()
                    .target(new LatLng(address.getLatitude(), address.getLongitude())).zoom(DEFAULT_ZOOM)
                    .tilt(DEFAULT_TILT).build();
            animateCameraToPosition(position, null);
        }
    } catch (IOException e) {
        Log.e(TAG, "Error getting address from location name.");
    }
}

From source file:org.redbus.ui.stopmap.StopMapActivity.java

public void onAsyncGeocodeResponseSuccess(int requestId, List<Address> addresses_) {
    Log.i(TAG, "Async Geocode success!");
    if (requestId != expectedRequestId)
        return;//from   w w  w  . j  a  v a  2  s. com

    if (busyDialog != null)
        busyDialog.dismiss();

    if (addresses_.size() == 1) {
        Address address = addresses_.get(0);
        LatLng pos = new LatLng(address.getLatitude(), address.getLongitude());
        zoomTo(pos);
        return;
    }

    final List<Address> addresses = addresses_;
    List<String> addressNames = new ArrayList<String>();
    for (Address a : addresses) {
        StringBuilder strb = new StringBuilder();
        for (int i = 0; i < a.getMaxAddressLineIndex(); i++) {
            if (i > 0)
                strb.append(", ");
            strb.append(a.getAddressLine(i));
        }
        addressNames.add(strb.toString());
    }

    String[] addressArray = addressNames.toArray(new String[addressNames.size()]);
    DialogInterface.OnClickListener onClickSetPosition = new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            if (which < 0)
                return;

            Address address = addresses.get(which);
            LatLng pos = new LatLng(address.getLatitude(), address.getLongitude());
            zoomTo(pos);
            dialog.dismiss();
        }
    };
    new AlertDialog.Builder(this).setSingleChoiceItems(addressArray, -1, onClickSetPosition).show();
}

From source file:com.javielinux.fragments.SearchGeoFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = View.inflate(getActivity(), R.layout.search_geo_fragment, null);

    useGeo = (CheckBox) view.findViewById(R.id.cb_use_geo);
    typeGeo = (RadioGroup) view.findViewById(R.id.rg_type_geo);
    typeGeoGPS = (RadioButton) view.findViewById(R.id.rb_use_gps);
    typeGeoMap = (RadioButton) view.findViewById(R.id.rb_use_map);
    typeDistance = (RadioGroup) view.findViewById(R.id.rg_type_distance);
    typeDistanceMiles = (RadioButton) view.findViewById(R.id.rb_distance_miles);
    typeDistanceKM = (RadioButton) view.findViewById(R.id.rb_distance_km);

    llLocation = (LinearLayout) view.findViewById(R.id.ll_location);
    llMap = (LinearLayout) view.findViewById(R.id.ll_map);
    llDistance = (LinearLayout) view.findViewById(R.id.ll_distance);

    place = (AutoCompleteTextView) view.findViewById(R.id.et_place);

    address_list = new ArrayList<Address>();
    address_adapter = new AddressAdapter(getActivity(), address_list);
    place.setAdapter(address_adapter);//from   w w  w. j  av a2  s  . co m

    place.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Address address = address_adapter.getAddressItem(i);

            if (address != null) {
                latitude.setText(String.valueOf(address.getLatitude()));
                longitude.setText(String.valueOf(address.getLongitude()));
            }
        }
    });

    latitude = (EditText) view.findViewById(R.id.et_latitude);
    longitude = (EditText) view.findViewById(R.id.et_longitude);
    distance = (SeekBar) view.findViewById(R.id.sb_distance);
    distanceTxt = (TextView) view.findViewById(R.id.distance);

    distance.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
            changeTextDistance(arg1);
        }

        @Override
        public void onStartTrackingTouch(SeekBar arg0) {
        }

        @Override
        public void onStopTrackingTouch(SeekBar arg0) {
        }
    });

    useGeo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                showFields();
            } else {
                hideFields();
            }
        }
    });

    typeGeoGPS.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                hideFieldsMap();
            }
        }
    });

    typeGeoMap.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                showFieldsMap();
            }
        }
    });

    typeDistanceMiles.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            reloadTextDistance();
        }
    });

    typeDistanceKM.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            reloadTextDistance();
        }
    });

    place.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String currentText = place.getText().toString();

            if (currentText.length() >= 3)
                APITweetTopics.execute(getActivity(), getLoaderManager(), SearchGeoFragment.this,
                        new GetGeolocationAddressRequest(getActivity(), currentText, false));
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void afterTextChanged(Editable editable) {
        }
    });

    populateFields();

    return view;
}

From source file:com.mobicage.rogerthat.GetLocationActivity.java

@Override
protected void onServiceBound() {
    T.UI();//  www  . ja  va 2  s  .com
    setContentView(R.layout.get_location);

    mProgressDialog = new ProgressDialog(this);
    mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    mProgressDialog.setMessage(getString(R.string.updating_location));
    mProgressDialog.setCancelable(true);
    mProgressDialog.setCanceledOnTouchOutside(true);
    mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            T.UI();
            if (mLocationManager != null) {
                try {
                    mLocationManager.removeUpdates(mLocationListener);
                } catch (SecurityException e) {
                    L.bug(e); // Should never happen
                }
            }
        }
    });
    mProgressDialog.setMax(10000);

    mUseGPS = (CheckBox) findViewById(R.id.use_gps_provider);
    mGetCurrentLocationButton = (Button) findViewById(R.id.get_current_location);
    mAddress = (EditText) findViewById(R.id.address);
    mCalculate = (Button) findViewById(R.id.calculate);

    mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    if (mLocationManager == null) {
        mGetCurrentLocationButton.setEnabled(false);
    } else {
        mUseGPS.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked && !mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                    new AlertDialog.Builder(GetLocationActivity.this).setMessage(R.string.gps_is_not_enabled)
                            .setPositiveButton(R.string.yes, new SafeDialogInterfaceOnClickListener() {
                                @Override
                                public void safeOnClick(DialogInterface dialog, int which) {
                                    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                                    startActivityForResult(intent, TURNING_ON_GPS);
                                }
                            }).setNegativeButton(R.string.no, new SafeDialogInterfaceOnClickListener() {
                                @Override
                                public void safeOnClick(DialogInterface dialog, int which) {
                                    mUseGPS.setChecked(false);
                                }
                            }).create().show();
                }
            }
        });

        mGetCurrentLocationButton.setOnClickListener(new SafeViewOnClickListener() {
            @Override
            public void safeOnClick(View v) {
                T.UI();
                if (mService.isPermitted(Manifest.permission.ACCESS_FINE_LOCATION)) {
                    getMyLocation();
                } else {
                    ActivityCompat.requestPermissions(GetLocationActivity.this,
                            new String[] { Manifest.permission.ACCESS_FINE_LOCATION },
                            PERMISSION_REQUEST_ACCESS_FINE_LOCATION);
                }
            }
        });
    }

    mCalculate.setOnClickListener(new SafeViewOnClickListener() {
        @Override
        public void safeOnClick(View v) {
            final ProgressDialog pd = new ProgressDialog(GetLocationActivity.this);
            pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            pd.setMessage(getString(R.string.updating_location));
            pd.setCancelable(false);
            pd.setIndeterminate(true);
            pd.show();
            final String addressText = mAddress.getText().toString();
            mService.postOnIOHandler(new SafeRunnable() {
                @Override
                protected void safeRun() throws Exception {
                    Geocoder geoCoder = new Geocoder(GetLocationActivity.this, Locale.getDefault());
                    try {
                        List<Address> addresses = geoCoder.getFromLocationName(addressText, 5);
                        if (addresses.size() > 0) {
                            Address address = addresses.get(0);
                            final Location location = new Location("");
                            location.setLatitude(address.getLatitude());
                            location.setLongitude(address.getLongitude());
                            mService.postOnUIHandler(new SafeRunnable() {
                                @Override
                                protected void safeRun() throws Exception {
                                    pd.dismiss();
                                    mLocation = location;
                                    showMap();
                                }
                            });
                            return;
                        }
                    } catch (IOException e) {
                        L.d("Failed to geo code address " + addressText, e);
                    }
                    mService.postOnUIHandler(new SafeRunnable() {
                        @Override
                        protected void safeRun() throws Exception {
                            pd.dismiss();
                            UIUtils.showLongToast(GetLocationActivity.this,
                                    getString(R.string.failed_to_lookup_address));
                        }
                    });
                }
            });
        }
    });
}

From source file:com.vishwa.pinit.LocationSuggestionProvider.java

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {

    switch (uriMatcher.match(uri)) {
    case SEARCH_SUGGEST:
        String query = uri.getLastPathSegment();

        MatrixCursor cursor = new MatrixCursor(SEARCH_SUGGEST_COLUMNS, 1);
        if (Geocoder.isPresent()) {
            try {
                mGeocoder = new Geocoder(getContext(), Locale.ENGLISH);
                List<Address> addressList = mGeocoder.getFromLocationName(query, 5);
                for (int i = 0; i < addressList.size(); i++) {
                    Address address = addressList.get(i);
                    StringBuilder fullAddress = new StringBuilder();
                    for (int j = 1; j < address.getMaxAddressLineIndex(); j++) {
                        fullAddress.append(address.getAddressLine(j));
                    }/*from  w  ww . jav  a2 s. c o  m*/
                    cursor.addRow(new String[] { Integer.toString(i), address.getAddressLine(0).toString(),
                            fullAddress.toString(), address.getLatitude() + "," + address.getLongitude() });
                }

                return cursor;
            } catch (IllegalArgumentException e) {
                return getLocationSuggestionsUsingAPI(query, cursor);
            } catch (IOException e) {
                return getLocationSuggestionsUsingAPI(query, cursor);
            }
        }
    default:
        throw new IllegalArgumentException("Unknown Uri: " + uri);
    }
}

From source file:es.rczone.tutoriales.gmaps.MainActivity.java

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    /**// ww  w  .  jav  a  2 s . c om
     * Result for Address activity
     */
    if (requestCode == 1) {

        if (resultCode == RESULT_OK) {
            Address current_location = data.getExtras().getParcelable("result");
            LatLng addressLocation = new LatLng(current_location.getLatitude(),
                    current_location.getLongitude());
            CameraPosition camPos = new CameraPosition.Builder().target(addressLocation) //Center camera in 'Plaza Maestro Villa'
                    .zoom(16) //Set 16 level zoom
                    .build();

            CameraUpdate camUpd3 = CameraUpdateFactory.newCameraPosition(camPos);
            map.animateCamera(camUpd3);
            String description = current_location.getThoroughfare() + " "
                    + current_location.getSubThoroughfare() + ", " + current_location.getLocality() + ", "
                    + current_location.getCountryName();

            Marker m = map.addMarker(new MarkerOptions().position(addressLocation));
            markersList.add(m);

            Toast.makeText(this, description, Toast.LENGTH_LONG).show();
        }
        if (resultCode == RESULT_CANCELED) {
            // Write your code if there's no result
        }
    }
}

From source file:com.ant.sunshine.app.activities.MainActivity.java

private void reloadLocationIfChanged(String location) {
    if (location != null && !location.equals(mLocation)) {
        Fragment fragment = getFragmentById();
        if (isForecastFragment(fragment)) {
            reloadLocation();//from w w w.j a  va 2s  .  c o m
            updateLocationForecast(location);
        } else {
            MapLocationFragment mapLocationFragment = (MapLocationFragment) fragment;
            if (mapLocationFragment != null) {
                try {
                    Geocoder geocoder = new Geocoder(this);
                    List<Address> addressList = geocoder.getFromLocationName(location, 1);
                    Address address = addressList.get(0);
                    Location loc = new Location("");
                    loc.setLatitude(address.getLatitude());
                    loc.setLongitude(address.getLongitude());
                    mapLocationFragment.getLocationListener().onLocationChanged(loc);
                } catch (IOException ioex) {
                    Toast.makeText(this, "Exception in loading the address!", Toast.LENGTH_SHORT).show();
                } catch (Exception exc) {
                    Toast.makeText(this, "Exception in loading the address!", Toast.LENGTH_SHORT).show();
                }
            }
        }

        mLocation = location;
    }
}

From source file:com.javielinux.fragments.SearchGeoFragment.java

@Override
public void onResults(BaseResponse response) {
    GetGeolocationAddressResponse result = (GetGeolocationAddressResponse) response;

    if (result.getSingleResult()) {

        if (result.getAddressList().size() > 0) {
            Address address = result.getAddressList().get(0);

            String text = address.getAddressLine(0);

            if (address.getCountryName() != null)
                text = text + " (" + address.getCountryName() + ")";

            place.setText(text);/*from w w  w. j  a va  2  s . com*/
            latitude.setText(String.valueOf(address.getLatitude()));
            longitude.setText(String.valueOf(address.getLongitude()));
        }
    } else {
        address_list.clear();

        for (Address address : result.getAddressList()) {
            address_list.add(address);
        }

        address_adapter.notifyDataSetChanged();
    }
}

From source file:org.cesar.geofencesdemo.ui.activities.MainActivity.java

@Override
public void onItemClick(final AdapterView<?> adapterView, final View view, final int position, final long id) {
    String str = (String) adapterView.getItemAtPosition(position);
    Address address = GeofenceUtils.getReverseLocationDetails(this, str);
    if (address != null) {
        CommonUtils.hideSoftKeyboard(this, mAutocompleteText);
        updateCamera(address.getLatitude(), address.getLongitude());
    }//from   w  ww .j av  a 2  s . c o m
}

From source file:com.timemachine.controller.ControllerActivity.java

private void handleIntent(Intent intent) {
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String input = intent.getStringExtra(SearchManager.QUERY);
        String suggestion = (String) intent.getExtras().get("intent_extra_data_key");
        String query;//from   w  w w  . j a  v  a2 s  .co m
        // Use the query to search your data somehow
        if (suggestion == null)
            query = input;
        else
            query = suggestion;
        Geocoder geocoder = new Geocoder(ControllerActivity.this);
        try {
            List<Address> address = geocoder.getFromLocationName(query, 1);
            if (address != null && !address.isEmpty()) {
                Address location = address.get(0);
                System.out.println(location.getLatitude() + ", " + location.getLongitude());
                mMap.animateCamera(
                        CameraUpdateFactory.newLatLngZoom(
                                new LatLng(location.getLatitude(), location.getLongitude()), maxZoom),
                        animateCameraDuration, null);
            } else
                System.out.println("No address found.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}