【问题标题】:cannot get Locality using Geocoder and GPS in android无法在 android 中使用 Geocoder 和 GPS 获取位置
【发布时间】:2014-12-12 09:39:48
【问题描述】:

您好,我正在尝试使用 geoCoder 位置和 gps 获取城市、位置、纬度、经度

它可以在最大的设备上运行,但不能在 Galaxy s4、sony 或三星平板电脑上运行

更具体地说,它不适用于 xperia z1(android 4.1.1.,型号 C1505)

这里是地理编码器的代码 sn-p

Geocoder geocoder = new Geocoder(mContext, Locale.getDefault());
     List<Address> addresses;
    try {
        addresses = geocoder.getFromLocation(lat, longi, 1);
        Log.e("geocoder size", addresses.get(0).getLocality()+"");


        //setCityName(addresses.get(0).getLocality());
        //setStateName(addresses.get(0).getAdminArea());
        //setCountryName(addresses.get(0).getCountryName());

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

    }

【问题讨论】:

    标签: android geocoding google-geocoder


    【解决方案1】:

    其实哥有时候GEOCoder不能完美运行所以你必须这样使用:

    JSONObject ret = getLocationInfo(latlng_Source.latitude, latlng_Source.longitude);
                    Log.e("address string", ret.toString());
    
                    JSONObject location;
    
                    try
                    {
                        location = ret.getJSONArray("results").getJSONObject(0);
                        mCuurentAddress = location.getString("formatted_address");
                        Log.d("test", "formattted address:" + mCuurentAddress);
                    }
                    catch (JSONException e1)
                    {
                        e1.printStackTrace();
    
                    }
    
    
    
    public  JSONObject getLocationInfo(double lat, double lng)
        {
    
            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;
        }
    

    我希望这会帮助你。它的永久解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多