[英]How to use Google Maps JavaScript API v3's Geocoding Service in Filter.js?
我在网站上使用filter.js以及Google Maps API V3, 如其demo / example(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
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.