Vous êtes sur la page 1sur 4

1. Explain Java multithreading (alongwith examples, code etc).

class MultithreadingDemo implements Runnable{


public void run(){
System.out.println("My thread is in running state.");
try {
for(int i = 4; i > 0; i--) {
System.out.println("Thread:1" + i);
// Let the thread sleep for a while.
Thread.sleep(50);
}
} catch (InterruptedException e) {
System.out.println("Thread1 exception");
}
System.out.println("Thread 1 exiting");
}
public static void main(String args[]){
MultithreadingDemo obj=new MultithreadingDemo();
Thread tobj =new Thread(obj);
tobj.start();
}
}

2. Use HTML5 and javascript to get geolocation (user's position) and find city name.
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Reverse Geocoding</title>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var geocoder;

if (navigator.geolocation) {

navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
}
//Get the latitude and the longitude;
function successFunction(position) {
var lat = position.coords.latitude;
var lng = position.coords.longitude;
document.getElementById("locationHolder").innerHTML = "Your current location is: (" + lat + ", " +
lng + ")";
codeLatLng(lat, lng)
var imageURL = "http://maps.googleapis.com/maps/api/staticmap?center="+ lat + "," + lng +
"&zoom=14&size=400x300&sensor=false";
document.getElementById("mapHolder").innerHTML = "<img src='" + imageURL + "'>";
}
function errorFunction(){
document.getElementById("locationHolder").innerHTML ="Geocoder failed";
}
function initialize() {
geocoder = new google.maps.Geocoder();
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
}
else { message.innerHTML = "Geolocation is not supported by this browser."; }

function codeLatLng(lat, lng) {


//alert("lat"+lat+"lng"+lng)

var latlng = new google.maps.LatLng(lat, lng);


geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
console.log(results)
if (results[1]) {
//formatted address
// alert(results[0].formatted_address)
document.getElementById("cityHolder").innerHTML = "Your current city is: (" +
results[0].formatted_address + ")";
//find country name
for (var i=0; i<results[0].address_components.length; i++) {
for (var b=0;b<results[0].address_components[i].types.length;b++) {
//there are different types that might hold a city admin_area_lvl_1 usually does in come cases
looking for sublocality type will be more appropriate
if (results[0].address_components[i].types[b] == "administrative_area_level_1") {
//this is the object you are looking for
city= results[0].address_components[i];
break;
}
}
}

//city data
// alert(city.short_name + " " + city.long_name)
document.getElementById("countryHolder").innerHTML = "Your current state is: (" +
city.short_name + " " + city.long_name + ")";
} else {
// alert("No results found");
document.getElementById("locationHolder").innerHTML = "No results found";
}
} else {
// alert("Geocoder failed due to: " + status);
document.getElementById("locationHolder").innerHTML = "Geocoder failed due to: " + status ;
}
});
}
</script>
</head>
<body onload="initialize()">
<div id="cityHolder"></div>
<div id="countryHolder"></div>
<div id="locationHolder"></div>
<div id="mapHolder"></div>
</body>
</html>

Vous aimerez peut-être aussi