Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
89 views
in Technique[技术] by (71.8m points)

java - How do I output the location using gps on Android

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);
        }
    });
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...