CartoDB-SQL-API/test/websocket_test/public/test.html
2023-05-19 00:42:48 +08:00

112 lines
3.5 KiB
HTML

<html>
<head>
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no">
<style>
html, body, #map {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
#map {
width: 100%;
height: 100%;
}
.button {
position: absolute;
background-color: #888;
font: bold 16px Helvetica;
padding: 5px 10px;
width: 10px;
color: white;
text-decoration: none;
border-radius: 3px;
display: inline-block;
text-align: center;
}
#plus {
top: 40px;
left: 40px;
}
#minus {
top: 40px;
left: 73px;
}
.button:hover {
background-color: #BBB;
}
</style>
</head>
<body>
<div id="map"></div>
<a id="plus" href="#" class="button">+</a>
<a id="minus" href="#" class="button">-</a>
</body>
<script src="gmaps_mercator.js"></script>
<script src="map.js"></script>
<script src="http://localhost:8080/socket.io/socket.io.js"></script>
<script>
map = new Map(document.getElementById('map'), {
center: new LatLng(41.69, -4.83),
zoom: 4
});
document.getElementById('plus').onclick = function() {
map.model.setZoom(map.model.zoom + 1);
return false;
}
document.getElementById('minus').onclick = function() {
map.model.setZoom(map.model.zoom - 1);
return false;
}
function render() {
//var tiles = self.model.visibleTiles(self.view.width, self.view.height);
//self.view.renderTiles(tiles, this.center_pixel);
}
var socket = io.connect('http://localhost:8080');
function get_sql() {
var bbox = map.model.getBBox(map.view.width, map.view.height);
var sql = "select st_asgeojson(the_geom,5) as the_geom_geojson from places WHERE the_geom && ST_SetSRID(ST_MakeBox2D(";
sql += "ST_Point(" + bbox[0].lng + "," + bbox[0].lat +"),";
sql += "ST_Point(" + bbox[1].lng + "," + bbox[1].lat +")), 4326)";
return sql ;
}
map.model.on('end_move', jajaja=function() {
var s = get_sql();
//console.log(s);
//setTimeout(function() {
socket.emit("sql_query", {sql: s, id:'LOVELY'});
//}, 1000);
var tiles = map.model.visibleTiles(map.view.width, map.view.height);
map.view.renderTiles(tiles, this.center_pixel);
});
function add_point(p) {
var p = new LatLng(p[1], p[0]);
var px = map.model.projection.fromLatLngToPixel(p, map.model.zoom);
var centerpx= map.model.getCenterPixel(map.view.width, map.view.height);
map.view.context.fillStyle = '#000';
map.view.context.fillRect(px.x - centerpx.x , px.y - centerpx.y, 2, 2);
}
socket.on('sql_result', function(data){
if (data.state == 1){
geo = JSON.parse(data.r['the_geom_geojson']);
add_point(geo.coordinates);
}
});
map.model.on('zoom_changed', jajaja);
map.model.emit('center_changed');
</script>
</html>