dongyimo1293 2017-08-17 10:54
浏览 51

在javascript中从纬度和经度获取邮政编码

want to show the postal code of a location from latitude and longitude of that location. below is my code.

function markerLocation() { 
  var currentLocation = marker.getPosition();
  var geocoder  = new google.maps.Geocoder();        
  var location  = new google.maps.LatLng(currentLocation.lat(), currentLocation.lng());

  geocoder.geocode({ 'latLng': location , function (results, status) {
    if (status == google.maps.GeocoderStatus.OK) { 
      var add = results[0].address_components[1][1];
      document.getElementById('lat').value = add;
    }
  });

Here I want to show the postal code in lat. but when i use address_components it shows undefined. kindly help.

  • 写回答

1条回答 默认 最新

  • dowbwrr3590709 2017-08-17 12:19
    关注

    Try this:

    <input id="postal_code"/>
    <input id="street"/>
    ...
    

    script

    if (status == google.maps.GeocoderStatus.OK) {
    for(var i in results[0].address_components) {
      // skip items with no types 
      if(typeof results[0].address_components[i].types == 'undefined') {
        continue;
      }
    
      if(results[0].address_components[i].types.indexOf('postal_code') > -1) {
        document.getElementById('postal_code').value = results[0].address_components[i].long_name;
      }
      if(results[0].address_components[i].types.indexOf('street') > -1) {
        document.getElementById('street').value = results[0].address_components[i].long_name;
      }
      ...
    }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)