简体   繁体   English

如何在Filter.js中使用Google Maps JavaScript API v3的地理编码服务?

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

I am using filter.js in a website along with Google Maps API V3 as it shows in its demo/example(of filter.js) with only one difference, my requirements are to use Address instead of Latitude or Longitude and as far I know it could be done using Geocoding Service of Google Maps. 我在网站上使用filter.js以及Google Maps API V3, 如其demo / example(filter.js)所示 ,只有一个区别,我的要求是使用地址而不是纬度或经度,据我所知可以使用Google Maps的地址解析服务来完成。 I have to modify some of my code: 我必须修改一些代码:

Before 之前

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);
    });
  },

After

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);
        }
    });

  },

It looks like its working but it is not showing markers on map at all. 看起来像在工作,但根本没有在地图上显示标记。 Trying to find my mistake but after many hours still could not fix it. 试图找到我的错误,但经过许多小时仍无法解决。 Can any one point out my coding mistake? 有人可以指出我的编码错误吗?

I have changed "this" to "that" in following code and it some how fixed my issue. 在下面的代码中,我已将“ this”更改为“ that”,并且它解决了我的问题。 Not sure about the logic yet. 尚不确定逻辑。

Before: 之前:

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

After: 后:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM