Java Android地理编码功能不如iOS

Java Android地理编码功能不如iOS,java,android,ios,google-maps,geocoding,Java,Android,Ios,Google Maps,Geocoding,因此,我有一个函数,用于将地址(字符串)转换为坐标 这是iOS中的外观: func setCoords(buildet: BuildingDetail) { let geoCoder = CLGeocoder() geoCoder.geocodeAddressString(buildet.address, completionHandler: {(placemarks: [AnyObject]!, error: NSError!) in

因此,我有一个函数,用于将地址(字符串)转换为坐标

这是iOS中的外观:

func setCoords(buildet: BuildingDetail) {

    let geoCoder = CLGeocoder()

    geoCoder.geocodeAddressString(buildet.address, completionHandler:
        {(placemarks: [AnyObject]!, error: NSError!) in

            if error != nil {
                println("Geocode failed with error: \(error.localizedDescription)")
            } else if placemarks.count > 0 {
                let placemark = placemarks[0] as! CLPlacemark
                let location = placemark.location
                buildet.lat = location.coordinate.latitude
                buildet.lon = location.coordinate.longitude
            }
            self.setupMarker(buildet)
    })
}
这是Android中的外观:

public static double[] getLatLongPositions(String address) throws Exception
{
    int responseCode = 0;
    String api = "http://maps.googleapis.com/maps/api/geocode/xml?address=" + URLEncoder.encode(address, "UTF-8") + "&sensor=true";
    System.out.println("URL : "+api);
    URL url = new URL(api);
    HttpURLConnection httpConnection = (HttpURLConnection)url.openConnection();
    httpConnection.connect();
    responseCode = httpConnection.getResponseCode();
    if(responseCode == 200)
    {
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();;
        Document document = builder.parse(httpConnection.getInputStream());
        XPathFactory xPathfactory = XPathFactory.newInstance();
        XPath xpath = xPathfactory.newXPath();
        XPathExpression expr = xpath.compile("/GeocodeResponse/status");
        String status = (String)expr.evaluate(document, XPathConstants.STRING);
        if(status.equals("OK"))
        {
            expr = xpath.compile("//geometry/location/lat");
            String latitude = (String)expr.evaluate(document, XPathConstants.STRING);
            expr = xpath.compile("//geometry/location/lng");
            String longitude = (String)expr.evaluate(document, XPathConstants.STRING);
            return new double[] {Double.parseDouble(latitude), Double.parseDouble(longitude)};
        }
    }
    return new double[]{0,0};
}
现在,上面的iOS函数只运行setupMarker函数,其中Android方法返回坐标,没什么大不了的

我的问题是,我为这两个函数提供了完全相同的地址参数

iOS完美地返回所有坐标

然而,安卓系统只能正常返回大约30%的数据

是否有一个Android功能等同于上面的iOS功能,或者只有一个可以正确地进行地理编码

如您所见,Android在此处调用API:

http://maps.googleapis.com/maps/api/geocode/xml?address=
我已经对此进行了测试,结果并不好,至少不如iOS

我能怎么办

编辑-一些示例(都与iOS配合使用)

  • 都柏林伯灵顿路2号EBS 2
  • 爱尔兰都柏林4号巴尔斯布里奇梅里恩路AIB银行中心
  • 都柏林桑迪福德商业中心33单元AIB 18

尝试使用返回JSON格式数据的URL,然后可以解析并获取纬度和经度。以下是一个例子:

public static void getLatLongFromAddress(String youraddress) {
String uri = "http://maps.google.com/maps/api/geocode/json?address=" +
              youraddress + "&sensor=false";
HttpGet httpGet = new HttpGet(uri);
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) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

JSONObject jsonObject = new JSONObject();
try {
    jsonObject = new JSONObject(stringBuilder.toString());

    double lng = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
        .getJSONObject("geometry").getJSONObject("location")
        .getDouble("lng");

    double lat = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
        .getJSONObject("geometry").getJSONObject("location")
        .getDouble("lat");

    Log.d("latitude", "" + lat);
    Log.d("longitude", "" + lng);
} catch (JSONException e) {
    e.printStackTrace();
}

} 

希望这有帮助

你能举几个在Android上失败的地址的例子吗?这不起作用。如果您尝试使用示例OP,它仍然不会返回任何结果。这不是结果的格式问题,API根本找不到地址。它确实有效,API没有任何问题。这里是json格式的结果。在xml中。可能的问题是地址附加到URL的方式。这不是OP给出的完整地址。开头的“EBS”会将其丢弃好的,所以我认为这里的问题是URL。OP使用的是maps.googleapis.com/maps/api/geocode/,我正在尝试。我的JSON格式[链接]()。XML格式。我建议你把URL改成我提到的那个,看看是否有结果。我试过你的方法,结果是一样的。它必须是相同的数据,只是格式不同。