0

I am trying to output the location into a textview. I have used the locationManager.toString(), but it would only give me the output android.location.LocationManager@44ee7718. I would like to output the location like Los Angeles, CA 90501.

The code for LocationManager:

LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    //conversion
    Criteria criteria = new Criteria();
    String bp = lm.getBestProvider(criteria, false);
    Location location = lm.getLastKnownLocation(bp);
    double lat = location.getLatitude();
    double lon = location.getLongitude();
    Geocoder gc = new Geocoder(this, Locale.getDefault());
    List<Address> addresses = null;
        try{
            addresses = gc.getFromLocation(lat, lon, 1);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    Address address = addresses.get(0);
    final String ad = address.getAddressLine(0);
    //ends here

The code for the button:

Red.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent (Intent.ACTION_VIEW);
            intent.putExtra("sms_body", ad);
            intent.setType("vnd.android-dir/mms-sms");
            startActivity(intent);
        }
    });
4

2 回答 2

2

Try following code

try {
                // Get the location manager
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
                    Criteria criteria = new Criteria();
                    bestProvider = locationManager.getBestProvider(criteria, false);
                    Location location = locationManager
                            .getLastKnownLocation(bestProvider);
                    double lat = location.getLatitude();
                    double lon = location.getLongitude();
                    Geocoder gc = new Geocoder(this);
                    List<Address> lstAdd = gc.getFromLocation(lat, lon, 1);
                    Address ad = lstAdd.get(0);
                    String str = ad.getAddressLine(0);
                Toast tst = Toast.makeText(this, "Your current location is " + str,
                        Toast.LENGTH_LONG);
                tst.show();
} catch (Exception e) {
                Toast tst = Toast.makeText(this,"Please check your gps settings",
                        Toast.LENGTH_LONG);
                tst.show();
}

Hope this helps.

于 2013-05-04T07:41:11.483 回答
0

Call appropriate Location methods (getLatitude(), getLongitude())

Read this, http://developer.android.com/reference/android/location/Location.html

Also for converting to human readable names, you need to use GeoCoding

Read this, http://developer.android.com/reference/android/location/Geocoder.html

Also, http://developer.android.com/training/basics/location/geocoding.html

于 2013-05-04T07:41:04.827 回答