1Navigator.geolocation.getCurrentPosition (function(position) {2 varcoords=position.coords;3 Console.log (coords.latitude,coords.longitude,coords.accuracy);4 },5 //Catching error Messages6 function(Error) {7 varErrortype={81: ' Location service is denied ',92: "Cannot get location information",Ten3: "Get information timeout" One }; AAlert (errortypes[error.code]+ ": Unable to get your current location"); -})
1 <!DOCTYPE HTML>2 <HTML>3 4 <Head>5 <MetaCharSet= "UTF-8">6 <title></title>7 <Scripttype= "Text/javascript">8 functionShowobject (obj, k) {9 //recursive display of objectTen if(!obj) { return; } One for(varIinchobj) { A if(typeof(Obj[i]!= "Object" ||Obj[i]== NULL)) { - for(varJ= 0; J<K; J++) { - document.write (" "); the } - document.write (i+ ":" +Obj[i]+ "<br/>"); - } Else { - document.write (i+ ":<br/>"); + Showobject (Obj[i], K+ 1); - } + } A } at - functionget_location () { - if(navigator.geolocation) - navigator.geolocation.getCurrentPosition (Show_map, Handle_error, {enablehighaccuracy:true, Maximumage: + }); - Else - Alert ("not supported"); in } - to functionHandle_error (err) { + //Error Handling - Switch(err.code) { the Case 1: * Alert ("location service is denied"); $ Break;Panax Notoginseng Case 2: - Alert ("location service is denied"); the Break; + Case 3: A Alert ("location service is denied") the Break; + default: - Alert ("Unknown error") $ Break; $ } - } - the functionShow_map (position) { - //displaying geographic informationWuyi varLatitude=Position.coords.latitude; the varLongitude=Position.coords.longitude; - showobject (position,0); Wu } - get_location (); About </Script> $ </Head> - - <Body> - <DivID= "Map"style= "width:400px; height:400px;"></Div> A </Body> + the </HTML>
HTML5 authoritative guide 15. Obtaining Geo-location information