• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

java - Android 地理编码功能不如 iOS

[复制链接]
菜鸟教程小白 发表于 2022-12-13 09:49:40 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

所以我有一个将地址(字符串)转换为坐标的函数。

这就是它在 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 完美返回所有坐标。

然而,Android 只能正确返回大约 30%。

是否有与上面的 iOS 等效的 Android 功能,或者只有一个可以正确地理编码的功能。

您可以在此处看到 Android 调用 API:

http://maps.googleapis.com/maps/api/geocode/xml?address=

而且我已经对此进行了测试,它并没有给出好的结果,至少不如 iOS。

我该怎么办?

编辑 - 一些示例(均适用于 iOS)

  • EBS, 2 Burlington Road, Dublin 2
  • 爱尔兰都柏林 4 区鲍尔斯布里奇梅林路 AIB 银行中心
  • AIB, Unit 33, Sandyford Business Centre, Sandyford, Dublin 18



Best Answer-推荐答案


尝试使用返回 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();
}

} 

希望这会有所帮助。

关于java - Android 地理编码功能不如 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31944893/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap