android - 如何在android中通过纬度和经度获取城市名称?

标签 android map gps location

我想按当前位置的城市名称,但我有纬度和经度。如何得到它?我使用按钮单击然后我得到双值纬度和经度。我在下面的代码。请帮助我。

谢谢!!

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();

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

                    gps.showSettingsAlert();
                }

            }
        });

我编辑了下面的代码,但没有得到城市名,请帮帮我!!!

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

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

                    gps.showSettingsAlert();
                }

            }
        });

但我在下面得到错误::::

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

最佳答案

使用这个:

 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);

有关详细的谷歌地图示例,请查看以下链接: http://www.demoadda.com/demo/android/load-googlemap_107

对于后台位置更新: http://www.demoadda.com/demo/android/download-android-background-location-update-service-demo_21

关于android - 如何在android中通过纬度和经度获取城市名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22323974/

相关文章:

google-maps - 创建地理边界框的简单 map 界面?

scala - 如何在 scala 中等待异步任务完成?

java - 使用比较器对 map 进行排序

ios - 尽管用户已终止应用程序并检查 iOS 中的设备位置,如何保持 iOS 应用程序进程始终运行?

java - 获取两点之间的距离并计算出速度

android - requestLocationUpdates 在一些距离改变/行进时获取更新

android - 资源链接失败/Android Studio College Project

android - Genymotion android 模拟器显示很多日志

android - Youtube 视频未显示在 RecyclerView 中

android - 设置 listView 分隔线的宽度