Javascript 如何在WP中用标记覆盖此google地图上的多段线?
我已经设置了谷歌地图,它可以从我的WP主题中的列表中创建标记,其中包含街道信息或lat,long信息。这在下面的代码中运行良好。但是,我也不知道如何将多段线路径添加到此地图。我对JS很在行,但据我所知。。。只要您使用“setMap”方法,就应该可以工作。以下是我到目前为止所拥有的,我应该如何/在何处将多段线添加到该页面,以便我可以将类似这样的内容添加到页面:?非常感谢任何能帮忙的人Javascript 如何在WP中用标记覆盖此google地图上的多段线?,javascript,wordpress,google-maps-api-3,markers,polyline,Javascript,Wordpress,Google Maps Api 3,Markers,Polyline,我已经设置了谷歌地图,它可以从我的WP主题中的列表中创建标记,其中包含街道信息或lat,long信息。这在下面的代码中运行良好。但是,我也不知道如何将多段线路径添加到此地图。我对JS很在行,但据我所知。。。只要您使用“setMap”方法,就应该可以工作。以下是我到目前为止所拥有的,我应该如何/在何处将多段线添加到该页面,以便我可以将类似这样的内容添加到页面:?非常感谢任何能帮忙的人 var estateMapping = (function () { var self = {},
var estateMapping = (function () {
var self = {},
marker_list = [],
open_info_window = null,
x_center_offset = 0, // x,y offset in px when map gets built with marker bounds
y_center_offset = -100,
x_info_offset = 0, // x,y offset in px when map pans to marker -- to accomodate infoBubble
y_info_offset = -100;
function build_marker(latlng, property) {
var marker = new MarkerWithLabel({
map: self.map,
draggable: false,
flat: true,
labelContent: property.bath,
labelAnchor: new google.maps.Point(22, 0),
labelClass: "label", // the CSS class for the label
labelStyle: {opacity: 1},
icon: 'http://bedstuybid.org/wp-content/uploads/2013/04/blank.png',
position: latlng
});
self.bounds.extend(latlng);
self.map.fitBounds(self.bounds);
self.map.setCenter(convert_offset(self.bounds.getCenter(), x_center_offset, y_center_offset));
var infoBubble = new InfoBubble({
maxWidth: 275,
content: contentString,
borderRadius: 3,
disableAutoPan: true
});
var residentialString = '';
if(property['commercial'] != 'commercial') {
var residentialString='<p class="details">'+property.bed+' br, '+property.bath+' ba';
}
var contentString =
'<div class="info-content">'+
'<a href="'+property.permalink+'"><img class="left" src="'+property.thumb+'" /></a>'+
'<div class="listing-details left">'+
'<h3><a href="'+property.permalink+'">'+property.street+'</a></h3>'+
'<p class="location">'+property.city+', '+property.state+' '+property.zip+'</p>'+
// '<p class="price"><strong>'+property.fullPrice+'</strong></p>'+residentialString+', '+property.size+'</p></div>'+
'</div>';
var tabContent =
'<div class="info-content">'+
'<img class="left" src="'+property.agentThumb+'" />'+
'<div class="listing-details left">'+
'<h3>'+property.agentName+'</h3>'+
'<p class="tagline">'+property.agentTagline+'</p>'+
'<p class="phone"><strong>Tel:</strong> '+property.agentPhone+'</p>'+
'<p class="email"><a href="mailto:'+property.agentEmail+'">'+property.agentEmail+'</a></p>'+
'</div>'+
'</div>';
infoBubble.addTab('Details', contentString);
infoBubble.addTab('Contact', tabContent);
google.maps.event.addListener(marker, 'click', function() {
if(open_info_window) open_info_window.close();
if (!infoBubble.isOpen()) {
infoBubble.open(self.map, marker);
self.map.panTo(convert_offset(this.position, x_info_offset, y_info_offset));
open_info_window = infoBubble;
}
});
function geocode_and_place_marker(property) {
var geocoder = new google.maps.Geocoder();
var address = property.street+', '+property.city+' '+property.state+', '+property.zip;
//If latlong exists build the marker, otherwise geocode then build the marker
if (property['latlong']) {
var lat = parseFloat(property['latlong'].split(',')[0]),
lng = parseFloat(property['latlong'].split(',')[1]);
var latlng = new google.maps.LatLng(lat,lng);
build_marker(latlng, property);
} else {
geocoder.geocode({ address : address }, function( results, status ) {
if(status == google.maps.GeocoderStatus.OK) {
var latlng = results[0].geometry.location;
build_marker(latlng, property);
}
});
}
}
function init_canvas_projection() {
function CanvasProjectionOverlay() {}
CanvasProjectionOverlay.prototype = new google.maps.OverlayView();
CanvasProjectionOverlay.prototype.constructor = CanvasProjectionOverlay;
CanvasProjectionOverlay.prototype.onAdd = function(){};
CanvasProjectionOverlay.prototype.draw = function(){};
CanvasProjectionOverlay.prototype.onRemove = function(){};
self.canvasProjectionOverlay = new CanvasProjectionOverlay();
self.canvasProjectionOverlay.setMap(self.map);
}
function convert_offset(latlng, x_offset, y_offset) {
var proj = self.canvasProjectionOverlay.getProjection();
var point = proj.fromLatLngToContainerPixel(latlng);
point.x = point.x + x_offset;
point.y = point.y + y_offset;
return proj.fromContainerPixelToLatLng(point);
}
self.init_property_map = function (properties, defaultmapcenter) {
var options = {
zoom: 1,
center: new google.maps.LatLng(defaultmapcenter.mapcenter),
mapTypeId: google.maps.MapTypeId.TERRAIN,
disableDefaultUI: true,
streetViewControl: false
};
self.map = new google.maps.Map( document.getElementById( 'map' ), options );
self.bounds = new google.maps.LatLngBounds();
init_canvas_projection();
//wait for idle to give time to grab the projection (for calculating offset)
var idle_listener = google.maps.event.addListener(self.map, 'idle', function() {
for (i=0;i<properties.length;i++) {
geocode_and_place_marker(properties[i]);
}
google.maps.event.removeListener(idle_listener);
});
}
return self;
}());
var estateMapping=(函数(){
var self={},
标记列表=[],
打开\u信息\u窗口=空,
当使用标记边界构建贴图时,x_center_offset=0,//x,y偏移量(以px为单位)
y_中心_偏移=-100,
x_info_offset=0,//x,y在映射平移到标记时的偏移量(以像素为单位)——以容纳infoBubble
y_信息_偏移量=-100;
功能构建标记(板条、属性){
变量标记=新标记WithLabel({
map:self.map,
可拖动:错误,
平:是的,
标签内容:property.bath,
labelAnchor:新的google.maps.Point(22,0),
labelClass:“label”//标签的CSS类
标签样式:{opacity:1},
图标:'http://bedstuybid.org/wp-content/uploads/2013/04/blank.png',
职位:latlng
});
自边界扩展(latlng);
self.map.fitbunds(self.bounds);
self.map.setCenter(convert_offset(self.bounds.getCenter(),x_center_offset,y_center_offset));
var infoBubble=新的infoBubble({
最大宽度:275,
content:contentString,
边界半径:3,
真的吗
});
var residentialString='';
如果(属性['commercial']!='commercial'){
var residentialString=''+property.bed+'br'+property.bath+'ba';
}
var contentString=
''+
''+
''+
''+
“
”+property.city+,“+property.state+”+property.zip+”
'+
//“”+property.fullPrice+”
“+residentialString+”,“+property.size+””+
'';
var tabContent=
''+
''+
''+
''+属性。代理名称+''的+
“”+property.agentagline+”
'+
“电话:”+property.agentPhone+“
”+
“”+
''+
'';
infoBubble.addTab('Details',contentString);
infoBubble.addTab('Contact',tabContent);
google.maps.event.addListener(标记'click',函数(){
如果(打开信息窗口)打开信息窗口。关闭();
如果(!infoBubble.isOpen()){
infoBubble.open(self.map,marker);
self.map.panTo(convert_offset(this.position,x_info_offset,y_info_offset));
打开信息窗口=信息气泡;
}
});
功能地理代码和位置标记(属性){
var geocoder=new google.maps.geocoder();
var address=property.street+','+property.city+'+property.state+','+property.zip;
//如果latlong存在,请构建标记,否则请编写地理代码,然后构建标记
if(属性['latlong']){
var lat=parseFloat(属性['latlong'].split(',')[0]),
lng=parseFloat(属性['latlong'].split(',')[1]);
var latlng=新的google.maps.latlng(lat,lng);
建筑标志(板条、不动产);
}否则{
geocoder.geocode({address:address},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
var latlng=results[0]。geometry.location;
建筑标志(板条、不动产);
}
});
}
}
函数init_canvas_projection(){
函数CanvasProjectionOverlay(){}
CanvasProjectionOverlay.prototype=新的google.maps.OverlayView();
CanvasProjectionOverlay.prototype.constructor=CanvasProjectionOverlay;
CanvasProjectionOverlay.prototype.onAdd=function(){};
CanvasProjectionOverlay.prototype.draw=函数(){};
CanvasProjectionOverlay.prototype.onRemove=function(){};
self.canvasProjectionOverlay=新的canvasProjectionOverlay();
self.canvasProjectionOverlay.setMap(self.map);
}
函数转换偏移(latlng、x\U偏移、y\U偏移){
var proj=self.canvasProjectionOverlay.getProjection();
var point=LatLngToContainerPixel(latlng)项目;
点x=点x+x_偏移;
点y=点y+y_偏移;
从ContainerPixeltolatlng返回项目(点);
}
self.init_property_map=函数(属性,defaultmapcenter){
变量选项={
缩放:1,
中心:新的google.maps.LatLng(defaultmapcenter.mapcenter),
mapTypeId:google.maps.mapTypeId.TERRAIN,
disableDefaultUI:true,
街景控制:错误
};
self.map=new google.maps.map(document.getElementById('map'),选项);
self.bounds=new google.maps.LatLngBounds();
初始化画布投影();
//等待空闲时间获取投影(用于计算偏移)
var idle_listener=google.maps.event.addListener(self.map,'idle',function()){
对于(i=0;像我这样的人,你必须先用这样的东西勾勒出你的路径,var myTrip=[stavanger,amsterdam,london];
。在你的情况下,这可能是某种路线,尽管我不确定你的路线是什么