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
517 views
in Technique[技术] by (71.8m points)

map - How to Get City Name by Latitude &Longitude in android?

I want to city name by current location but i have latitude and longitude.how to get it? i used button click then i get double value latitude and longitude. my code in below.please help me.

Thanks!!

Button btnShowLocation;

    GPSTracker gps;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btnShowLocation = (Button) findViewById(R.id.btnShowLocation);

        // show location button click event
        btnShowLocation.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {       
                // create class object
                gps = new GPSTracker(AndroidGPSTrackingActivity.this);

                // check if GPS enabled     
                if(gps.canGetLocation()){

                    double latitude = gps.getLatitude();
                    double longitude = gps.getLongitude();

                    // 
 is for new line
                    Toast.makeText(getApplicationContext(), "Your Location is - 
Lat: " + latitude + "
Long: " + longitude, Toast.LENGTH_LONG).show();    
                }else{

                    gps.showSettingsAlert();
                }

            }
        });

I edited below code but not get cityname please help me !!!

Button btnShowLocation;

    GPSTracker gps;
    String CityName;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btnShowLocation = (Button) findViewById(R.id.btnShowLocation);

        // show location button click event
        btnShowLocation.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // create class object
                gps = new GPSTracker(AndroidGPSTrackingActivity.this);

                // check if GPS enabled
                if (gps.canGetLocation()) {

                    double latitude = gps.getLatitude();
                    double longitude = gps.getLongitude();

                    Geocoder geocoder = new Geocoder(
                            AndroidGPSTrackingActivity.this, Locale
                                    .getDefault());
                    List<Address> addresses;
                    try {
                        Log.v("log_tag", "latitude" + latitude);
                        Log.v("log_tag", "longitude" + longitude);
                        addresses = geocoder.getFromLocation(latitude,
                                longitude, 1);
                        Log.v("log_tag", "addresses+)_+++" + addresses);
                        CityName = addresses.get(0).getAddressLine(0);
                        Log.v("log_tag", "CityName" + CityName);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    // 
 is for new line
                    Toast.makeText(
                            getApplicationContext(),
                            "Your Location is - 
Lat: " + latitude
                                    + "
Long: " + longitude, Toast.LENGTH_LONG)
                            .show();
                } else {

                    gps.showSettingsAlert();
                }

            }
        });

But I get Error in below::::

03-11 17:01:46.465: W/System.err(27587): java.io.IOException: Service not Available
03-11 17:01:46.465: W/System.err(27587):    at android.location.Geocoder.getFromLocation(Geocoder.java:136)
03-11 17:01:46.465: W/System.err(27587):    at com.example.gpstracking.AndroidGPSTrackingActivity$1.onClick(AndroidGPSTrackingActivity.java:81)
03-11 17:01:46.465: W/System.err(27587):    at android.view.View.performClick(View.java:4191)
03-11 17:01:46.475: W/System.err(27587):    at android.view.View$PerformClick.run(View.java:17229)
03-11 17:01:46.475: W/System.err(27587):    at android.os.Handler.handleCallback(Handler.java:615)
03-11 17:01:46.475: W/System.err(27587):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-11 17:01:46.475: W/System.err(27587):    at android.os.Looper.loop(Looper.java:137)
03-11 17:01:46.475: W/System.err(27587):    at android.app.ActivityThread.main(ActivityThread.java:4960)
03-11 17:01:46.475: W/System.err(27587):    at java.lang.reflect.Method.invokeNative(Native Method)
03-11 17:01:46.475: W/System.err(27587):    at java.lang.reflect.Method.invoke(Method.java:511)
03-11 17:01:46.475: W/System.err(27587):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
03-11 17:01:46.475: W/System.err(27587):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
03-11 17:01:46.475: W/System.err(27587):    at dalvik.system.NativeStart.main(Native Method)
03-11 17:02:21.335: W/IInputConnectionWrapper(27587): getSelectedText on inactive InputConnection
03-11 17:02:21.335: W/IInputConnectionWrapper(27587): setComposingText on inactive InputConnection
03-11 17:02:21.425: W/IInputConnectionWrapper(27587): getExtractedText on inactive InputConnection
question from:https://stackoverflow.com/questions/22323974/how-to-get-city-name-by-latitude-longitude-in-android

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

1 Answer

0 votes
by (71.8m points)

Use This:

 Geocoder geocoder = new Geocoder(this, Locale.getDefault());
 List<Address> addresses = geocoder.getFromLocation(MyLat, MyLong, 1);
 String cityName = addresses.get(0).getAddressLine(0);
 String stateName = addresses.get(0).getAddressLine(1);
 String countryName = addresses.get(0).getAddressLine(2);

For more in detailed google map example you check the links below: http://www.demoadda.com/demo/android/load-googlemap_107

And for the background location updates: http://www.demoadda.com/demo/android/download-android-background-location-update-service-demo_21


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

...