202 Chapter 7—Geolocation
if (navigator.geolocation) {
$('status').innerHTML = '<span style="color:red">'
+'getting current position / item unsaved</span>';
navigator.geolocation.getCurrentPosition(
function(pos) {
d.pos = pos.coords;
localStorage.setItem(d.id, JSON.stringify(d));
$('status').innerHTML =
'<span style="color:green">item saved. Position'
+' is: '+pos.coords.latitude
+','+pos.coords.longitude+'</span>';
showItems();
},
posError,
{ enableHighAccuracy: true, maximumAge: 60000 }
);
} else {
// alert("Browser does not support Geolocation");
localStorage.setItem(d.id, JSON.stringify(d));
$('status').innerHTML =
"Browser does not support Geolocation/item saved.";
}
showItems();
}
If the text field is empty (d.msg = ''), a corresponding dialog appears and the
function is terminated with return. Otherwise, the timestamp is set to the cur-
rent millisecond, and the entry’s ID is assembled from the string geonotes_ and
the timestamp. If several applications should access the localStorage from one
server, the string prefix can help to distinguish the data. After the position has
been successfully determined, the variable pos within the diaryItem object is
filled with coordinates and the appropriate meta information, and then saved as
a JSON string in the localStorage via JSON.stringify().
If the browser does not support the Geolocation API, the application saves the
text anyway and points out that there is no support. The final call of showItems()
ensures that the list of messages is updated.
7.7 Browser Support
As mentioned previously, the functions for determining location offer many new
possibilities, especially for use on mobile devices. The most important mobile
platforms at the time of this writing are products by Apple (iPhone, iPad, iPod)
and Android cell phones. Both Google’s browser (standard on the Android plat-
form) and Safari (for Apple devices) support the Geolocation API.