html5指南 -- 7.geolocation结合google maps一例

简介:   demo地址:http://www.mycookingroom.com/geo.html     今天我们将把html5的geolocation结合google maps开发一个小的应用。google maps的api地址:https://developers.google.com/maps/documentation/javascript/?hl=zh-CN。

  demo地址:http://www.mycookingroom.com/geo.html

 

  今天我们将把html5的geolocation结合google maps开发一个小的应用。google maps的api地址:https://developers.google.com/maps/documentation/javascript/?hl=zh-CN

  调用google maps,实现需要添加js引用<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>,其中sensor参数的具体含义:

  要使用 Google Maps API,您需要指明自己的应用程序在任何 Maps API 库或服务请求中是否是使用传感器(如 GPS 定位器)来确定用户所处位置的。这对移动设备尤为重要。如果您的 Google Maps API 应用程序使用任何形式的传感器确定访问您的应用程序的设备的位置,那么您必须通过将 sensor 参数值设置为 true 以声明这一点。

  html部分比较简单,只需要准备一个div就可:

<body>
    <div id="map">
    </div>
</body>

  js代码的框架如下:

<script type="text/javascript">
    var map;
    var browserSupport = false;
    var attempts = 0;

    $(document).ready(function () {
       //初始化地图 
    InitMap(); //定位
getLocation();     //定位跟踪 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>

  InitMap方法就是调用google maps api初始化地图,他需要设置options对象,在调用地图初始化的时候使用。

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);
    }

  getLocation和watchLocation方法获取定位信息。

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();
        }
    }

  成功获取位置信息后,调用plotLocation方法把位置显示在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

adpics.aspx?source=kbh1983&sourcesuninfo
目录
相关文章
|
5月前
|
Web App开发
在 HTML 中禁用 Chrome 浏览器的 Google 翻译功能
在 html 标签中添加 translate=“no” 属性,浏览器将不会翻译整个页面。
278 0
|
4天前
|
存储 移动开发 定位技术
HTML5 Geolocation(地理定位)优化到最高精度
HTML5 Geolocation API 可让网页访问用户的地理位置信息。为优化地理定位精度,需考虑设备、浏览器设置、网络状况及编码实现。使用 `enableHighAccuracy` 选项请求高精度,并确保设备开启 GPS,网络良好。结合多种数据源(如 GPS、Wi-Fi)可提高准确性。利用 `watchPosition` 定期更新位置,并妥善处理定位错误。务必遵循用户隐私原则,获取同意并遵守相关法规。这样可有效提升地理定位的精度与用户体验。
|
5月前
|
JSON 定位技术 API
谷歌地图接口Google Maps APIs中地图样式设计配置调整与JSON或URL导出
谷歌地图接口Google Maps APIs中地图样式设计配置调整与JSON或URL导出
|
JSON 小程序 定位技术
Google Maps APIs地图样式的设计与导出方法
本文介绍在谷歌地图API(Google Maps APIs)中,设计地图样式并将设计好的样式通过JSON或URL导出的方法~
279 1
Google Maps APIs地图样式的设计与导出方法
|
JavaScript 前端开发 定位技术
最佳网络地图服务对比分析:Google Maps 与 OpenStreetMap
最佳网络地图服务对比分析:Google Maps 与 OpenStreetMap
524 0
最佳网络地图服务对比分析:Google Maps 与 OpenStreetMap
|
API
Google Guava之Maps&Lists&Sets
日常开发中,使用最多的就是集合了,所以避免不了对集合的各种操作,本篇文章来看一下,Guava中都有哪些常用的集合操作的API可以简化我们的代码。
218 0
Google Guava之Maps&Lists&Sets
|
Web App开发 移动开发 前端开发
HTML5实践 -- 如何使用css3完成google涂鸦动画
  今天我们将介绍,如何使用css3完成google涂鸦动画。当你点击demo页面的【开始】按钮之后,页面中的骑手和马匹将会运动起来,http://www.mycookingroom.com/demo/google-doodle-animation-in-css3-without-javascript.html。
1091 0
|
Web App开发 监控 HTML5
html5指南--4.使用Geolocation
  今天我们要学习的是使用Geolocation实现定位功能。我们可以通过navigator.geolocation获取Geolocation对象,他提供了下列方法: getCurrentPosition(callback,errorCallback,options):获取当前位置; watchPosition(callback,error,options):开始监控当前位置; clearWatch(id):停止监控当前位置。
1079 0
|
JavaScript 定位技术 API
HTML5的Geolocation API
Geolocation API用于将用户当前地理位置信息共享给信任的站点,这涉及用户的隐私安全问题,所以当一个站点需要获取用户的当前地理位置,浏览器会提示用户是“允许” or “拒绝”。 先看看哪些浏览器支持Geolocation API: IE9.0+、FF3.5+、Safari5.0+、Chrome5.0+、Opera10.6+、IPhone3.0+、Android2.0+ 也就是说除IE6~IE8外,其它最新的浏览器基本上都支持,包括最新的移动手机。
1015 0
下一篇
无影云桌面