Java Geocoder仅返回空对象

Java Geocoder仅返回空对象,java,android,geocoding,Java,Android,Geocoding,我正在制作一个android应用程序,该应用程序的一个特点是,它可以将输入的街道地址地理编码为其纬度和经度,然后在地图上标记这一点 我已经尝试了来自各地的各种解决方案,但我无法让它发挥作用 public LatLng getLocationFromAddress(String strAddress) { Geocoder coder = new Geocoder(getApplicationContext(), Locale.getDefault()); List<A

我正在制作一个android应用程序,该应用程序的一个特点是,它可以将输入的街道地址地理编码为其纬度和经度,然后在地图上标记这一点

我已经尝试了来自各地的各种解决方案,但我无法让它发挥作用

  public LatLng getLocationFromAddress(String strAddress) {

    Geocoder coder = new Geocoder(getApplicationContext(), Locale.getDefault());
    List<Address> address;

    try {
        address = coder.getFromLocationName(strAddress, 1);
        while(address.size()==0){
            address = coder.getFromLocationName(strAddress,1);
        }
        Address location = address.get(0);
        double lat = location.getLatitude();
        double lng = location.getLongitude();
        return new LatLng(lat,lng);
    } catch (Exception e) {
        return null;
    }
}
试试这个

(注意:您将
getLocationFromAddress()
设置为公共方法,因此我无法确定它是否位于调用它的另一个类中,因此我将传递上下文以确定。如果
getLocationFromAddress()
位于活动类中,则只需使用“TheNameOfActivity.this”而不是
getApplicationContext()


一定要用活动的实际名称替换“TheNameOfActivity”

您正在接受异常。至少,打印它的stack trace.BTW,
address=…
和while循环可以在不重复自己作为
do/while
循环的情况下编写。仍然会出现相同的错误,如果我实现了您建议的第二个代码段,应用程序不会崩溃,但它仍然无法正常工作:(可能我以前需要检查互联网或其他东西的权限吗?我只是想不通为什么会失败!@jammymister::我犯了一个错误,留下了
返回新的LatLng(lat,lng);
在我的代码中,我希望你能抓住它!(我应该删除它!)检查我的编辑以查看我所做的更改。除此之外,我的应用程序请求
互联网
权限。如果您使用的是“谷歌地图Android API v2”,则无需明确请求
访问精细位置
访问粗略位置
@jammymmister::以上代码与我在应用程序中使用的代码之间的唯一区别(工作正常!)我不使用
Locale.getDefault()
初始化我的
GeoCoder
对象。@jammymister::你在真实设备上测试吗?互联网连接正常吗?感谢Barns的帮助!我一直在真实设备上测试,是的,我重新启动了设备作为最后的希望,不知怎的修复了它哈哈!我使用了LngLat的PickupPoints列表,因为我正在尝试读入firebase的作业列表,然后在我的地图上显示拾取位置!PickupPointsLots.add(getLocationFromAddress(这个,“伦敦”);工作非常完美,但对于(作业j:jobs){LatLng loca=getLocationFromAddress(这个,j.getPickupLoc());PickupPointsLots.add(loca);}什么也没做,我想你不明白为什么?再次感谢!
       pickupPointsPlots.add(getLocationFromAddress("london"));
        for (int i = 0; i < pickupPointsPlots.size(); i++) {
        LatLng position = pickupPointsPlots.get(i);
        MarkerOptions options = new MarkerOptions().position(position);

        googleMap.addMarker(options);

    }

} 
 java.lang.IllegalArgumentException: latlng cannot be null - a position is required.
public LatLng getLocationFromAddress(Context context, String strAddress) {
    LatLng latLng = null;
    try {
        Geocoder coder = new Geocoder(context, Locale.getDefault());
        List<Address> address = coder.getFromLocationName(strAddress, 1);
        // will only iterate through address list when the geocoder was successful
        for(Address a : address){
            latLng = new LatLng(a.getLatitude, a.getLongitude);
            Log.e(TAG, "Country Name = " + a.getCountryName());
        }
    } catch (Exception e) {
        //Log that error!
        Log.e(TAG, e.getMessage());
    }
    //will return null if an address was not found
    return latLng;
}
    ....
    LatLng latLng = getLocationFromAddress(TheNameOfActivity.this, "london");
    if(latLng != null){
        MarkerOptions options = new MarkerOptions().position(latLng);
        if(googleMap != null){
            googleMap.addMarker(options);
        {
    }
}