{"id":256770,"date":"2022-11-22T08:51:23","date_gmt":"2022-11-22T00:51:23","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=256770"},"modified":"2022-11-03T20:51:58","modified_gmt":"2022-11-03T12:51:58","slug":"to-html5-geolocation","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/to-html5-geolocation.html","title":{"rendered":"HTML5 Geolocation\uff08\u5730\u7406\u5b9a\u4f4d\uff09\u7b80\u4ecb"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nHTML5 Geolocation\uff08\u5730\u7406\u5b9a\u4f4d\uff09\u7528\u4e8e\u5b9a\u4f4d\u7528\u6237\u7684\u4f4d\u7f6e\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u5b9a\u4f4d\u7528\u6237\u7684\u4f4d\u7f6e<\/strong><\/div>\n

HTML5 Geolocation API \u7528\u4e8e\u83b7\u5f97\u7528\u6237\u7684\u5730\u7406\u4f4d\u7f6e\u3002<\/p>\n

\u9274\u4e8e\u8be5\u7279\u6027\u53ef\u80fd\u4fb5\u72af\u7528\u6237\u7684\u9690\u79c1\uff0c\u9664\u975e\u7528\u6237\u540c\u610f\uff0c\u5426\u5219\u7528\u6237\u4f4d\u7f6e\u4fe1\u606f\u662f\u4e0d\u53ef\u7528\u7684\u3002<\/p>\n

\u6d4f\u89c8\u5668\u652f\u6301<\/strong><\/div>\n

\"\"
\nInternet Explorer 9+, Firefox, Chrome, Safari \u548c Opera \u652f\u6301Geolocation\uff08\u5730\u7406\u5b9a\u4f4d\uff09.<\/p>\n

\u6ce8\u610f: Geolocation\uff08\u5730\u7406\u5b9a\u4f4d\uff09\u5bf9\u4e8e\u62e5\u6709 GPS \u7684\u8bbe\u5907\uff0c\u6bd4\u5982 iPhone\uff0c\u5730\u7406\u5b9a\u4f4d\u66f4\u52a0\u7cbe\u786e\u3002<\/p>\n

HTML5 - \u4f7f\u7528\u5730\u7406\u5b9a\u4f4d<\/strong><\/div>\n

\u8bf7\u4f7f\u7528 getCurrentPosition() \u65b9\u6cd5\u6765\u83b7\u5f97\u7528\u6237\u7684\u4f4d\u7f6e\u3002<\/p>\n

\u4e0b\u4f8b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5730\u7406\u5b9a\u4f4d\u5b9e\u4f8b\uff0c\u53ef\u8fd4\u56de\u7528\u6237\u4f4d\u7f6e\u7684\u7ecf\u5ea6\u548c\u7eac\u5ea6:
\n\u5b9e\u4f8b<\/p>\n

var x=document.getElementById(\"demo\");\r\nfunction getLocation()\r\n{\r\n    if (navigator.geolocation)\r\n    {\r\n        navigator.geolocation.getCurrentPosition(showPosition);\r\n    }\r\n    else\r\n    {\r\n        x.innerHTML=\"\u8be5\u6d4f\u89c8\u5668\u4e0d\u652f\u6301\u83b7\u53d6\u5730\u7406\u4f4d\u7f6e\u3002\";\r\n    }\r\n}\r\n \r\nfunction showPosition(position)\r\n{\r\n    x.innerHTML=\"\u7eac\u5ea6: \" + position.coords.latitude + \r\n    \"
\u7ecf\u5ea6: \" + position.coords.longitude; \r\n}<\/pre>\n

\u5b9e\u4f8b\u89e3\u6790:<\/p>\n

    \n
  1. \u68c0\u6d4b\u662f\u5426\u652f\u6301\u5730\u7406\u5b9a\u4f4d<\/li>\n
  2. \u5982\u679c\u652f\u6301\uff0c\u5219\u8fd0\u884c getCurrentPosition() \u65b9\u6cd5\u3002\u5982\u679c\u4e0d\u652f\u6301\uff0c\u5219\u5411\u7528\u6237\u663e\u793a\u4e00\u6bb5\u6d88\u606f\u3002<\/li>\n
  3. \u5982\u679c getCurrentPosition() \u8fd0\u884c\u6210\u529f\uff0c\u5219\u5411\u53c2\u6570showPosition\u4e2d\u89c4\u5b9a\u7684\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a coordinates \u5bf9\u8c61<\/li>\n
  4. showPosition() \u51fd\u6570\u83b7\u5f97\u5e76\u663e\u793a\u7ecf\u5ea6\u548c\u7eac\u5ea6<\/li>\n<\/ol>\n

    \u4e0a\u9762\u7684\u4f8b\u5b50\u662f\u4e00\u4e2a\u975e\u5e38\u57fa\u7840\u7684\u5730\u7406\u5b9a\u4f4d\u811a\u672c\uff0c\u4e0d\u542b\u9519\u8bef\u5904\u7406\u3002<\/p>\n

    \u5904\u7406\u9519\u8bef\u548c\u62d2\u7edd<\/strong><\/div>\n

    getCurrentPosition() \u65b9\u6cd5\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u7528\u4e8e\u5904\u7406\u9519\u8bef\u3002\u5b83\u89c4\u5b9a\u5f53\u83b7\u53d6\u7528\u6237\u4f4d\u7f6e\u5931\u8d25\u65f6\u8fd0\u884c\u7684\u51fd\u6570\uff1a<\/p>\n

    \u5b9e\u4f8b<\/p>\n

    function showError(error)\r\n{\r\n    switch(error.code) \r\n    {\r\n        case error.PERMISSION_DENIED:\r\n            x.innerHTML=\"\u7528\u6237\u62d2\u7edd\u5bf9\u83b7\u53d6\u5730\u7406\u4f4d\u7f6e\u7684\u8bf7\u6c42\u3002\"\r\n            break;\r\n        case error.POSITION_UNAVAILABLE:\r\n            x.innerHTML=\"\u4f4d\u7f6e\u4fe1\u606f\u662f\u4e0d\u53ef\u7528\u7684\u3002\"\r\n            break;\r\n        case error.TIMEOUT:\r\n            x.innerHTML=\"\u8bf7\u6c42\u7528\u6237\u5730\u7406\u4f4d\u7f6e\u8d85\u65f6\u3002\"\r\n            break;\r\n        case error.UNKNOWN_ERROR:\r\n            x.innerHTML=\"\u672a\u77e5\u9519\u8bef\u3002\"\r\n            break;\r\n    }\r\n}<\/pre>\n

    \u9519\u8bef\u4ee3\u7801\uff1a<\/p>\n

      \n
    1. Permission denied - \u7528\u6237\u4e0d\u5141\u8bb8\u5730\u7406\u5b9a\u4f4d<\/li>\n
    2. Position unavailable - \u65e0\u6cd5\u83b7\u53d6\u5f53\u524d\u4f4d\u7f6e<\/li>\n
    3. Timeout - \u64cd\u4f5c\u8d85\u65f6<\/li>\n<\/ol>\n
      \u5728\u5730\u56fe\u4e2d\u663e\u793a\u7ed3\u679c<\/strong><\/div>\n

      \u5982\u9700\u5728\u5730\u56fe\u4e2d\u663e\u793a\u7ed3\u679c\uff0c\u60a8\u9700\u8981\u8bbf\u95ee\u53ef\u4f7f\u7528\u7ecf\u7eac\u5ea6\u7684\u5730\u56fe\u670d\u52a1\uff0c\u6bd4\u5982\u8c37\u6b4c\u5730\u56fe\u6216\u767e\u5ea6\u5730\u56fe\uff1a<\/p>\n

      \u5b9e\u4f8b<\/p>\n

      function showPosition(position)\r\n{\r\n    var latlon=position.coords.latitude+\",\"+position.coords.longitude;\r\n \r\n    var img_url=\"http:\/\/maps.googleapis.com\/maps\/api\/staticmap?center=\"\r\n    +latlon+\"&zoom=14&size=400x300&sensor=false\";\r\n    document.getElementById(\"mapholder\").innerHTML=\"<img src='\"+img_url+\"'>\";\r\n}<\/pre>\n

      \u5728\u4e0a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u8fd4\u56de\u7684\u7ecf\u7eac\u5ea6\u6570\u636e\u5728\u8c37\u6b4c\u5730\u56fe\u4e2d\u663e\u793a\u4f4d\u7f6e\uff08\u4f7f\u7528\u9759\u6001\u56fe\u50cf\uff09\u3002<\/p>\n

      Google\u5730\u56fe\u811a\u672c
      \n\u4e0a\u9762\u7684\u94fe\u63a5\u5411\u60a8\u6f14\u793a\u5982\u4f55\u4f7f\u7528\u811a\u672c\u6765\u663e\u793a\u5e26\u6709\u6807\u8bb0\u3001\u7f29\u653e\u548c\u62d6\u66f3\u9009\u9879\u7684\u4ea4\u4e92\u5f0f\u5730\u56fe\u3002<\/p>\n

      \u7ed9\u5b9a\u4f4d\u7f6e\u7684\u4fe1\u606f<\/strong><\/div>\n

      \u672c\u9875\u6f14\u793a\u7684\u662f\u5982\u4f55\u5728\u5730\u56fe\u4e0a\u663e\u793a\u7528\u6237\u7684\u4f4d\u7f6e\u3002\u4e0d\u8fc7\uff0c\u5730\u7406\u5b9a\u4f4d\u5bf9\u4e8e\u7ed9\u5b9a\u4f4d\u7f6e\u7684\u4fe1\u606f\u540c\u6837\u5f88\u6709\u7528\u5904\u3002<\/p>\n

      \u53ef\u7528\u4e8e:<\/p>\n

        \n
      1. \u66f4\u65b0\u672c\u5730\u4fe1\u606f<\/li>\n
      2. \u663e\u793a\u7528\u6237\u5468\u56f4\u7684\u5174\u8da3\u70b9<\/li>\n
      3. \u4ea4\u4e92\u5f0f\u8f66\u8f7d\u5bfc\u822a\u7cfb\u7edf(GPS) <\/li>\n<\/ol>\n
        getCurrentPosition() \u65b9\u6cd5 - \u8fd4\u56de\u6570\u636e<\/strong><\/div>\n

        T\u82e5\u6210\u529f\uff0c\u5219 getCurrentPosition() \u65b9\u6cd5\u8fd4\u56de\u5bf9\u8c61\u3002\u59cb\u7ec8\u4f1a\u8fd4\u56de latitude\u3001longitude \u4ee5\u53ca accuracy \u5c5e\u6027\u3002\u5982\u679c\u53ef\u7528\uff0c\u5219\u4f1a\u8fd4\u56de\u5176\u4ed6\u4e0b\u9762\u7684\u5c5e\u6027\u3002<\/p>\n\n\n\n\n\n\n\n\n\n\n\n
        \u5c5e\u6027<\/th>\n\u63cf\u8ff0<\/th>\n<\/tr>\n
        coords.latitude<\/td>\n\u5341\u8fdb\u5236\u6570\u7684\u7eac\u5ea6<\/td>\n<\/tr>\n
        coords.longitude<\/td>\n\u5341\u8fdb\u5236\u6570\u7684\u7ecf\u5ea6<\/td>\n<\/tr>\n
        coords.accuracy<\/td>\n\u4f4d\u7f6e\u7cbe\u5ea6<\/td>\n<\/tr>\n
        coords.altitude<\/td>\n\u6d77\u62d4\uff0c\u6d77\u5e73\u9762\u4ee5\u4e0a\u4ee5\u7c73\u8ba1<\/td>\n<\/tr>\n
        coords.altitudeAccuracy<\/td>\n\u4f4d\u7f6e\u7684\u6d77\u62d4\u7cbe\u5ea6<\/td>\n<\/tr>\n
        coords.heading<\/td>\n\u65b9\u5411\uff0c\u4ece\u6b63\u5317\u5f00\u59cb\u4ee5\u5ea6\u8ba1<\/td>\n<\/tr>\n
        coords.speed<\/td>\n\u901f\u5ea6\uff0c\u4ee5\u7c73\/\u6bcf\u79d2\u8ba1<\/td>\n<\/tr>\n
        timestamp<\/td>\n\u54cd\u5e94\u7684\u65e5\u671f\/\u65f6\u95f4<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
        Geolocation \u5bf9\u8c61 - \u5176\u4ed6\u6709\u8da3\u7684\u65b9\u6cd5<\/strong><\/div>\n

        watchPosition() - \u8fd4\u56de\u7528\u6237\u7684\u5f53\u524d\u4f4d\u7f6e\uff0c\u5e76\u7ee7\u7eed\u8fd4\u56de\u7528\u6237\u79fb\u52a8\u65f6\u7684\u66f4\u65b0\u4f4d\u7f6e\uff08\u5c31\u50cf\u6c7d\u8f66\u4e0a\u7684 GPS\uff09\u3002<\/p>\n

        clearWatch() - \u505c\u6b62 watchPosition() \u65b9\u6cd5<\/p>\n

        \u4e0b\u9762\u7684\u4f8b\u5b50\u5c55\u793a watchPosition() \u65b9\u6cd5\u3002\u60a8\u9700\u8981\u4e00\u53f0\u7cbe\u786e\u7684 GPS \u8bbe\u5907\u6765\u6d4b\u8bd5\u8be5\u4f8b\uff08\u6bd4\u5982 iPhone\uff09\uff1a<\/p>\n

        \u5b9e\u4f8b<\/p>\n

        var x=document.getElementById(\"demo\");\r\nfunction getLocation()\r\n{\r\n    if (navigator.geolocation)\r\n    {\r\n        navigator.geolocation.watchPosition(showPosition);\r\n    }\r\n    else\r\n    {\r\n        x.innerHTML=\"\u8be5\u6d4f\u89c8\u5668\u4e0d\u652f\u6301\u83b7\u53d6\u5730\u7406\u4f4d\u7f6e\u3002\";\r\n    }\r\n}\r\nfunction showPosition(position)\r\n{\r\n    x.innerHTML=\"\u7eac\u5ea6: \" + position.coords.latitude + \r\n    \"
        \u7ecf\u5ea6: \" + position.coords.longitude; \r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"

        HTML5 Geolocation API \u7528\u4e8e\u83b7\u5f97\u7528\u6237\u7684\u5730\u7406\u4f4d\u7f6e\u3002 \u9274\u4e8e\u8be5\u7279\u6027\u53ef\u80fd\u4fb5\u72af\u7528\u6237\u7684\u9690\u79c1\uff0c\u9664\u975e\u7528\u6237\u540c […]<\/p>\n","protected":false},"author":1470,"featured_media":256779,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-256770","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/256770","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1470"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=256770"}],"version-history":[{"count":7,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/256770\/revisions"}],"predecessor-version":[{"id":256780,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/256770\/revisions\/256780"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/256779"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=256770"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=256770"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=256770"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}