【问题标题】:converting lat long to Address in javascript在javascript中将lat long转换为地址
【发布时间】:2012-07-09 02:52:49
【问题描述】:

我正在尝试将 markerWindow 中的显示从 lat long 更改为地址。我无法理解有关如何执行此操作的 Google API。我愿意使用 Google API 或 geocoder gem,无论哪个效果更好。我只是不确定如何将它们中的任何一个包含到我的代码中。

    var map;
  function initialize() {

    //initializing map view
    var myOptions = {
      zoom: 13,
      center: new google.maps.LatLng(42.32657009662249, -71.06678009033203),
      disableDefaultUI: true,
      panControl: true,
      zoomControl: true,

      scaleControl: true,
      mapTypeId: google.maps.MapTypeId.ROADMAP, 
      panControl: true,
      panControlOptions: {
        position: google.maps.ControlPosition.TOP_RIGHT
      },
      zoomControl: true,
      zoomControlOptions: {
        style: google.maps.ZoomControlStyle.LARGE,
        position: google.maps.ControlPosition.TOP_RIGHT
      }
    };
    map = new google.maps.Map(document.getElementById('map_canvas'),
        myOptions);

    //BIKE STUFF
    var bikeLayer = new google.maps.BicyclingLayer();
    bikeLayer.setMap(map);

    <% @Reports.each do |x|%>
      addMarker(<%= x.latitude %>, <%=x.longitude%>);
    <% end %>
  }

  function addMarker(lat, long){
        var marker = new google.maps.Marker({
          position: new google.maps.LatLng(lat, long),
          map: map,
          title: 'REPORT'
        });
        //DROP PIN
        marker.setAnimation(google.maps.Animation.DROP);
        marker.setTitle('DROP');
        //DROP MARKER INFOWINDOW
        var latlng = new google.maps.LatLng(lat, long);
        google.maps.event.addListener(marker, 'click', function(){
          infowindow.open(map, this);
          marker = this
        });
        var infowindow = new google.maps.InfoWindow({
          content:"Lat: " + lat + ", Long: " + long
        });
        //CLOSING MARKER INFOWINDOW
        google.maps.event.addListener(infowindow,'closeclick',function(){
          infowindow.close(map, this); //removes the marker
        });
    }    
  console.log("message")
  google.maps.event.addDomListener(window, 'load', initialize);

我希望它显示地址,而不是在 InfoWindow 中显示内容。

【问题讨论】:

    标签: ruby google-maps geocode google-geocoding-api


    【解决方案1】:

    从您现有的代码开始,显示标记地址的一种方法是将其存储在您的数据库中(或您要查询的任何内容以获取纬度和经度),然后将其传递给“addMarker”调用(在将 addMarker 函数更改为接受包含地址的第三个参数之后。

    未测试:

    function addMarker(lat, long, address){
        var marker = new google.maps.Marker({
          position: new google.maps.LatLng(lat, long),
          map: map,
          title: 'REPORT'
        });
        //DROP PIN
        marker.setAnimation(google.maps.Animation.DROP);
        marker.setTitle('DROP');
        //DROP MARKER INFOWINDOW
        var latlng = new google.maps.LatLng(lat, long);
        google.maps.event.addListener(marker, 'click', function(){
          infowindow.open(map, this);
          marker = this
        });
        var infowindow = new google.maps.InfoWindow({
          content:address
        });
    

    如果您还没有存储地址,您可以使用 Google 的地理编码服务作为反向地理编码器将 lat/lng 转换为地址。

    编辑: 您可以从讨论地理编码策略的文档中查看"article",地理编码器和反向地理编码器服务相似,并且适用相同的配额和速率限制。

    【讨论】:

    • "您可以使用 Google 的地理编码服务作为反向地理编码器将 lat/lng 转换为地址。"是我不确定该怎么做的部分
    • 你有存储在数据库中的纬度/经度坐标吗?你那里也没有相关的地址吗?您要添加标记的点是从哪里来的?
    • 是的,我的坐标存储在数据库中。我的数据库中只有经纬度坐标。我想在我的地图上显示这些点,但在 infoWindow 中显示地址而不是坐标。但我不确定如何将坐标变成地址。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多