如何从谷歌地图地理编码返回地址类型?

发布于 2024-12-28 07:54:17 字数 677 浏览 4 评论 0原文

我目前正在使用谷歌地图 API 对地图上的位置进行地理编码并返回街道的地址。

我目前使用以下代码返回地址:

        function codeLatLng(markerPos) {
            geocoder.geocode({'latLng': markerPos}, function(results, status) {
              if (status == google.maps.GeocoderStatus.OK) {
                if (results[1]) {
                //Set markerAddress variable
                var markerAddress = results[0].formatted_address;
alert(markerAddress);
...

但是,如果我不想返回格式化地址,而是使用地址组件类型返回更详细的版本,如何返回某些地址值,例如: http://code.google.com/apis/maps/documentation/geocoding/#Types

感谢帮助。

I am currently using the google maps api to geocode locations on the map and return the address of the street.

I currently return the address with the following code:

        function codeLatLng(markerPos) {
            geocoder.geocode({'latLng': markerPos}, function(results, status) {
              if (status == google.maps.GeocoderStatus.OK) {
                if (results[1]) {
                //Set markerAddress variable
                var markerAddress = results[0].formatted_address;
alert(markerAddress);
...

But what if I don't want to return the formatted address but a more detailed version using Address Component Types, how can I return certain address values like: http://code.google.com/apis/maps/documentation/geocoding/#Types

Help appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

或十年 2025-01-04 07:54:17

请问,为什么要检查 results[1] 然后使用 results[0] (或者这只是我应该忽略的拼写错误)?

只要statusOK,就会至少一个结果。
否则,status 将为 ZERO_RESULTS

无论如何,您可以使用这样的东西:

function codeLatLng(markerPos) {
  geocoder.geocode({'latLng': markerPos}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      var markerAddress = results[0].address_components[0].long_name
          + ' (type: ' + results[0].address_components[0].types[0] + ')';
      alert(markerAddress);

您可以使用整个 address_components 数组进行很多操作,玩得开心! :)

为了获得更多乐趣,请查看 Google Maps API v3 Geocoder Tool(源代码),网址为 http://gmaps-samples-v3.googlecode.com/svn/trunk/geocoder/v3-geocoder-tool.html

May I ask, why are you checking for results[1] and then using results[0] (or is it just a typo I should ignore)?

As long as status is OK, there will be at least one result.
Otherwise, status would be ZERO_RESULTS.

Anyway, you can use something like this:

function codeLatLng(markerPos) {
  geocoder.geocode({'latLng': markerPos}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      var markerAddress = results[0].address_components[0].long_name
          + ' (type: ' + results[0].address_components[0].types[0] + ')';
      alert(markerAddress);

You can play a lot with the whole address_components array, have fun! :)

For even more fun, have a look at the (source code of the) Google Maps API v3 Geocoder Tool at http://gmaps-samples-v3.googlecode.com/svn/trunk/geocoder/v3-geocoder-tool.html

太阳哥哥 2025-01-04 07:54:17

long_name 是地理编码器返回的地址组件的完整文本描述或名称。

 function codeLatLng(markerPos) {
                geocoder.geocode({'latLng': markerPos}, function(results, status) {
                  if (status == google.maps.GeocoderStatus.OK) {
                    if (results[1]) {
                    //Set markerAddress variable
                    var markerAddress = results[0].long_name;
    alert(markerAddress);
...

long_name is the full text description or name of the address component as returned by the Geocoder.

 function codeLatLng(markerPos) {
                geocoder.geocode({'latLng': markerPos}, function(results, status) {
                  if (status == google.maps.GeocoderStatus.OK) {
                    if (results[1]) {
                    //Set markerAddress variable
                    var markerAddress = results[0].long_name;
    alert(markerAddress);
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文