Viết một ứng dụng thời tiết, Weather sử dụng panel (ok)
https://teamtreehouse.com/community/finished-build-a-mobile-web-app-using-jquery-mobile-ajax-any-ideas-on-how-to-add-custom-location-with-a-form
C:\xampp\htdocs\oec\index.html
<!doctype html>
<html>
<head>
<title>My Weather</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="apple-touch-icon" href="images/icon.png" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<link rel="stylesheet" href="//code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" />
<script src="//code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="//code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>
<link rel="stylesheet" type="text/css" href="css/stylesheet.css">
<script type="text/javascript">
var icons = {
"clear-day": "B ",
"clear-night": "C ",
"rain": "R ",
"snow": "G ",
"sleet": "X ",
"wind": "S ",
"fog": "N ",
"cloudy": "Y ",
"partly-cloudy-day": "H ",
"partly-cloudy-night": "I "
};
var cities = {
"new york": { coords: { latitude: 40.672060, longitude: -73.983898 } },
"los angeles": { coords: { latitude: 34.101422, longitude: -118.341224 } },
"chicago": { coords: { latitude: 41.879003, longitude: -87.63675 } },
"san francisco": { coords: { latitude: 37.788531, longitude: -122.407237 } },
"miami": { coords: { latitude: 25.790176, longitude: -80.140133 } },
"current location": ""
};
function loadWeather(cityCoords) {
console.log(cityCoords);
var latlng = cityCoords.coords.latitude + "," + cityCoords.coords.longitude;
// forecast URL
var forecastURL = "https://api.forecast.io/forecast/7d94e61948eb1e8dda90cff8207973d7/" + latlng;
$.ajax({
url: forecastURL,
jsonpCallback: 'jsonCallback',
contentType: "application/json",
dataType: 'jsonp',
success: function(json) {
console.log(json);
$("#current_temp").html(Math.round(json.currently.temperature) + "°F");
$("#current_summary").html(json.currently.summary);
$("#current_precipitation").html("Precipitation: " + (Math.round(json.currently.precipProbability) * 100) + "%");
$("#current_humidity").html("Humidity: " + ((json.currently.humidity) * 100) + "%");
$("#current_wind").html("Wind: " + (json.currently.windSpeed) + " mph");
$("#current_cloudcover").html("Cloud Cover: " + (Math.round(json.currently.cloudCover) * 100) + "%");
$("#current_temp").attr("data-icon", icons[json.currently.icon]);
},
error: function(e) {
console.log(e.message);
}
});
}
function loadCity(city) {
$("#location").html(city);
if (city.toLowerCase() == "current location") {
if (navigator.geolocation) navigator.geolocation.getCurrentPosition(loadWeather, loadDefaultCity);
else {
loadDefaultCity();
}
} else {
loadWeather(cities[city.toLowerCase()]);
}
}
function loadDefaultCity() {
loadCity("New York");
}
$(document).ready(function() {
loadCity("Chicago");
$("a.city").bind("click", function() {
loadCity($(this).html());
});
});
</script>
</head>
<body>
<div data-role="page">
<div data-role="panel" id="left-panel" data-theme="c">
<ul data-theme="d" data-role="listview">
<li data-icon="delete"><a href="#" data-rel="close">Close</a></li>
<li data-role="list-divider">Select a City</li>
<li><a href="#" class="city" data-rel="close">Current Location</a></li>
<li><a href="#" class="city" data-rel="close">Chicago</a></li>
<li><a href="#" class="city" data-rel="close">Los Angeles</a></li>
<li><a href="#" class="city" data-rel="close">Miami</a></li>
<li><a href="#" class="city" data-rel="close">New York</a></li>
<li><a href="#" class="city" data-rel="close">San Francisco</a></li>
</ul>
</div><!-- /panel -->
<div data-role="header" data-position="fixed" data-theme="c">
<h1>My Weather</h1>
<a href="#left-panel" data-icon="bars" data-role="button" data-iconshadow="false">Location</a>
</div>
<div data-role="content" class="content">
<h1 id="current_temp" class="icon" data-icon="B ">...°F</h1>
<p id="current_summary">Condition ...</p>
<p id="location">Location: ...</p>
<p id="current_precipitation">Precipitation: ...</p>
<p id="current_humidity">Humidity: ...</p>
<p id="current_wind">Wind: ...</p>
<p id="current_cloudcover">Cloud Cover: ...</p>
</div>
</div>
</body>
</html>
PreviousViết một ứng dụng Restaurant Picke sử dụng "back menu" (ok)NextjQuery Mobile Tutorial w3schools.com (ok)
Last updated