Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android Geocoder getFromLocationName始终返回null_Java_Android_Eclipse_Android Mapview_Google Maps Android Api 2 - Fatal编程技术网

Java Android Geocoder getFromLocationName始终返回null

Java Android Geocoder getFromLocationName始终返回null,java,android,eclipse,android-mapview,google-maps-android-api-2,Java,Android,Eclipse,Android Mapview,Google Maps Android Api 2,我一直在尝试对字符串进行地理编码以获取其坐标,但我的程序总是崩溃,因为当我尝试使用getFromLocationName()时,它返回null。我已经试着修复这个问题好几个小时了,但什么都没用。这是我的密码 public class MainActivity extends Activity { private GoogleMap mMap; List<Address> addresses; MarkerOptions miami; String

我一直在尝试对字符串进行地理编码以获取其坐标,但我的程序总是崩溃,因为当我尝试使用
getFromLocationName()
时,它返回
null
。我已经试着修复这个问题好几个小时了,但什么都没用。这是我的密码

public class MainActivity extends Activity {

    private GoogleMap mMap;

    List<Address> addresses;
    MarkerOptions miami;
    String myLocation = "Miami,Florida";

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (mMap == null) {
            mMap = ((MapFragment) getFragmentManager().findFragmentById(
                    R.id.map)).getMap();
        }

        if (mMap != null) {

            Geocoder geocoder = new Geocoder(this);


            double latitude = 0;
            double longitude = 0;

            while(addresses==null){
            try {
                addresses = geocoder.getFromLocationName(myLocation, 1);

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            }
            Address address = addresses.get(0);
            if (addresses.size() > 0) {
                latitude = address.getLatitude();
                longitude = address.getLongitude();
            }
            LatLng City = new LatLng(latitude, longitude);

            miami = new MarkerOptions().position(City).title("Miami");

            mMap.addMarker(miami);

            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(City, 15));

        }
}
公共类MainActivity扩展活动{
私有谷歌地图;
列出地址;
迈阿密MarkerOptions;
字符串myLocation=“迈阿密,佛罗里达”;
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
如果(mMap==null){
mMap=((MapFragment)getFragmentManager().findFragmentById(
R.id.map)).getMap();
}
如果(mMap!=null){
Geocoder Geocoder=新的Geocoder(本);
双纬度=0;
双经度=0;
while(地址==null){
试一试{
地址=geocoder.getFromLocationName(myLocation,1);
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
地址=地址。获取(0);
如果(地址.size()>0){
纬度=地址。getLatitude();
经度=地址。getLongitude();
}
LatLng City=新LatLng(纬度、经度);
迈阿密=新MarkerOptions().职位(城市).title(“迈阿密”);
mMap.addMarker(迈阿密);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(城市,15));
}
}

Geocoder并不总是返回值。您可以尝试在for循环中发送请求3次。我应该能够至少返回一次。如果不是,则可能是连接问题,也可能是其他问题,如服务器无法回复您的请求。请尝试查看以下线程:

更新:

我也有一个while循环,但我过去最多尝试10次。有时,即使连接到internet,它也不会返回任何内容。然后,我每次都使用更可靠的方法获取地址:

public JSONObject getLocationInfo() {

        HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?latlng="+lat+","+lng+"&sensor=true");
        HttpClient client = new DefaultHttpClient();
        HttpResponse response;
        StringBuilder stringBuilder = new StringBuilder();

        try {
            response = client.execute(httpGet);
            HttpEntity entity = response.getEntity();
            InputStream stream = entity.getContent();
            int b;
            while ((b = stream.read()) != -1) {
                stringBuilder.append((char) b);
            }
        } catch (ClientProtocolException e) {
            } catch (IOException e) {
        }

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject = new JSONObject(stringBuilder.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonObject;
    }
    
我这样称呼它:

JSONObject ret = getLocationInfo(); 
JSONObject location;
String location_string;
try {
    location = ret.getJSONArray("results").getJSONObject(0);
    location_string = location.getString("formatted_address");
    Log.d("test", "formattted address:" + location_string);
} catch (JSONException e1) {
    e1.printStackTrace();

}
希望这有帮助。我也厌倦了依赖地理编码器。这对我来说很有效。
如果您用纬度和经度坐标替换URL,并在web浏览器中看到返回的JSON对象,您将看到刚才发生的事情。

有人想回答这个问题吗?但我有一个while循环,以确保我得到geocoder的值,它将永远循环。请参阅我更新的答案。希望它能解决您试图解决的问题我相信。我也面临同样的问题。我发现设备上的
网络定位器
由于某种原因(不在我们手中)已停止工作或死亡。您可以通过重新启动设备来确认这一点。它现在必须工作(因为
网络定位器也重新启动
)。有3种情况。1.你第一次得不到位置,然后你尝试循环第四次/第五次,等等。2.无论你循环多少次,你都得不到任何位置详细信息,因此你假设服务可能暂时停止,并采用上述解决方案。3.正如我在上面的评论中解释的,除非重新启动,否则它永远不会工作您的设备,这意味着我将始终执行步骤1和步骤2,每次可能需要2-5秒。没有人愿意等待这么多时间来获取像
地址行这样的单个数据。对于案例3,有更好的解决方案吗?@Ben See some liek