-
Notifications
You must be signed in to change notification settings - Fork 0
GoogleMap
After the setup of MQTT and MQTTitude bindings to provide geo-fencing I decided some more color in the UI would be nice. The idea was to show a map that centers on my home and automatically scales to show the location of the inhabitants:
For people familiar with Google API and Ajax that is probably nothing fancy ... but for those (like me) that are new to that topic my setup might give some help and a head start.
For this example to work you'll need a proper MQTT and OwnTracks setup. Once you have the "raw" data available in OH you're ready ... ###Items...
/* Phone */
String mqttPositionPatrikRaw "Patrik Raw Data" { mqtt="<[home:owntracks/Lex/LexLuther:state:default]" }
String mqttPatrikLatitude "Patrik's Lat"
String mqttPatrikLongitude "Patrik's Lon"
String mqttPatrikAccuracy "Patrik's Accuracy"
String mqttHtcOneBattery "Patrik's HTC One Battery [%s%%]" <battery> (Phone, MQTT, Battery)For each person you'ld like to show on the map you'll need to have latitude and longitude data available. A rule can be used to parse the data received:
import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
rule "MqttPostionParsePatrik"
when
Item mqttPositionPatrikRaw changed
then
var String json = (mqttPositionPatrikRaw.state as StringType).toString
// {"_type": "location", "lat": "47.5010314", "lon": "8.3444293",
// "tst": "1422616466", "acc": "21.05", "batt": "40"}
var String type = transform("JSONPATH", "$._type", json)
if (type == "location") {
var String lat = transform("JSONPATH", "$.lat", json)
var String lon = transform("JSONPATH", "$.lon", json)
var String acc = transform("JSONPATH", "$.acc", json)
var String batt = transform("JSONPATH", "$.batt", json)
sendCommand(mqttPatrikLatitude, lat)
sendCommand(mqttPatrikLongitude, lon)
sendCommand(mqttPatikAccuracy, acc)
sendCommand(mqttHtcOneBattery, batt)
}
endThe following code will display a map based on your home location; and auto zoom to show all markers:
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
<!--
.Flexible-container {
position: relative;
padding-bottom: 56.25%;
padding-top: 30px;
height: 0;
overflow: hidden;
}
.Flexible-container iframe,
.Flexible-container object,
.Flexible-container embed {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
-->
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places,drawing,geometry"></script>
<script type="text/javascript">
////////////////////////////////////////////////////////////////////////
// Google Maps JavaScript API:
// https://developers.google.com/maps/documentation/javascript/?hl=de
// Marker Icons:
// https://sites.google.com/site/gmapsdevelopment/
////////////////////////////////////////////////////////////////////////
var map = null;
// LatLng's we want to show
var latlngHome = new google.maps.LatLng("47.501006", "8.344842");
var latlngPatrik = new google.maps.LatLng("47.501006", "8.344842"); // initialize to home ...
var latlngKarin = new google.maps.LatLng("47.501006", "8.344842"); // initialize to home ...
function startup() {
var map_canvas = document.getElementById('map_canvas');
var map_options = { center : latlngHome,
zoom : 14,
mapTypeId : google.maps.MapTypeId.ROADMAP };
map = new google.maps.Map(map_canvas, map_options);
var marker = new google.maps.Marker({
position : latlngHome,
map : map,
icon : 'http://maps.google.com/mapfiles/kml/pal2/icon10.png',
title : "Ehrendingen"
})
google.maps.event.addListener(map, 'tilesloaded', function(evt) { updateZoom(); });
} // end of function - startup
function updateZoom() {
// Array of google.maps.LatLng we want to be visible on screen ...
var latLngArray = [];
latLngArray.push(latlngHome);
latLngArray.push(latlngPatrik);
latLngArray.push(latlngKarin);
var viewPointBounds = new google.maps.LatLngBounds ();
for (var i = 0, length = latLngArray.length; i < length; i++) {
viewPointBounds.extend(latLngArray[i]);
}
map.fitBounds(viewPointBounds);
} // end of function - zoom
$(function() {
$.ajax({
url : "../rest/items/mqttPatrikLatitude/state",
data : { },
success : function( data ) {
var Latitude = data;
$.ajax({
url : "../rest/items/mqttPatrikLongitude/state",
data : { },
success : function( data ) {
var Longitude = data;
latlngPatrik = new google.maps.LatLng(Latitude, Longitude);
var marker = new google.maps.Marker({
position : latlngPatrik,
map : map,
icon : 'http://maps.google.com/mapfiles/ms/icons/green-dot.png',
title : "Patrik"
})
////////////////////////////////////////////////////////////////////////
}
})
}
}); // end of $.ajax - Patrik
$.ajax({
url : "../rest/items/mqttKarinLatitude/state",
data : { },
success : function( data ) {
var Latitude = data;
$.ajax({
url : "../rest/items/mqttKarinLongitude/state",
data : { },
success : function( data ) {
var Longitude = data;
latlngKarin = new google.maps.LatLng(Latitude, Longitude);
var marker = new google.maps.Marker({
position : latlngKarin,
map : map,
icon : 'http://maps.google.com/mapfiles/ms/icons/blue-dot.png',
title : "Karin"
})
////////////////////////////////////////////////////////////////////////
}
})
}
}); // end of $.ajax - Karin
}); // end of $(function)
</script>
</head>
<body onload="startup()">
<div id="map_canvas" class="Flexible-container" />
</body>
</html>Store that file on your OH system in "\webapps\static" (e.g. as Map.html). You can then use it in our site definitions; or directly in a browser.
###Linux / OS X
###Windows
- Cosm Persistence
- db4o Persistence
- Exec Persistence
- InfluxDB Persistence
- JDBC Persistence
- JPA Persistence
- Logging Persistence
- mapdb Persistence
- MongoDB Persistence
- MQTT Persistence
- my.openHAB Persistence
- MySQL Persistence
- rrd4j Persistence
- Sen.Se Persistence
- SiteWhere Persistence
- AlarmDecoder Binding
- Anel Binding
- Arduino SmartHome Souliss Binding
- Asterisk Binding
- Astro Binding
- Autelis Pool Control Binding
- BenQ Projector Binding
- Bluetooth Binding
- Bticino Binding
- CalDAV Binding
- Comfo Air Binding
- Config Admin Binding
- CUL Binding
- CUL Intertechno Binding
- CUPS Binding
- DAIKIN Binding
- Davis Binding
- Denon Binding
- digitalSTROM Binding
- DMX512 Binding
- DSC Alarm Binding
- DSMR Binding
- eBUS Binding
- Ecobee Binding
- EDS OWSever Binding
- eKey Binding
- Energenie Binding
- EnOcean Binding
- Enphase Energy Binding
- Epson Projector Binding
- Exec Binding
- Freebox Binding
- Freeswitch Binding
- Frontier Silicon Radio Binding
- Fritz AHA Binding
- Fritz!Box Binding
- FS20 Binding
- Global Cache IR Binding
- GPIO Binding
- HAI/Leviton OmniLink Binding
- HDAnywhere Binding
- Heatmiser Binding
- Homematic / Homegear Binding
- HTTP Binding
- IEC 62056-21 Binding
- IHC / ELKO Binding
- ImperiHome Binding
- Insteon Hub Binding
- Insteon PLM Binding
- IPX800 Binding
- IRtrans Binding
- jointSPACE-Binding
- KNX Binding
- Koubachi Binding
- LCN Binding
- LightwaveRF Binding
- Leviton/HAI Omnilink Binding
- Lg TV Binding
- Logitech Harmony Hub
- MailControl Binding
- MAX!Cube-Binding
- MAX! CUL Binding
- MiLight Binding
- MiOS Binding
- Modbus TCP Binding
- MPD Binding
- MQTT Binding
- MQTTitude binding
- Neohub Binding
- Nest Binding
- Netatmo Binding
- Network Health Binding
- Network UPS Tools Binding
- Nibe Heatpump Binding
- Nikobus Binding
- Novelan/Luxtronic Heatpump Binding
- NTP Binding
- One-Wire Binding
- Onkyo AV Receiver Binding
- Open Energy Monitor Binding
- OpenPaths presence detection binding
- OpenSprinkler Binding
- OSGi Configuration Admin Binding
- Panasonic TV Bindung
- panStamp Binding
- Philips Hue Binding
- Piface Binding
- pilight Binding
- Pioneer-AVR-Binding
- Plex Binding
- Plugwise Binding
- PLCBus Binding
- Primare Binding
- Pulseaudio Binding
- RFXCOM Binding
- RWE Smarthome Binding
- Sager WeatherCaster Binding
- Samsung AC Binding
- Samsung TV Binding
- Serial Binding
- Sallegra Binding
- Satel Alarm Binding
- Sinthesi Sapp Binding
- Snmp Binding
- Somfy URTSI II Binding
- Sonos Binding
- Squeezebox Binding
- Swegon ventilation Binding
- System Info Binding
- TA CMI Binding
- TCP/UDP Binding
- Tellstick Binding
- TinkerForge Binding
- Tivo Binding
- VDR Binding
- Velleman-K8055-Binding
- Wago Binding
- Wake-on-LAN Binding
- Waterkotte EcoTouch Heatpump Binding
- Weather Binding
- Wemo Binding
- Withings Binding
- XBMC Binding
- xPL Binding
- Yamahareceiver Binding
- Zibase Binding
- Z-Wave Binding
- Asterisk
- Google Calendar
- Linux Media Players
- ROS Robot Operating System
- Telldus Tellstick
- Zoneminder
- Wink Hub (rooted)
- Wink Monitoring
- Transformations
- XSLT
- JSON
- REST-API
- Security
- Service Discovery
- Voice Control
- BritishGasHive-Using-Ruby
- Dropbox Bundle
A good source of inspiration and tips from users gathered over the years. Be aware that things may have changed since they were written and some examples might not work correctly.
Please update the wiki if you do come across any out of date information.
- Comfo Air Binding
- Ecobee Examples
- Nest Examples
- Rollershutter Bindings
- Squeezebox
- WAC Binding
- WebSolarLog
- Alarm Clock
- Convert Farenheit to Celcius
- The mother of all lighting rules
- Reusable Rules via Functions
- Combining different Items
- Items, Rules and more Examples of a SmartHome
- Google Map
- Controlling openHAB with Android
- Usecase examples
- B-Control Manager
- Spell checking for foreign languages
- Flic via Tasker
- Chromecast via castnow

