gpt4 book ai didi

java - 我想从特定地址分别获取经度和纬度

转载 作者:行者123 更新时间:2023-12-02 02:48:56 25 4
gpt4 key购买 nike

我按照 Android 地理编码中的示例获取地址教程的纬度经度,它有效,但我想分别获取经度和纬度坐标,但无法。

这是我的代码:

public class MainActivity extends AppCompatActivity {
Button addressButton;
TextView addressTV;
TextView latLongTV;
static TextView txtLatitude;
TextView txtLongitude;


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


addressTV = (TextView) findViewById(R.id.addressTV);
latLongTV = (TextView) findViewById(R.id.latLongTV);
txtLatitude = (TextView) findViewById(R.id.txtLatitude);
txtLatitude= (TextView) findViewById(R.id.txtLongitude);

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,latitude ,longitude ;

switch (message.what) {
case 1:
Bundle bundle = message.getData();
locationAddress = bundle.getString("address");
latitude = bundle.getString("latitude");
longitude = bundle.getString("longitude");
break;
default:
locationAddress = null;
latitude = null;
longitude= null;
}
latLongTV.setText(locationAddress);
txtLatitude.setText(latitude);
// txtLongitude.setText(longitude);
}
}

private static class GeocodingLocation {


private static final String TAG = "GeocodingLocation";

public 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;
String strLatitude = null, strLongitude = null;
try {
List<Address> addressList = geocoder.getFromLocationName(locationAddress, 1);
if (addressList != null && addressList.size() > 0) {
Address address = addressList.get(0);
double latitude = address.getLatitude();
double longitude = address.getLongitude();
StringBuilder sb = new StringBuilder();
sb.append(address.getLatitude()).append("\n");
sb.append(address.getLongitude()).append("\n");
result = sb.toString();
strLatitude = String.valueOf(latitude);
strLongitude= String.valueOf(longitude);
}
} 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);
bundle.putString("Latitude", strLatitude);
bundle.putString("Longitude", strLongitude);
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();
}
}

最佳答案

根据this answer , bundle key 区分大小写。您可以使用以下命令设置字符串:

bundle.putString("Latitude", strLatitude);
bundle.putString("Longitude", strLongitude);

但是然后让他们:

latitude = bundle.getString("latitude");
longitude = bundle.getString("longitude");

请注意,您输入的是大写的 L,但输入的是小写的 l

关于java - 我想从特定地址分别获取经度和纬度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44127986/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com