Java 获取纬度&;从给定地址名开始的经度。不是地理编码器

Java 获取纬度&;从给定地址名开始的经度。不是地理编码器,java,android,google-maps,Java,Android,Google Maps,我有一个地址名,我想得到一个准确的纬度和经度。我知道我们可以使用Geocoder的getFromLocationName(address,maxresult)来获取此信息 问题是,我得到的结果总是空的——与我们得到的结果不同。这总是让我得到一些结果,不像地理编码器 我还尝试了另一种方法:“http://maps.google.com/maps/api/geocode/json?address=“+address+”&sensor=false”(这里是!)它比geocoder提供更好的结果,但经常

我有一个地址名,我想得到一个准确的纬度和经度。我知道我们可以使用Geocoder的getFromLocationName(address,maxresult)来获取此信息

问题是,我得到的结果总是空的——与我们得到的结果不同。这总是让我得到一些结果,不像地理编码器

我还尝试了另一种方法:
“http://maps.google.com/maps/api/geocode/json?address=“+address+”&sensor=false”
(这里是!)它比geocoder提供更好的结果,但经常返回错误
(java.net.SocketException:recvfrom failed:EconReset(由对等方重置连接)
。这很无聊

我的问题是:我们如何从java代码中搜索得到相同的latlong结果

附加:关于使用“+address+”和sensor=false”的api文档在哪里?是提供此功能的web服务列表

我最喜欢的是

你为什么不试试打

返回以下输出。(确保将lat和lon放在URL中)


Albert,我想您担心的是您的代码不起作用。下面的代码对我来说非常有效。我认为您缺少将字符串转换为URI的
URIUtil.encodeQuery

我正在使用gson库,下载它并将其添加到您的路径

要获取用于gson解析的类,您需要转到。只需在浏览器上启动,获取结果并将其粘贴到此网站。它将为您生成pojo。您可能还需要添加annotation.jar

相信我,开始工作很容易。不要灰心丧气

try {
        URL url = new URL(
                "http://maps.googleapis.com/maps/api/geocode/json?address="
                        + URIUtil.encodeQuery("Sayaji Hotel, Near balewadi stadium, pune") + "&sensor=true");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Accept", "application/json");

        if (conn.getResponseCode() != 200) {
            throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
        }
        BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

        String output = "", full = "";
        while ((output = br.readLine()) != null) {
            System.out.println(output);
            full += output;
        }

        PincodeVerify gson = new Gson().fromJson(full, PincodeVerify.class); 
        response = new IsPincodeSupportedResponse(new PincodeVerifyConcrete(
                gson.getResults().get(0).getFormatted_address(), 
                gson.getResults().get(0).getGeometry().getLocation().getLat(),
                gson.getResults().get(0).getGeometry().getLocation().getLng())) ;
        try {
            String address = response.getAddress();
            Double latitude = response.getLatitude(), longitude = response.getLongitude();
            if (address == null || address.length() <= 0) {
                log.error("Address is null");
            }
        } catch (NullPointerException e) {
            log.error("Address, latitude on longitude is null");
        }
        conn.disconnect();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
编辑

“关于”的回答没有包含足够的细节`

从所有的研究中,你都希望谷歌地图能够参考每一个地方、地区、城市的组合。但事实是谷歌地图在其自身的上下文中包含地理和反向地理。你不能期望它有像
Sayaji酒店这样的组合,靠近浦那balewadi体育场。
。网络谷歌地图将为你找到它u因为它使用了更广泛的
搜索
丰富的谷歌后端。谷歌api的唯一反向地理地址来自他们自己的api。在我看来,这似乎是一种合理的工作方式,考虑到我们的印度地址系统有多复杂,第二十字路口可以离第一十字路口几英里:)

我使用,顺序是:
-谷歌地理编码API v3(在少数情况下会出现问题)
-地理编码器(在某些情况下,存在描述的问题)

如果我没有收到来自谷歌地理编码API的结果,我将使用地理编码器。

希望这能帮助您:

public static GeoPoint getGeoPointFromAddress(String locationAddress) {
        GeoPoint locationPoint = null;
        String locationAddres = locationAddress.replaceAll(" ", "%20");
        String str = "http://maps.googleapis.com/maps/api/geocode/json?address="
                + locationAddres + "&sensor=true";

        String ss = readWebService(str);
        JSONObject json;
        try {

            String lat, lon;
            json = new JSONObject(ss);
            JSONObject geoMetryObject = new JSONObject();
            JSONObject locations = new JSONObject();
            JSONArray jarr = json.getJSONArray("results");
            int i;
            for (i = 0; i < jarr.length(); i++) {
                json = jarr.getJSONObject(i);
                geoMetryObject = json.getJSONObject("geometry");
                locations = geoMetryObject.getJSONObject("location");
                lat = locations.getString("lat");
                lon = locations.getString("lng");

                locationPoint = Utils.getGeoPoint(Double.parseDouble(lat),
                        Double.parseDouble(lon));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return locationPoint;
    }
公共静态地质点getGeoPointFromAddress(字符串位置地址){
地理点位置点=空;
字符串locationAddres=locationAddress.replaceAll(“,“%20”);
字符串str=”http://maps.googleapis.com/maps/api/geocode/json?address="
+locationAddres+“&sensor=true”;
字符串ss=readWebService(str);
JSONObject json;
试一试{
弦板条,长;
json=新的JSONObject(ss);
JSONObject geoMetryObject=新JSONObject();
JSONObject locations=新的JSONObject();
JSONArray jarr=json.getJSONArray(“结果”);
int i;
对于(i=0;i
据我所知,Google Maps正在使用Google Places API进行自动完成,然后获取可以获取坐标的位置的详细信息


这个方法应该会给你预期的结果。

很好的搜索位置,但它不是我要找的伪造代码不是完美的科学。记住这一点。例如,我想要“Wadgaon sheri”,它向我展示了靠近EON it公园的Kharadi大道。所以我在地址上加了“邮局”。我用pincode代替了“wadgaon sheri”。长话短说,别指望地理编码能为你带来奇迹。很可能你使用的任何API最终都会得到相同的结果,因为underneat他们也会使用地理编码。siddharth做得很好,但是如果你检查它的位置,不要给我确切的位置,正如我们可以从上面的代码中说的,我们得到的latLag为18.5204303 73.85674370,如果我们将此作为输入给地理编码程序,它将指向普纳附近的某个地方,这是不正确的。该网站还使用地理编码器。我在代码中使用了latLag,但它给出了同样错误的结果。好吧,我认为我们可以安全地得出结论,谷歌地图并没有引用每个地方、地区、城市的组合。事实上,你能得到一个合适的地址的唯一方法是,如果你从谷歌自己的地址。因此,在您的情况下,您需要从谷歌获取自动完成地址,保存它,使用它并在其上反转地理位置。你不能只取一个实际的地址并转换它(就像你可以为我们的地址做的那样。考虑到我们的印度地址系统有多么复杂,在我看来这是一种合理的工作方式,第二十字路口可以离第一十字路口几英里:)我同意。自动更正对我们的印度地址不起作用,我也尝试过,但有时如果客户要求,找到这样的脚本会变得非常困难(让我们看看是否有任何解决方案,这对我有好处。技术上的局限性是无法用固执来克服的,你需要重新审视。如果你真的这么做了,我会的
String locationAddres = anyLocationSpec.replaceAll(" ", "%20");
URL url = new URL("https://maps.googleapis.com/maps/api/geocode/json?sensor=false&address="+locationAddres+"&language=en&key="YourKey");
try(InputStream is = url.openStream(); JsonReader rdr = Json.createReader(is)) {
    JsonObject obj = rdr.readObject();
    JsonArray results = obj.getJsonArray("results");
    JsonObject geoMetryObject, locations;

    for (JsonObject result : results.getValuesAs(JsonObject.class)) {
        geoMetryObject=result.getJsonObject("geometry");
        locations=geoMetryObject.getJsonObject("location");
        log.info("Using Gerocode call - lat : lng value for "+ anyLocationSpec +" is - "+locations.get("lat")+" : "+locations.get("lng"));
        latLng = locations.get("lat").toString() + "," +locations.get("lng").toString();
    }
}
String locationAddres = anyLocationSpec.replaceAll(" ", "%20");
URL url = new URL("https://maps.googleapis.com/maps/api/geocode/json?sensor=false&address="+locationAddres+"&language=en&key="YourKey");
try(InputStream is = url.openStream(); JsonReader rdr = Json.createReader(is)) {
    JsonObject obj = rdr.readObject();
    JsonArray results = obj.getJsonArray("results");
    JsonObject geoMetryObject, locations;

    for (JsonObject result : results.getValuesAs(JsonObject.class)) {
        geoMetryObject=result.getJsonObject("geometry");
        locations=geoMetryObject.getJsonObject("location");
        log.info("Using Gerocode call - lat : lng value for "+ anyLocationSpec +" is - "+locations.get("lat")+" : "+locations.get("lng"));
        latLng = locations.get("lat").toString() + "," +locations.get("lng").toString();
    }
}