#Blogger #html
HTML5 Geolocation
HTML5 Geolocation « PreviousNext Chapter » HTML Geolocation is used to locate a user's position. Try It Locate the User's Position The HTML Geolocation API is used to get the geographical position of a user. Since this can compromise user privacy, the position is not available unless the user approves it. Browser Support The numbers in the table specify the first browser version that fully supports Geolocation. API Geolocation 5.0 9.0 3.5 5.0 16.0 Note: Geolocation is much more accurate for devices with GPS, like iPhone. Using HTML Geolocation Use the getCurrentPosition() method to get the user's position. The example below is a simple Geolocation example returning the latitude and longitude of the user's position: Example Try it yourself » Example explained: Check if Geolocation is supported If supported, run the getCurrentPosition() method. If not, display a message to the user If the getCurrentPosition() method is successful, it returns a coordinates object to the function specified in the parameter ( showPosition ) The showPosition() function gets the displays the Latitude and Longitude The example above is a very basic Geolocation script, with no error handling. Handling Errors and Rejections The second parameter of the getCurrentPosition() method is used to handle errors. It specifies a function to run if it fails to get the user's location: Example function showError(error) { switch(error.code) { case error.PERMISSION_DENIED: x.innerHTML = "User denied the request for Geolocation." break; case error.POSITION_UNAVAILABLE: x.innerHTML = "Location information is unavailable." break; case error.TIMEOUT: x.innerHTML = "The request to get user location timed out." break; case error.UNKNOWN_ERROR: x.innerHTML = "An unknown error occurred." break; } } Try it yourself » Error Codes: Permission denied - The user did not allow Geolocation Position unavailable - It is not possible to get the current location Timeout - The operation timed out Displaying the Result in a Map To display the result in a map, you need access to a map service that can use latitude and longitude, like Google Maps: Example function showPosition(position) { var latlon = position.coords.latitude + "," + position.coords.longitude; var img_url = "http://maps.googleapis.com/maps/api/staticmap?center= "+latlon+"&zoom=14&size=400x300&sensor=false"; document.getElementById("mapholder").innerHTML = ""; } Try it yourself » In the example above we use the returned latitude and longitude data to show the location in a Google map (using a static image). Google Map Script How to use a script to show an interactive map with a marker, zoom and drag options. Location-specific Information This page demonstrated how to show a user's position on a map. However, Geolocation is also very useful for location-specific information. Examples: Up-to-date local information Showing Points-of-interest near the user Turn-by-turn navigation (GPS) The getCurrentPosition() Method - Return Data The getCurrentPosition() method returns an object if it is successful. The latitude, longitude and accuracy properties are always returned. The other properties below are returned if available. Property Description coords.latitude The latitude as a decimal number coords.longitude The longitude as a decimal number coords.accuracy The accuracy of position coords.altitude The altitude in meters above the mean sea level coords.altitudeAccuracy The altitude accuracy of position coords.heading The heading as degrees clockwise from North coords.speed The speed in meters per second timestamp The date/time of the response Geolocation object - Other interesting Methods watchPosition() - Returns the current position of the user and continues to return updated position as the user moves (like the GPS in a car). clearWatch() - Stops the watchPosition() method. The example below shows the watchPosition() method. You need an accurate GPS device to test this (like iPhone): Example Try it yourself »
Ярлыки: GeoIP, Geolocation, HTML5
Geo IP JavaScript
Simple JavaScript code that checks if the browser has the Geolocation API implemented and then uses it to get the current position of the device. this code creates a function which can be called on html using :
Ярлыки: GeoIP
api-maps.yandex.ru Script
(function(){ var project_data = {}; project_data["lang"]="en_US"; project_data["languageCode"]="en"; project_data["countryCode"]="US"; project_data["token"]="bc10bf9b08d244d57fb0a90bd7caedf9"; project_data["coordinatesOrder"]="latlong";project_data["geolocation"] = {"longitude":37.617671,"latitude":55.755768,"isHighAccuracy":false,"city":"Moscow","region":"Moscow and Moscow region","country":"Russia","zoom":10};project_data["hosts"]={api:{main:'http:\/\/api-maps.yandex.ru\/',ua:'http:\/\/legal.yandex.com\/maps_termsofuse\/?lang={{lang}}',maps:'http:\/\/maps.yandex.com\/',services:{coverage:'https:\/\/api-maps.yandex.ru\/services\/coverage\/',geoxml:'https:\/\/api-maps.yandex.ru\/services\/geoxml\/',route:'https:\/\/api-maps.yandex.ru\/services\/route\/',regions:'https:\/\/api-maps.yandex.ru\/services\/regions\/',psearch:'http:\/\/psearch-maps.yandex.ru\/',search:'https:\/\/api-maps.yandex.ru\/services\/search\/'}},layers:{map:'http:\/\/vec0%d.maps.yandex.net\/tiles?l=map&%c&%l',sat:'http:\/\/sat0%d.maps.yandex.net\/tiles?l=sat&%c&%l',skl:'http:\/\/vec0%d.maps.yandex.net\/tiles?l=skl&%c&%l',pmap:'http:\/\/0%d.pvec.maps.yandex.net\/?l=pmap&%c&%l',pskl:'http:\/\/0%d.pvec.maps.yandex.net\/?l=pskl&%c&%l'},traffic:'http:\/\/jgo.maps.yandex.net\/',trafficArchive:'http:\/\/jft.maps.yandex.net\/'};project_data.distribution={"enabled":false};project_data["layers"]={'map':{version:'4.32.2',scaled:true},'sat':{version:'3.225.0'},'skl':{version:'4.32.2',scaled:true}};var init=function(e,t){function r(e){this.browser=e,this.css=new i(this),this.graphics=new s}function i(t){function o(e){return typeof s[e]=="undefined"?s[e]=u(e):s[e]}function u(e){return a(e)||a(t.browser.cssPrefix+l(e))}function a(e){return typeof f().style[e]!="undefined"?e:null}function f(){return n||(n=e.createElement("div"))}function l(e){return e?e.substr(0,1).toUpperCase()+e.substr(1):e}function c(e){return r[e]&&o("transitionProperty")?h(r[e]):null}function h(e){var n=o(e);return n&&n!=e&&(n="-"+t.browser.cssPrefix.toLowerCase()+"-"+e),n}var n,r={transform:"transform",opacity:"opacity",transitionTimingFunction:"transition-timing-function",userSelect:"user-select",height:"height"},i={},s={};this.checkProperty=o,this.checkTransitionProperty=function(e){return typeof i[e]=="undefined"?i[e]=c(e):i[e]}}function s(){this.hasSVG=function(){return e.implementation&&e.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure","1.1")},this.hasCanvas=function(){var t=e.createElement("canvas");return!!("getContext"in t&&t.getContext("2d"))},this.hasVML=function(){var t=!1,n=e.createElement("div");n.innerHTML='
<script src='http://api-maps.yandex.ru/2.0-stable/?load=package.standard&lang=en' type='text/javascript'></script></html>