Our goal is to display website visitors on the map, similar to the screenshots below.
We will convert their IP address to the LAT/LNG coordinates and display the marker on the OpenStreetMap map. To convert the IP address to the LAT/LNG pair, we will use geololization data from IP2Location.com.
We will display active users in the last ten minutes. If the user has several activities in the last 60 seconds, their point will throb.
There are also YouTube videos that provide more details from this project.
1. Database structure.
We will need two tables, ‘users’ and ‘IP2Location’. The following is a script for the MySQL database.
Please note that this SQL script only makes ‘IP2Location’ but does not come with data. The data collection itself is around 300MB and you can download it for free
2. Enter the code snippet into the page where you want to display the map. In our case it will be the menu page. The code itself is very simple, only issuing a div with a ‘map’ id.
PHP Code:
3. After AppplayInitialized Event
PHP Code:
4. Custom_function.js
The following Javascript code goes to the Event Editor -> Custom_Function.js section.
$(document).ready(function() { $("#map").width($(".r-fluid").width()); var height = $(window).height() - $("#map").offset().top - 30; $("#map").height(height); window.mapObj = new OpenLayers.Map("map", { controls: [ new OpenLayers.Control.PanZoomBar(), new OpenLayers.Control.Navigation() ], }); var layer = new OpenLayers.Layer.OSM(); mapObj.addLayer(layer); window.markersList = new OpenLayers.Layer.Markers("Markers"); mapObj.addLayer(markersList); mapObj.zoomToMaxExtent(); updateMarkers(); setInterval(updateMarkers, 5000); function updateMarkers() { $.post("", { getActiveUsers: true }, function(response) { var coordsArr = JSON.parse(response), activeIds = coordsArr.map(function(coords) { return coords.id; }), allIds = markersList.markers.map(function(marker) { return marker.id; }); $.each(coordsArr, function(i, latLon) { if (!allIds.includes(latLon.id)) { addMarker(latLon.id, latLon.lat, latLon.lng, latLon.active); } else { var curMarker = markersList.markers.find(function(marker) { return marker.id == latLon.id }); if (curMarker.active != latLon.active) { $(curMarker.icon.imageDiv).toggleClass("active", latLon.active); } } }); allIds = markersList.markers.map(function(marker) { return marker.id; }); for (var i = 0; i < allIds.length; i++) { if (allIds[i] != undefined && !activeIds.includes(allIds[i])) { var markerToRemove = markersList.markers.find(function(marker) { return marker.id == allIds[i] }); markersList.removeMarker(markerToRemove); } } function addMarker(id, lat, lng, active) { var lonLat = new OpenLayers.LonLat(lng, lat) .transform( new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984 mapObj.getProjectionObject() // to Spherical Mercator Projection ); var icon = new OpenLayers.Icon("", new OpenLayers.Size(15, 15)); var marker = new OpenLayers.Marker(lonLat, icon); if (active) { $(marker.icon.imageDiv).addClass("active"); } marker.id = id; marker.active = active; markersList.addMarker(marker); return marker; } }); } updateMarkers(); });
5. CSS Code (Style Editor -> Modification of CSS)
We use this CSS code to adjust and confirm the default display of the OSM map.
Game Center
Game News
Review Film
Rumus Matematika
Anime Batch
Berita Terkini
Berita Terkini
Berita Terkini
Berita Terkini
review anime