Demo address: http://www.mycookingroom.com/geo.html
Today, we will combine HTML5 geolocation with Google Maps to develop a small application. The API address for Google Maps: https://developers.google.com/maps/documentation/javascript? Hl = ZH-CN.
Call Google Maps to add JS references to the implementation <SCRIPT type = "text/JavaScript" src = "http://maps.google.com/maps/api/js? Sensor = false "> </SCRIPT>. The specific meanings of the sensor parameter are as follows:
To use the Google Maps API, you must specify whether your application uses sensors (such as GPS locators) in any maps api library or service requests to determine the location of the user. This is especially important for mobile devices. If your Google Maps API application uses any form of sensor to determine the location of the device accessing your application, you mustsensorSet the parameter valuetrueTo declare this.
The HTML part is relatively simple. You only need to prepare a div:
<body> <div id="map"> </div></body>
The JS Code framework is as follows:
<SCRIPT type = "text/JavaScript"> var map; var browsersupport = false; var attempts = 0; $ (document). Ready (function () {// initialize the map
Initmap (); // locate getlocation (); // locate and track watchlocation () ;}); function initmap () {/* set all of the options for the map */var options ={};/* Create a new map for the application */map = new Google. maps. map ($ ('# map') [0], options);}/** if the W3C geolocation object is available then get the current * location, otherwise report the problem */function getlocation () {} function watchlocation () {}/* plot the location on the map and zoom to it */function plotlocation (position) {}/ * report any errors using this function */function reportproblem (e) {}</SCRIPT>
The initmap method is to call the Google Maps API to initialize a map. It needs to set the options object and use it when calling the map initialization.
function InitMap() { /* Set all of the options for the map */ var options = { zoom: 4, center: new google.maps.LatLng(38.6201, -90.2003), mapTypeId: google.maps.MapTypeId.ROADMAP, mapTypeControl: true, mapTypeControlOptions: { style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR, position: google.maps.ControlPosition.BOTTOM_CENTER }, panControl: true, panControlOptions: { position: google.maps.ControlPosition.TOP_RIGHT }, zoomControl: true, zoomControlOptions: { style: google.maps.ZoomControlStyle.LARGE, position: google.maps.ControlPosition.LEFT_CENTER }, scaleControl: true, scaleControlOptions: { position: google.maps.ControlPosition.BOTTOM_LEFT }, streetViewControl: true, streetViewControlOptions: { position: google.maps.ControlPosition.LEFT_TOP } }; /* Create a new Map for the application */ map = new google.maps.Map($('#map')[0], options); }
The getlocation and watchlocation methods obtain the location information.
function getLocation() { /* Check if the browser supports the W3C Geolocation API */ if (navigator.geolocation) { browserSupport = true; navigator.geolocation.getCurrentPosition(plotLocation, reportProblem, { timeout: 45000 }); } else { reportProblem(); } } function watchLocation() { /* Check if the browser supports the W3C Geolocation API */ if (navigator.geolocation) { browserSupport = true; navigator.geolocation.watchPosition(plotLocation, reportProblem, { timeout: 45000 }); } else { reportProblem(); } }
After obtaining the location information, call the plotlocation method to display the location on Google Maps.
function plotLocation(position) { attempts = 0; var point = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); var marker = new google.maps.Marker({ position: point }); marker.setMap(map); map.setCenter(point); map.setZoom(15); }
Demo: googlemapgeolocation.rar