如何在 android studio JAVA 的 2 个文本视图中分别获取经纬度?

How to get latitude and longitude separately in 2 textviews in android studio JAVA?

由此我在单个文本视图中获取纬度和经度。但我希望纬度在 1 个文本视图中,经度在另一个文本视图中。请帮我解决这个问题。

我的主要activity

    package com.shopping.myapplication;

    import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    Button addressButton;
    TextView addressTV;
    TextView latLongTV;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        addressTV = (TextView) findViewById(R.id.addressTV);
       

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

        addressButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {

                EditText editText = (EditText) findViewById(R.id.addressET);
                String address = editText.getText().toString();

                GeocodingLocation locationAddress = new GeocodingLocation();
                locationAddress.getAddressFromLocation(address,
                        getApplicationContext(), new GeocoderHandler());
            }
        });

    }

    private class GeocoderHandler extends Handler {
        @Override
        public void handleMessage(Message message) {
            String locationAddress;
            switch (message.what) {
                case 1:
                    Bundle bundle = message.getData();
                    locationAddress = bundle.getString("address");
                    break;
                default:
                    locationAddress = null;
            }
            latLongTV.setText(locationAddress);
        }
    }
    }




我的地理编码位置class.java

    package com.shopping.myapplication;

import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

public class GeocodingLocation {

    private static final String TAG = "GeocodingLocation";

    public static void getAddressFromLocation(final String locationAddress,
                                              final Context context, final Handler handler) {
        Thread thread = new Thread() {
            @Override
            public void run() {
                Geocoder geocoder = new Geocoder(context, Locale.getDefault());
                String result = null;

                try {
                    List
                            addressList = geocoder.getFromLocationName(locationAddress, 1);
                    if (addressList != null && addressList.size() > 0) {
                        Address address = (Address) addressList.get(0);
                        StringBuilder sb = new StringBuilder();



                        sb.append(address.getLatitude()).append("\n");
                        sb.append(address.getLongitude()).append("\n");
                        result = sb.toString();
                    }
                } catch (IOException e) {
                    Log.e(TAG, "Unable to connect to Geocoder", e);
                } finally {
                    Message message = Message.obtain();
                    message.setTarget(handler);
                    if (result != null) {
                        message.what = 1;
                        Bundle bundle = new Bundle();
                        result = "Address: " + locationAddress +
                                "\n\nLatitude and Longitude :\n" + result;
                        bundle.putString("address", result);
                        message.setData(bundle);
                    } else {
                        message.what = 1;
                        Bundle bundle = new Bundle();
                        result = "Address: " + locationAddress +
                                "\n Unable to get Latitude and Longitude for this address location.";
                        bundle.putString("address", result);
                        message.setData(bundle);
                    }
                    message.sendToTarget();
                }
            }
        };
        thread.start();
    }
}


由此我在单个文本视图中获取纬度和经度。但我希望纬度在 1 个文本视图中,经度在另一个文本视图中。请帮我解决这个问题。

如果我错了,请纠正我,但您似乎将地址的长度作为单个字符串。在线程的 run() 方法中,有这些行:

sb.append(address.getLatitude()).append("\n");
sb.append(address.getLongitude()).append("\n");
result = sb.toString();

如您所见,您可以直接从这里的地址对象中获取经纬度!有很多方法可以解决这个问题。如果您愿意,可以将纬度和经度值粘贴到包中。根据 documentationgetLatitude()getLongitude() return double:

public double getLatitude()

您可以尝试将值独立地放入包中,并为它们提供适当的键。或者,如果你不想弄乱地理代码,你可以在 onCreate() 方法中简单地做:

String address = editText.getText().toString();
String[] values = address.split("\n"); //we know that we need to split by \n because we did sb.append(address.getLatitude()).append("\n")
//N.b: make sure to include \n double backslash!
String lat = values[0];
String long = values[1];

希望这对您有所帮助!如果这有效,请告诉我 :D

编辑:我已经尝试自己实现它,它似乎没有问题!这是屏幕截图:

这里是主要的 activity:

import androidx.appcompat.app.AppCompatActivity;

import android.location.Address;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    Button addressButton;
    TextView addressTV;
    TextView latTV;
    TextView longTV;
    EditText editText;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        addressTV = findViewById(R.id.addressTV);
        latTV = findViewById(R.id.latTV);
        longTV = findViewById(R.id.longTV);
        addressButton = findViewById(R.id.addressButton);
        editText = (EditText) findViewById(R.id.addressET);

        addressButton.setOnClickListener(arg0 -> {
            String address = editText.getText().toString();

            GeocodingLocation locationAddress = new GeocodingLocation();
            locationAddress.getAddressFromLocation(address,
                    getApplicationContext(), new GeocoderHandler());
        });
    }

    private class GeocoderHandler extends Handler {
        @Override
        public void handleMessage(Message message) {
            switch (message.what) {
                case 1:
                    Bundle bundle = message.getData();
                    Address address = bundle.getParcelable("address");
                    latTV.setText(address.getLatitude()+"");
                    longTV.setText(address.getLongitude()+"");
                    break;
                case 2:
                    addressTV.setText("unable to get address");
            }

        }
    }
}

注意:我在这里为您简化了一些内容,但本质上是一样的。然而重要的一点是,您可以将 Parcable 对象直接放入包中,并且由于 Address 对象是 Parcable,您可以将 Address 对象直接放入包中!

这里是地理编码位置class:

import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

public class GeocodingLocation {

    private static final String TAG = "GeocodingLocation";

    public static void getAddressFromLocation(final String locationAddress,
                                              final Context context, final Handler handler) {
        Thread thread = new Thread() {
            @Override
            public void run() {
                Geocoder geocoder = new Geocoder(context, Locale.getDefault());
                Message message = Message.obtain();
                message.setTarget(handler);

                try {
                    List addressList = geocoder.getFromLocationName(locationAddress, 1);
                    if (addressList != null && addressList.size() > 0) {
                        Address address = (Address) addressList.get(0);

                        message.what = 1;
                        Bundle bundle = new Bundle();
                        bundle.putParcelable("address", address);
                        message.setData(bundle);
                    }
                } catch (IOException e) {
                    Log.e(TAG, "IOException", e);
                    message.what = 2;
                }
                message.sendToTarget();
            }
        };
        thread.start();
    }
}

注意:我在这里也稍微简化了您的代码。就我个人而言(我想大多数人都会同意),我喜欢以使用最少缩进的方式编写代码,但仍然可以解释。我没有做 try catch finally,只是将正确的代码片段分别放在 trycatch 中,因此根本不再需要 finally 子句。

在收到带有地址字符串的消息之前,在 catch 子句中,您会将字符串“unable to get address for location”放入带有键“address”的包中:

Bundle bundle = new Bundle();
result = "Address: " + locationAddress +
         "\n\nLatitude and Longitude :\n" + result;
bundle.putString("address", result);

result 字符串不包含地址,而是一条错误消息,因此将它与“地址”键捆绑在一起可能不是一个好主意(因为它是一条错误消息)。我做的略有不同,我将 message.what 属性 设置为 message.what = 2;,并在 activity 内的 switch 中将其设为 case

我希望这对你有进一步的帮助! :D