如何使用谷歌地图JavaScript API v3's Filter.js中的地理编码服务

How to use Google Maps JavaScript API v3's Geocoding Service in Filter.js?

本文关键字:js Filter 服务 编码 JavaScript 谷歌地图 何使用 API v3      更新时间:2023-09-26

我在网站中使用filter.js和Google Maps API V3,正如它在其演示/示例(filter.js)中所示,只有一个区别,我的要求是使用地址而不是纬度或经度,据我所知,这可以使用Google Maps的地理编码服务来完成。我必须修改我的一些代码:

之前

addMarker: function(salon){
    var that = this;
    var marker = new google.maps.Marker({
      position: new google.maps.LatLng(salon.lat, salon.lng),
      map: this.map,
      title: salon.title
    });
    marker.info_window_content = salon.title + '<br/> Treatments: ' + salon.treatments + '<br/> Booking: ' + salon.bookings_1 + '<br/> Address: ' + salon.address + '<br/><a href="' + salon.permalink + '"> View Full Details </a>'
    this.markers[salon.id] = marker
    google.maps.event.addListener(marker, 'click', function() {
      that.infowindow.setContent(marker.info_window_content)
      that.infowindow.open(that.map,marker);
    });
  },

之后

addMarker: function(salon){
    //new line for address 
    var salonaddress = salon.address1 + ' ' + salon.address2 + ' ' + salon.address3 + ', ' + salon.city + ', ' + salon.state + ' ' + salon.postal_code + ', ' + salon.country ;
    console.log(" salonaddress: " + salonaddress)
    var that = this;
    geocoder.geocode( { 'address': salonaddress}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          that.map.setCenter(results[0].geometry.location);
          var marker = new google.maps.Marker({
              map: this.map,
              title: salon.title,
              position: results[0].geometry.location
          });
          marker.info_window_content = salon.title + '<br/> Treatments: ' + salon.treatments + '<br/> Booking: ' + salon.bookings_1 + '<br/> Address: ' + salon.address + '<br/><a href="' + salon.   permalink + '"> View Full Details </a>'
            that.markers[salon.id] = marker
            google.maps.event.addListener(marker, 'click', function() {
              that.infowindow.setContent(marker.info_window_content)
              that.infowindow.open(that.map,marker);
            });
        } else {
          alert('Geocode was not successful for the following reason: ' + status);
        }
    });
  },

它看起来像是在工作,但它根本没有在地图上显示标记。试图找出我的错误,但几个小时后仍然无法修复。有人能指出我的编码错误吗?

我在下面的代码中将"this"改为"that",它在一定程度上解决了我的问题。还不确定逻辑。

之前:

var marker = new google.maps.Marker({
              map: this.map,
              title: salon.title,
              position: results[0].geometry.location
          });

之后:

 var marker = new google.maps.Marker({
              map: that.map,
              title: salon.title,
              position: results[0].geometry.location
          });