如何在 Java 代码中使用 Google Maps API 获取指定位置的坐标?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12852993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:28:59  来源:igfitidea点击:

How do I use Google Maps API in a Java code to fetch coordinates of specified location?

javafunctiongoogle-maps

提问by SNag

In a BI project I'm currently working on, we are in need of geo-coordinates for a list of locations. With the address location (such as "New York, US") as input, the output should be the coordinates as a latitude-longitude pair (like {40.71435, -74.00597}). The behaviour is similar to what is seen on thispage.

在我目前正在进行的 BI 项目中,我们需要位置列表的地理坐标。以地址位置(例如"New York, US")作为输入,输出应该是作为纬度-经度对(例如{40.71435, -74.00597})的坐标。该行为类似于在页面上看到的内容。

A similar question earlier on SOpoints to using the Google Maps API in JavaScript to achieve this, but I'm looking for a Java solution -- some function of the form getCoordinates(location), because this is a small requirement in a larger Java program already in existence.

之前在 SO 上的一个类似问题指出在 JavaScript 中使用 Google Maps API 来实现这一点,但我正在寻找一种 Java 解决方案——某种形式的功能getCoordinates(location),因为这是已经存在的较大 Java 程序中的一个小要求.

Any pointers on how I may use the Google Maps API (or any other maps API) in Java to achieve this would be of great help!

关于我如何使用 Java 中的 Google Maps API(或任何其他地图 API)来实现这一点的任何指示都会有很大帮助!

回答by guido.rota

You can use the Google Geocoding HTTP API (see here). To connect to it and get the responses you can use a Java URLConnection (tutorial is here) and parse the response using your favourite Json library (I personally use Hymanson)

您可以使用 Google Geocoding HTTP API(请参阅此处)。要连接到它并获得响应,您可以使用 Java URLConnection(教程在这里)并使用您最喜欢的 Json 库解析响应(我个人使用 Hymanson)

回答by Jai Govindani

So you'd like a way to perform Google Maps API Geocoding via Java - here's one that might work for you. The response might not be in the exact same format you need but should be pretty workable:

因此,您想要一种通过 Java 执行 Google Maps API 地理编码的方法 - 这里有一个可能适合您的方法。响应可能与您需要的格式不同,但应该非常可行:

http://code.google.com/p/geocoder-java/

http://code.google.com/p/geocoder-java/

You can see the final format returned in LatLng.java - just trace the code through starting from GeocodeResponse.java and you'll see the final format - the classes are pretty simple.

您可以看到在 LatLng.java 中返回的最终格式——只需从 GeocodeResponse.java 开始跟踪代码,您就会看到最终格式——这些类非常简单。