1

我正在尝试使用 htc hope c 反转地理编码,在运行应用程序时,它的抛出服务不可用异常。但是 GeoCoder.isPresent() 正在返回“真”。请帮助我找出问题所在。

这是我的代码:

public class MainActivity extends Activity {

LocationManager lManager;
String provider;
 Location location ;

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();

    provider = lManager.getBestProvider(criteria, false);
    if(provider!=null && !provider.equals("")){

        // Get the location from the given provider
      location = lManager.getLastKnownLocation(provider);
    }

    boolean geoCoder = false;
    Geocoder geo = new Geocoder(this, Locale.getDefault());
     geoCoder = Geocoder.isPresent();
     System.out.println("GEO CODER : "+geoCoder);

     try {
        List<Address> address = geo.getFromLocation(location.getLatitude(), location.getLongitude(), 3);
        System.out.println("Size------------- "+address.size());

        /*Address addr = address.get(0);
        System.out.println("City "+addr.getLocality());*/
    } catch (IOException e) {
        // TODO Auto-generated catch block
        System.out.println("in here--------------");
        e.printStackTrace();
    }

}

日志:

03-16 10:33:58.609: I/System.out(30398): GEO CODER : true
03-16 10:33:58.609: I/System.out(30398): in here--------------
03-16 10:33:58.609: W/System.err(30398): java.io.IOException: Service not Available
03-16 10:33:58.619: W/System.err(30398):    at  android.location.Geocoder.getFromLocation(Geocoder.java:136)
03-16 10:33:58.619: W/System.err(30398):    at   com.example.geocoder.MainActivity.onCreate(MainActivity.java:46)
03-16 10:33:58.619: W/System.err(30398):    at   android.app.Activity.performCreate(Activity.java:4538)
03-16 10:33:58.629: W/System.err(30398):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
03-16 10:33:58.629: W/System.err(30398):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
03-16 10:33:58.629: W/System.err(30398):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240)
03-16 10:33:58.629: W/System.err(30398):    at android.app.ActivityThread.access$600(ActivityThread.java:139)
03-16 10:33:58.629: W/System.err(30398):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
03-16 10:33:58.629: W/System.err(30398):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-16 10:33:58.629: W/System.err(30398):    at android.os.Looper.loop(Looper.java:156)
03-16 10:33:58.629: W/System.err(30398):    at android.app.ActivityThread.main(ActivityThread.java:4987)
03-16 10:33:58.639: W/System.err(30398):    at java.lang.reflect.Method.invokeNative(Native Method)
03-16 10:33:58.639: W/System.err(30398):    at java.lang.reflect.Method.invoke(Method.java:511)
03-16 10:33:58.639: W/System.err(30398):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-16 10:33:58.639: W/System.err(30398):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-16 10:33:58.649: W/System.err(30398):    at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

1

@smk 给了你一个有用的链接。为了使您免于进行大量研究,我将尝试简短地回答您。地理编码器并不总是返回一个值。您可以尝试在 for 循环中发送 3 次请求。我应该至少能回来一次。如果不是,那么它们可能是连接问题,也可能是服务器未回复您的请求等其他问题。

我也有一个while循环,但我曾经最多尝试10次。有时,即使它连接到互联网,它也不会返回任何东西。然后,我每次都使用这种更可靠的方式来获取地址:

我曾经获取纬度和经度,然后请求谷歌服务器,回复一个 JSOn 对象,其中包含有关位置坐标的各种信息。这是功能:

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();

}

希望这可以帮助。我也厌倦了依赖 Geocoder。这对我有用。虽然它可能比地理编码器慢。您只需将 URL 中的经纬度坐标替换为您拥有的坐标即可。尝试在 Web 浏览器中查看返回的 JSON 对象。您将看到如何提取地址字符串。尝试并阅读这些线程:

Geocoder 并不总是返回值,并且geocoder.getFromLocationName 仅返回 null

于 2013-03-16T05:26:33.743 回答