Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.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不可用?_Java_Android_Map - Fatal编程技术网

Java Android Geocoder不可用?

Java Android Geocoder不可用?,java,android,map,Java,Android,Map,我使用这个方法通过地址获取位置,我使用的是安卓4.2.2设备 addresses = g.getFromLocationName(addressString, 1); 当我第一次尝试获取地址位置时,它会给出实际结果。但几天后,它说,Geocoder服务不可用。 我的问题是我使用了相同的设备,相同的api密钥和它的工作,但现在它不工作了。为什么? 如何解决问题。您是否超过了最大查询限制?当它停止工作时,您是否看到任何这样的日志?还是任何日志?这是谷歌自身的问题 如果重新启动设备,则可以解决此问题

我使用这个方法通过地址获取位置,我使用的是安卓4.2.2设备

addresses = g.getFromLocationName(addressString, 1);
当我第一次尝试获取地址位置时,它会给出实际结果。但几天后,它说,
Geocoder
服务不可用。 我的问题是我使用了相同的设备,相同的api密钥和它的工作,但现在它不工作了。为什么?
如何解决问题。

您是否超过了最大查询限制?当它停止工作时,您是否看到任何这样的日志?还是任何日志?

这是谷歌自身的问题

如果重新启动设备,则可以解决此问题。但这并不是用户的最佳解决方案

您可以这样做:

public void getCityName(){ 
    if (running)
        return;

    new AsyncTask<Void, Void, String>(){
        String streetName = null;
        //String cityName = null;

        protected void onPreExecute(){
            running = true;
        };

        @Override
        protected String doInBackground(Void... params){
            if (Geocoder.isPresent()){ //if geocoder network is not crashed
                try{
                    Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
                    List<Address> addresses = geocoder.getFromLocation(locationAll.getLatitude(), locationAll.getLongitude(), 1);
                    if (addresses.size() > 0){
                        cityName = addresses.get(0).getLocality();
                    }
                    for (Address address : addresses) {
                        System.out.println("my location .."+address.getAddressLine(0));
                        streetName = address.getAddressLine(0);

                    }
                }
                catch (Exception ignored){
                    // after a while, Geocoder start to throw "Service not available" exception. really weird since it was working before (same device, same Android version etc..
                }
            }

            if (cityName != null){ // Geocoder succeed

                return cityName;
            }
            else // Geocoder failed
            {
                return fetchCityNameUsingGoogleMap();
            }
        }

        // Geocoder failed :-(
        // My B Plan : Google Map
        private String fetchCityNameUsingGoogleMap()
        {
            String googleMapUrl = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + locationAll.getLatitude() + ","
                    + locationAll.getLongitude() + "&sensor=false&language=fr";


            try
            {
                JSONObject googleMapResponse = new JSONObject(ANDROID_HTTP_CLIENT.execute(new HttpGet(googleMapUrl),
                        new BasicResponseHandler()));

                // many nested loops.. not great -> use expression instead
                // loop among all results
                JSONArray results = (JSONArray) googleMapResponse.get("results");
                for (int i = 0; i < results.length(); i++)
                {
                    // loop among all addresses within this result
                    JSONObject result = results.getJSONObject(i);
                    if (result.has("address_components"))
                    {
                        JSONArray addressComponents = result.getJSONArray("address_components");
                        // loop among all address component to find a 'locality' or 'sublocality'
                        for (int j = 0; j < addressComponents.length(); j++)
                        {
                            JSONObject addressComponent = addressComponents.getJSONObject(j);
                            if (result.has("types"))
                            {
                                JSONArray types = addressComponent.getJSONArray("types");

                                // search for locality and sublocality
                                cityName = null;

                                for (int k = 0; k < types.length(); k++)
                                {
                                    if ("locality".equals(types.getString(k)) && cityName == null)
                                    {
                                        if (addressComponent.has("long_name"))
                                        {
                                            cityName = addressComponent.getString("long_name");
                                        }
                                        else if (addressComponent.has("short_name"))
                                        {
                                            cityName = addressComponent.getString("short_name");
                                        }
                                    }
                                    if ("sublocality".equals(types.getString(k)))
                                    {
                                        if (addressComponent.has("long_name"))
                                        {
                                            cityName = addressComponent.getString("long_name");
                                        }
                                        else if (addressComponent.has("short_name"))
                                        {
                                            cityName = addressComponent.getString("short_name");
                                        }
                                    }
                                }
                                if (cityName != null)
                                {

                                    return cityName;

                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ignored)
            {
                ignored.printStackTrace();
            }
            return null;
        }

        protected void onPostExecute(String cityName)
        {
            running = false;
            if (cityName != null)
            {
                // Do something with cityName
                Log.i("GeocoderHelper", cityName);
            }
        };     
}.execute();

}
http://maps.googleapis.com/maps/api/geocode/json?address=NewYork&sensor=false
public void getCityName(){
如果(正在运行)
返回;
新建异步任务(){
字符串streetName=null;
//字符串cityName=null;
受保护的void onPreExecute(){
运行=真;
};
@凌驾
受保护字符串doInBackground(无效…参数){
如果(Geocoder.isPresent()){//如果Geocoder网络未崩溃
试一试{
Geocoder Geocoder=新的Geocoder(getApplicationContext(),Locale.getDefault());
列表地址=geocoder.getFromLocation(locationAll.getLatitude(),locationAll.getLatitude(),1);
如果(地址.size()>0){
cityName=addresses.get(0.getLocality();
}
收件人(地址:地址){
System.out.println(“我的位置..”+address.getAddressLine(0));
streetName=address.getAddressLine(0);
}
}
捕获(忽略异常){
//过了一会儿,Geocoder开始抛出“服务不可用”异常。真的很奇怪,因为它以前工作过(相同的设备,相同的Android版本等)。。
}
}
如果(cityName!=null){//Geocoder成功
返回城市名称;
}
else//Geocoder失败
{
返回fetchCityNameUsingGoogleMap();
}
}
//地理编码器失败:-(
//我的B计划:谷歌地图
私有字符串fetchCityNameUsingGoogleMap()
{
字符串googleMapUrl=”http://maps.googleapis.com/maps/api/geocode/json?latlng=“+locationAll.getLatitude()+”,”
+locationAll.getLongitude()+“&sensor=false&language=fr”;
尝试
{
JSONObject googleMapResponse=新的JSONObject(ANDROID_HTTP_CLIENT.execute)(新的HttpGet(googleMapUrl),
新BasicResponseHandler());
//许多嵌套循环..不太好->改用表达式
//在所有结果之间循环
JSONArray结果=(JSONArray)googleMapResponse.get(“结果”);
对于(int i=0;i"The Geocoder class requires a backend service that is not included in the core android              framework. The Geocoder query methods will return an empty list if there no backend service in the platform. Use the isPresent() method to determine whether a Geocoder implementation    exists."
http://maps.googleapis.com/maps/api/geocode/json?address=NewYork&sensor=false
http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=false