Javascript 根据变量更改google地图标记颜色
我正在使用谷歌地图API3和一些Laravel4.2 我需要根据来自数据库的变量为地图上的标记上色 例如,如果未来的var是Javascript 根据变量更改google地图标记颜色,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,我正在使用谷歌地图API3和一些Laravel4.2 我需要根据来自数据库的变量为地图上的标记上色 例如,如果未来的var是Rent,则标记应为红色,如果是Sell,则标记应为绿色 这就是我所尝试的 <script type="text/javascript"> var locations = [ @foreach($bannerMapBin as $pin) ['<div styl
Rent
,则标记应为红色,如果是Sell
,则标记应为绿色
这就是我所尝试的
<script type="text/javascript">
var locations = [
@foreach($bannerMapBin as $pin)
['<div style="width:300px;"><div style="float:left;margin-right:10px;height:92px;overflow:hidden;width:120px;"><a href="{{ URL::to('property-details/'.$pin->id) }}">@foreach($pin->propImages->slice(0, 1) as $image){{ HTML::image('images/propertyImages/'.$image->image, $pin->title, array('width'=>100, 'height'=>100)) }}@endforeach</a></div>{{ HTML::link('property-details/'.$pin->id, $pin->title) }} <br/> {{ $pin->propLocation->city }}</div>', {{ $pin->propLocation->lat }}, {{ $pin->propLocation->lng }}, 'type: Rent'],
@endforeach
];
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: new google.maps.LatLng(25.412392055138543, 51.188288833984416),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var marker, i;
var icons = {
Rent: {
icon: 'http://maps.google.com/mapfiles/ms/icons/green-dot.png'
},
Sell: {
icon: 'http://chart.googleapis.com/chart?chst=d_map_pin_letter&chld=%E2%80%A2%7CFF0000'
}
};
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
icon: icons[locations.type],
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});
google.maps.event.addListener(marker, 'click', (function (marker, i) {
return function () {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
</script>
变量位置=[
@foreach($bannerMapBin作为$pin)
[{{HTML::link('property-details/'.$pin->id,$pin->title)}}
{{{$pin->propLocation->city}},{{{$pin->propLocation->lat}},{{$pin->propLocation->lng},{type:Rent'],
@endforeach
];
var map=new google.maps.map(document.getElementById('map'){
缩放:10,
中心:新google.maps.LatLng(25.412392055138543,51.188288833984416),
mapTypeId:google.maps.mapTypeId.ROADMAP
});
var infowindow=new google.maps.infowindow();
var标记,i;
变量图标={
租金:{
图标:'http://maps.google.com/mapfiles/ms/icons/green-dot.png'
},
出售:{
图标:'http://chart.googleapis.com/chart?chst=d_map_pin_letter&chld=%E2%80%A2%7CFF0000'
}
};
对于(i=0;i
一切都很好,但不能给每个标记不同的颜色
我还在我的
位置的末尾尝试了这个'type:Rent'
,并将其作为var设置在那里,但没有达到预期效果。查看代码,位置的内部元素
不是对象——对象数组,而是多维数组。我可以从访问元素的方式来判断,例如,纬度和经度。假设数组中的最后一个元素是类型,则可以使用最后一个索引或数字3(数组长度为4)获得类型
var lastIndex = locations[i].length - 1; // total elements (zero-based) minus 1;
var iconType = icons[ locations[i][lastIndex] ].icon;
marker = new google.maps.Marker({
icon: iconType,
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});
或简化版本:
marker = new google.maps.Marker({
icon: icons[locations[i][4]].icon, // returns Rents or Sell
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});
理论上,如果从内部数组中删除最后一个字符串type:Rent
,上述代码应该可以工作 可以发布位置的样本
数组吗?内部元素对我来说不是对象。请提供一个演示该问题的示例。我尝试了这两个选项,但没有使用它,只是显示了默认的图标
,我将用更新的代码编辑我的问题。@YousefAltaf,我错过了图标
在对象中包含对象。我反思性地更新了我的代码。是的,这正如预期的那样有效,thx brother,很抱歉我的js知识很差,因为我不是java开发人员:)