【发布时间】:2017-01-23 05:31:26
【问题描述】:
我想根据从我的数据库中获取的类型设置不同的颜色标记。例如类型是火,我希望标记为红色。可能吗?
这是我的 maps.html
(function(){
var map,marker,latlng,bounds,infowin;
/* initial locations for map */
var _lat=14.676;
var _lng=121.0437;
function showMap(){
/* set the default initial location */
latlng={ lat: _lat, lng: _lng };
bounds = new google.maps.LatLngBounds();
infowin = new google.maps.InfoWindow();
/* invoke the map */
map = new google.maps.Map( document.getElementById('map'), {
center:latlng,
zoom: 10
});
/* show the initial marker */
marker = new google.maps.Marker({
position:latlng,
map: map,
title: 'Hello World!'
});
bounds.extend( marker.position );
/* jQuery */
$.ajax({
url: 'get.php',
data: {'ajax':true },
dataType: 'json',
success: function( data, status ){
$.each( data, function( i,item ){
/* add a marker for each location in response data */
addMarker( item.lat, item.lng, item.username);
});
},
error: function(){
output.text('There was an error loading the data.');
}
});
}
/* simple function just to add a new marker */
function addMarker(lat,lng,title){
marker = new google.maps.Marker({/* Cast the returned data as floats using parseFloat() */
position:{ lat:parseFloat( lat ), lng:parseFloat( lng ) },
map:map,
title:title
});
google.maps.event.addListener( marker, 'click', function(event){
infowin.setContent(this.title);
infowin.open(map,this);
infowin.setPosition(this.position);
}.bind( marker ));
bounds.extend( marker.position );
map.fitBounds( bounds );
}
document.addEventListener( 'DOMContentLoaded', showMap, false );
}());
</script>
<style>
html, html body, #map{ height:100%; width:100%; padding:0; margin:0; }
</style>
</head>
<body>
<div id='map'></div>
</body>
这是我从数据库中获取数据的 get.php。
$mysql ="SELECT lat,lng,username,type FROM `tbl_coordinates`";
$result = mysqli_query($connect, $mysql);
if (!empty($result))
{
while ($row=mysqli_fetch_array($result))
{
$latlng[] = array(
'lat' => $row['lat'],
'lng' => $row['lng'],
'username' => $row['username'],
'type' => $row['type'],
);
}
}
mysqli_close($connect);
header('Content-type:application/json;charset=utf-8');
echo json_encode($latlng);
?>
【问题讨论】:
-
你在使用哪个版本的google api?
-
谷歌地图 api v3
标签: php google-maps google-maps-api-3