Javascript 更改脚本添加的标记的样式
我正在学习谷歌地图API,并在他们的网站上学习。我已经阅读了文档,花了几个小时试图弄明白这一点,但没有运气 该示例通过Javascript 更改脚本添加的标记的样式,javascript,google-maps-api-3,Javascript,Google Maps Api 3,我正在学习谷歌地图API,并在他们的网站上学习。我已经阅读了文档,花了几个小时试图弄明白这一点,但没有运气 该示例通过.addGeoJson添加标记,并使用一个函数将其样式化,该函数可根据地震震级将其更改为大小不同的圆圈。我试图在脚本中创建一个额外的标记,并使用相同的样式设置函数对其进行样式设置,但我不知道如何将该函数应用于新标记。我所做的: // My addition: newMarker = new google.maps.Marker({ position: {lat:20, lng
.addGeoJson
添加标记,并使用一个函数将其样式化,该函数可根据地震震级将其更改为大小不同的圆圈。我试图在脚本中创建一个额外的标记,并使用相同的样式设置函数对其进行样式设置,但我不知道如何将该函数应用于新标记。我所做的:
// My addition:
newMarker = new google.maps.Marker({
position: {lat:20, lng: -160},
map: map,
mag: 2
});
// The markers from .loadGeoJson are styled by calling this function but
// apparently it doesn't apply to the new marker
map.data.setStyle(styleFeature);
我猜newMarker
没有添加到默认的数据层对象中,其他标记可能位于该对象中?
您有两个选择
var映射;
函数initMap(){
map=new google.maps.map(document.getElementById('map'){
中心:{
拉丁美洲:20,
液化天然气:-160
},
缩放:2,
样式:mapStyle
});
map.data.add({
几何图形:新的google.maps.Data.Point({
拉丁美洲:20,
液化天然气:-160
}),
特性:{
杂志:2
}
});
map.data.setStyle(styleFeature);
//获取地震数据(JSONP格式)
//此提要是USGS提要的副本,您可以在此处找到原件:
// http://earthquake.usgs.gov/earthquakes/feed/v1.0/geojson.php
var script=document.createElement('script');
script.setAttribute(
“src”,
'https://storage.googleapis.com/mapsdevsite/json/quakes.geo.json');
document.getElementsByTagName('head')[0].appendChild(脚本);
}
//定义jsonp文件中引用的回调函数。
函数eqfeed_回调(数据){
map.data.addGeoJson(数据);
}
函数样式特征(特征){
var low=[151,83,34];//mag 1.0的颜色
var high=[5,69,54];//mag 6.0及以上版本的颜色
var minMag=1.0;
var maxMag=6.0;
//分数表示值位于最小值和最大值之间的位置
var分数=(Math.min(feature.getProperty('mag'),maxMag)-minMag)/
(maxMag-minMag);
var color=EHSL(低、高、分数);
返回{
图标:{
路径:google.maps.SymbolPath.CIRCLE,
冲程重量:0.5,
strokeColor:“#fff”,
fillColor:color,
fillOpacity:2/feature.getProperty('mag'),
//虽然指数在技术上是正确的,但二次曲线看起来更好
比例:Math.pow(feature.getProperty('mag'),2)
},
zIndex:Math.floor(feature.getProperty('mag'))
};
}
函数插值EHSL(低HSL、高HSL、分数){
var color=[];
对于(变量i=0;i<3;i++){
//根据分数计算颜色。
颜色[i]=(高hsl[i]-低hsl[i])*分数+低hsl[i];
}
返回'hsl('+color[0]+'、'+color[1]+'%、'+color[2]+'%)';
}
变量映射样式=[{
'featureType':'all',
'elementType':'all',
“stylers”:[{
“可见性”:“关闭”
}]
}, {
“功能类型”:“景观”,
'elementType':'geometry',
“stylers”:[{
“可见性”:“打开”
}, {
“颜色”:“fcfc”
}]
}, {
“功能类型”:“水”,
'elementType':'labels',
“stylers”:[{
“可见性”:“关闭”
}]
}, {
“功能类型”:“水”,
'elementType':'geometry',
“stylers”:[{
“可见性”:“打开”
}, {
“色调”:“5f94ff”
}, {
"轻盈":60
}]
}];代码>
html,
身体,
#地图{
身高:100%;
保证金:0;
填充:0;
}
function createMarker(latLng, mag) {
var low = [151, 83, 34]; // color of mag 1.0
var high = [5, 69, 54]; // color of mag 6.0 and above
var minMag = 1.0;
var maxMag = 6.0;
// fraction represents where the value sits between the min and max
var fraction = (Math.min(mag, maxMag) - minMag) /
(maxMag - minMag);
var color = interpolateHsl(low, high, fraction);
var marker = new google.maps.Marker({
position: latLng,
icon: {
path: google.maps.SymbolPath.CIRCLE,
strokeWeight: 0.5,
strokeColor: '#fff',
fillColor: color,
fillOpacity: 2 / mag,
// while an exponent would technically be correct, quadratic looks nicer
scale: mag
},
zIndex: Math.floor(mag),
map: map
});
return marker;
}
map.data.add({
geometry: new google.maps.Data.Point({
lat: 20,
lng: -160
}),
properties: {
mag: 2
}
});