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

目录
相关文章
|
9月前
|
Web App开发
在 HTML 中禁用 Chrome 浏览器的 Google 翻译功能
在 html 标签中添加 translate=“no” 属性,浏览器将不会翻译整个页面。
403 0
|
3月前
|
移动开发 监控 定位技术
HTML5 Geolocation(地理定位)6
`getCurrentPosition()` 方法用于获取设备当前地理位置,成功时返回包含多个属性的对象,如纬度、经度、精度等。`watchPosition()` 持续监控位置变化,适合移动应用;`clearWatch()` 则停止位置监控。示例代码展示了如何使用 `watchPosition()` 获取并显示当前位置信息。
|
3月前
|
传感器 移动开发 定位技术
HTML5 Geolocation(地理定位)2
`getCurrentPosition()`方法的第二个参数用于处理获取用户位置时可能出现的错误。示例中的`showError`函数通过`error.code`区分不同类型的错误,并显示相应的提示信息。错误类型包括:用户拒绝、位置不可用、请求超时和未知错误。此外,还展示了如何使用返回的经纬度在谷歌地图中显示位置的示例。
|
3月前
|
移动开发 定位技术 iOS开发
HTML5 Geolocation(地理定位)3
本页介绍如何在地图上显示用户位置,并利用地理定位提供给定位置的相关信息,如更新本地信息、显示周边兴趣点等。通过 `getCurrentPosition()` 方法可获取用户当前位置的详细数据,包括经纬度、精度等。此外,`watchPosition()` 和 `clearWatch()` 方法分别用于持续跟踪用户位置变化和停止位置跟踪。示例代码展示了如何使用这些方法实现位置跟踪功能。
|
3月前
|
移动开发 定位技术 HTML5
HTML5 Geolocation(地理定位)5
本示例展示了如何利用获取的经纬度数据,在谷歌地图中以静态图片形式显示用户位置。此方法可扩展用于更新本地信息、展示周边兴趣点及实现车载导航系统等应用。
|
3月前
|
移动开发 定位技术 HTML5
HTML5 Geolocation(地理定位)4
`getCurrentPosition()` 方法的第二个参数用于处理获取用户位置时可能出现的错误。通过定义 `showError` 函数,可以根据不同的错误代码(如权限被拒、位置不可用、超时等)显示相应的错误信息。
|
3月前
|
Web App开发 移动开发 定位技术
HTML5 Geolocation(地理定位)1
HTML5 Geolocation API 用于获取用户的地理位置,需用户同意才可使用。支持的浏览器包括IE9+、Firefox、Chrome、Safari 和 Opera。此API在GPS设备上定位更精准。示例代码展示了如何使用getCurrentPosition()方法获取并显示用户的位置信息。
|
5月前
|
存储 移动开发 定位技术
HTML5 Geolocation(地理定位)优化到最高精度
HTML5 Geolocation API 可让网页访问用户的地理位置信息。为优化地理定位精度,需考虑设备、浏览器设置、网络状况及编码实现。使用 `enableHighAccuracy` 选项请求高精度,并确保设备开启 GPS,网络良好。结合多种数据源(如 GPS、Wi-Fi)可提高准确性。利用 `watchPosition` 定期更新位置,并妥善处理定位错误。务必遵循用户隐私原则,获取同意并遵守相关法规。这样可有效提升地理定位的精度与用户体验。
|
9月前
|
JSON 定位技术 API
谷歌地图接口Google Maps APIs中地图样式设计配置调整与JSON或URL导出
谷歌地图接口Google Maps APIs中地图样式设计配置调整与JSON或URL导出
176 1
|
JSON 小程序 定位技术
Google Maps APIs地图样式的设计与导出方法
本文介绍在谷歌地图API(Google Maps APIs)中,设计地图样式并将设计好的样式通过JSON或URL导出的方法~
322 1
Google Maps APIs地图样式的设计与导出方法