ホームページ  >  記事  >  ウェブフロントエンド  >  html5 navigator.geolocation は、ブラウザに基づいて地理的位置コードのケースを取得します。

html5 navigator.geolocation は、ブラウザに基づいて地理的位置コードのケースを取得します。

黄舟
黄舟オリジナル
2017-04-01 11:15:103398ブラウズ

1. はじめに

html5 は、ブラウザに基づいて現在のユーザーの地理的位置を取得するために使用される geolocation 属性 を window.navigator に提供します。

Window.navigator.geolocation は 3 つのメソッドを提供します:

void getCurrentPosition(onSuccess,onError,options);
//获取用户当前位置

int watchCurrentPosition(onSuccess,onError,options);
//持续获取当前用户位置

void clearWatch(watchId);
//watchId 为watchCurrentPosition返回的值
//取消监控


options = {
     enableHighAccuracy,//boolean 是否要求高精度的地理信息
     timeout,//获取信息的超时限制
     maximumAge//对地理信息进行缓存的时间
}
//options可以不写,为默认即可

2. Position オブジェクト

地理的位置情報が正常に取得されると、onsuccess メソッドで位置オブジェクトが返され、これを通じて地理的位置を取得できます。関連情報:

position对象的属性:
 
latitude,//纬度

longitude,//经度

altitude,//海拔高度

accuracy,//获取纬度或者经度的精度

altitudeAccurancy,//海拔高度的精度

heading,//设备前景方向。正北方向的顺时针旋转角

speed,//设备的前进速度 m/s

timestamp,//获取地理位置信息时候的时间

3. Google map

に基づく例 コードを直接見てください:

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="UTF-8">
    <title>在页面上使用google地图示例</title>
</head>
<body onload = &#39;init()&#39;>
    <div id="map" style=&#39;width:800px;height:800px;&#39;></div>
    <script type="text/javascript" src=&#39;http://maps.google.com/maps/api/js?sensor=false&#39;></script>
    <script type="text/javascript">
        function init(){
            if(navigator.geolocation){
                navigator.geolocation.getCurrentPosition(function(pos){
                        var coords = pos.coords;
                        var latlng =new google.maps.LatLng(coords.latitude,coords.longitude);
                        var options = {zoom:14,center:latlng,mapTypeId : google.maps.MapTypeId.ROADMAP};
                        var map1;
                        map1 =new google.maps.Map(document.getElementById(&#39;map&#39;),options);
                        var marker =new google.maps.Marker({
                                position : latlng,
                                map : map1
                                });
                        var infowindow =new google.maps.InfoWindow({
                               content : &#39;当前位置!&#39;
                               });
                        infowindow.open(map1,marker);
                        });
            }
        }
    </script>
    
</body>
</html>

以上がhtml5 navigator.geolocation は、ブラウザに基づいて地理的位置コードのケースを取得します。の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。