在Android的经度;如何让城市名称由纬度和放大器?经度、纬度、放大器、名称

2023-09-06 04:50:25 作者:心碎的声音敲打着每次难舍

我想城市名由当前的位置,但我有纬度和longitude.how得到它?我用按一下按钮,然后我得到双重价值的经度和纬度。我的code中。请到帮助我。

谢谢!

 按钮btnShowLocation;

    GPSTracker GPS;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        btnShowLocation =(按钮)findViewById(R.id.btnShowLocation);

        //显示地点按钮点击事件
        btnShowLocation.setOnClickListener(新View.OnClickListener(){

            @覆盖
            公共无效的onClick(视图v){
                //创建类对象
                GPS =新GPSTracker(AndroidGPSTrackingActivity.this);

                //检查是否启用GPS
                如果(gps.canGetLocation()){

                    双纬度= gps.getLatitude();
                    双经度= gps.getLongitude();

                    // \ n是新行
                    Toast.makeText(getApplicationContext(),您现在的位置是 -  \新工人艺术团:+纬度+\ nLong:+经度,Toast.LENGTH_LONG).show();
                }其他{

                    gps.showSettingsAlert();
                }

            }
        });
 

我编辑跌破code,但没有获得城市名,请帮助我!

 按钮btnShowLocation;

    GPSTracker GPS;
    字符串城市名;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        btnShowLocation =(按钮)findViewById(R.id.btnShowLocation);

        //显示地点按钮点击事件
        btnShowLocation.setOnClickListener(新View.OnClickListener(){

            @覆盖
            公共无效的onClick(视图v){
                //创建类对象
                GPS =新GPSTracker(AndroidGPSTrackingActivity.this);

                //检查是否启用GPS
                如果(gps.canGetLocation()){

                    双纬度= gps.getLatitude();
                    双经度= gps.getLongitude();

                    地理codeR地理codeR =新的地缘codeR(
                            AndroidGPSTrackingActivity.this,语言环境
                                    .getDefault());
                    名单<地址>地址;
                    尝试 {
                        Log.v(log_tag,纬度+纬度);
                        Log.v(log_tag,经度+经度);
                        地址=地理coder.getFromLocation(纬度,
                                经度,1);
                        Log.v(log_tag,地址+)_ ++++地址);
                        城市名= addresses.get(0).getAddressLine(0);
                        Log.v(log_tag,城市名+城市名);
                    }赶上(IOException异常E){
                        // TODO自动生成的catch块
                        e.printStackTrace();
                    }

                    // \ n是新行
                    Toast.makeText(
                            getApplicationContext(),
                            您现在的位置是 -  \新工人艺术团:+纬度
                                    +\ nLong:+经度,Toast.LENGTH_LONG)
                            。显示();
                } 其他 {

                    gps.showSettingsAlert();
                }

            }
        });
 

不过,我在下面得到错误::::

  03-11 17:01:46.465:W / System.err的(27587):java.io.IOException异常:服务不可用
03-11 17:01:46.465:W / System.err的(27587):在android.location.Geo coder.getFromLocation(GEO coder.java:136)
03-11 17:01:46.465:W / System.err的(27587):在com.example.gpstracking.AndroidGPSTrackingActivity $ 1.onClick(AndroidGPSTrackingActivity.java:81)
03-11 17:01:46.465:W / System.err的(27587):在android.view.View.performClick(View.java:4191)
03-11 17:01:46.475:W / System.err的(27587):在android.view.View $ PerformClick.run(View.java:17229)
03-11 17:01:46.475:W / System.err的(27587):在android.os.Handler.handleCallback(Handler.java:615)
03-11 17:01:46.475:W / System.err的(27587):在android.os.Handler.dispatchMessage(Handler.java:92)
03-11 17:01:46.475:W / System.err的(27587):在android.os.Looper.loop(Looper.java:137)
03-11 17:01:46.475:W / System.err的(27587):在android.app.ActivityThread.main(ActivityThread.java:4960)
03-11 17:01:46.475:W / System.err的(27587):在java.lang.reflect.Method.invokeNative(本机方法)
03-11 17:01:46.475:W / System.err的(27587):在java.lang.reflect.Method.invoke(Method.java:511)
03-11 17:01:46.475:W / System.err的(27587):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1038)
03-11 17:01:46.475:W / System.err的(27587):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
03-11 17:01:46.475:W / System.err的(27587):在dalvik.system.NativeStart.main(本机方法)
03-11 17:02:21.335:W / IInputConnectionWrapper(27587):getSelectedText上的非活动InputConnection
03-11 17:02:21.335:W / IInputConnectionWrapper(27587):setComposingText上的非活动InputConnection
03-11 17:02:21.425:W / IInputConnectionWrapper(27587):getExtractedText上的非活动InputConnection
 
合肥市 经度117.282699纬度31.866942在哪

解决方案

使用此:

 地理codeR地理codeR =新的地缘codeR(这一点,Locale.getDefault());
 名单<地址>地址=地理coder.getFromLocation(MyLat,MyLong,1);
 串的cityName = addresses.get(0).getAddressLine(0);
 串Statename的= addresses.get(0).getAddressLine(1);
 串国家名称= addresses.get(0).getAddressLine(2);
 

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

                    // \n is for new line
                    Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + 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();
                    }

                    // \n is for new line
                    Toast.makeText(
                            getApplicationContext(),
                            "Your Location is - \nLat: " + latitude
                                    + "\nLong: " + 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

解决方案

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