1

我正在尝试将 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 中显示内容,我希望它显示地址。

4

1 回答 1

0

从您现有的代码开始,显示标记地址的一种方法是将其存储在您的数据库中(或您要查询的任何内容以获取纬度和经度),然后将其传递给“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 转换为地址。

编辑:您可以从讨论地理编码策略的文档中查看这篇“文章”,地理编码器和反向地理编码器服务是相似的,并且相同的配额和速率限制适用于它们。

于 2012-07-09T03:06:38.823 回答