gpt4 book ai didi

java - 无法将 GeoPoint 转换为地址

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

我正在尝试将 GeoPoint 对象从其纬度和经度值反转为实际的字符串地址。

到目前为止,我将 GeoPoint 对象传递给一个方法,提取纬度和经度值,将地址存储在数组中,第一个地址设置为“add”字符串变量,并重新设置为警报对话框..

这显示了 GeoPoint 'p' 的发送以及该方法的调用方式:

String address = convertGpToLoc(point);

city.setText("Address: " + address + "");

CityClickListner 类:

class CityClickListener extends Activity implements OnClickListener {


private GeoName geoName = null;

CityClickListener(GeoName name) {
this.geoName = name;
}

@Override
public void onClick(View v) {


Double Lattt = geoName.getGeometry().getLocation().getLat();
Double Longgg = geoName.getGeometry().getLocation().getLng();
updateMap(Lattt, Longgg);

AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setView(createView());
builder.setTitle("Details of " + geoName.getName());

builder.setCancelable(true);

builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});

AlertDialog alert = builder.create();
alert.show();

}

convertGpToLoc 方法:

public String convertGpToLoc(GeoPoint p)
{
String add = "";
try {

Geocoder gc = new Geocoder(CityClickListener.this, Locale.ENGLISH);

List<Address> addresses = gc.getFromLocation(
p.getLatitudeE6() / 1E6,
p.getLongitudeE6() / 1E6, 1);


if (addresses.size() < 0)
{
return null;
}

else
{

for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); i++)
{
add += addresses.get(0).getAddressLine(i) + "\n";
}

}

}
catch (IOException e)
{
e.printStackTrace();
}

return add;

}

日志猫:

03-11 15:16:10.952: E/AndroidRuntime(304): FATAL EXCEPTION: main
03-11 15:16:10.952: E/AndroidRuntime(304): java.lang.NullPointerException
03-11 15:16:10.952: E/AndroidRuntime(304): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:120)
03-11 15:16:10.952: E/AndroidRuntime(304): at android.location.GeocoderParams.<init>(GeocoderParams.java:50)
03-11 15:16:10.952: E/AndroidRuntime(304): at android.location.Geocoder.<init>(Geocoder.java:64)
03-11 15:16:10.952: E/AndroidRuntime(304): at com.example.restfulweb.PostalCodeAdapter$CityClickListener.convertGpToLoc(PostalCodeAdapter.java:207)
03-11 15:16:10.952: E/AndroidRuntime(304): at com.example.restfulweb.PostalCodeAdapter$CityClickListener.createView(PostalCodeAdapter.java:175)
03-11 15:16:10.952: E/AndroidRuntime(304): at com.example.restfulweb.PostalCodeAdapter$CityClickListener.onClick(PostalCodeAdapter.java:1 14)
03-11 15:16:10.952: E/AndroidRuntime(304): at android.view.View.performClick(View.java:2408)
03-11 15:16:10.952: E/AndroidRuntime(304): at android.view.View$PerformClick.run(View.java:8816)
03-11 15:16:10.952: E/AndroidRuntime(304): at android.os.Handler.handleCallback(Handler.java:587)
03-11 15:16:10.952: E/AndroidRuntime(304): at android.os.Handler.dispatchMessage(Handler.java:92)
03-11 15:16:10.952: E/AndroidRuntime(304): at android.os.Looper.loop(Looper.java:123)
03-11 15:16:10.952: E/AndroidRuntime(304): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-11 15:16:10.952: E/AndroidRuntime(304): at java.lang.reflect.Method.invokeNative(Native Method)
03-11 15:16:10.952: E/AndroidRuntime(304): at java.lang.reflect.Method.invoke(Method.java:521)
03-11 15:16:10.952: E/AndroidRuntime(304): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-11 15:16:10.952: E/AndroidRuntime(304): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-11 15:16:10.952: E/AndroidRuntime(304): at dalvik.system.NativeStart.main(Native Method)

最佳答案

address = addresses.get(0).getAddressLine(0); city = addresses.get(0).getAddressLine(1); country = addresses.get(0).getAddressLine(2);

如果上面有一些为null,你会得到空指针异常,因为你没有在if block 中使用try catch,

使用下面的代码,

 for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); i++)
{
try{
add += addresses.get(0).getAddressLine(i) + "\n";
}
catch(Exception e)
{}
}

关于java - 无法将 GeoPoint 转换为地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15342249/

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