调用googleMapApi
admin
2023-06-11 20:41:48
0

 基本调用

  1. > 
  2. <html> 
  3. <head> 
  4. <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> 
  5. <style type="text/css"> 
  6.   html { height: 100% } 
  7.   body { height: 100%; margin: 0px; padding: 0px } 
  8.   #map_canvas { height: 100% } 
  9. style> 
  10. <script type="text/javascript" 
  11.     src="https://maps.google.com/maps/api/js?sensor=true"> 
  12. script> 
  13. <script type="text/javascript"> 
  14.   function initialize() { 
  15.     var latlng = new google.maps.LatLng(-34.397, 150.644); 
  16.     var myOptions = { 
  17.       zoom: 8, 
  18.       center: latlng, 
  19.       mapTypeId: google.maps.MapTypeId.ROADMAP 
  20.     }; 
  21.     var map = new google.maps.Map(document.getElementById("map_canvas"), 
  22.         myOptions); 
  23.   } 
  24.  
  25. script> 
  26. head> 
  27. <body onload="initialize()"> 
  28.   <div id="map_canvas" style="width:100%; height:100%">div> 
  29. body> 
  30. html> 

基于html5的稍微复杂的Goolge Map 调用

 

  1. > 
  2. <html> 
  3. <head> 
  4. <meta charset="utf-8"> 
  5. <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> 
  6. <style type="text/css"> 
  7.   html { height:800px; width:480px; } 
  8.   body { height: 100%; margin: 0px; padding: 0px } 
  9.   #map_canvas { height: 100% } 
  10.   #metaDataTable { position:absolute; top:30px; left:0px; z-index:1000;} 
  11. style> 
  12. <title>html5_Geolocationtitle> 
  13. <script type="text/javascript" 
  14.     src="https://maps.google.com/maps/api/js?sensor=false"> 
  15. script> 
  16.  
  17. head> 
  18.  
  19. <body> 
  20. <div id="metaDataTable"> 
  21.  <h2> Html5 Geolocation Distance Trackerh2> 
  22.  <p id="status"> Html5 Geolocation is<strong> notstrong> suppert your browser!!!p> 
  23. <h3>Current Position:h3> 
  24. <table border="1"> 
  25.      <tr> 
  26.         <td width=40 scope="col">纬度td> 
  27.         <td wisth=114 id="latitude">?td> 
  28.      tr> 
  29.      <tr> 
  30.         <td width=40 scope="col">经度td> 
  31.         <td wisth=114 id="longitude">?td> 
  32.      tr> 
  33.       <tr> 
  34.         <td width=40 scope="col">海拔td> 
  35.         <td wisth=114 id="accuracy">?td> 
  36.      tr> 
  37.        <tr> 
  38.         <td width=40 scope="col">Last TimeStamptd> 
  39.         <td wisth=114 id="timestamp">?td> 
  40.      tr> 
  41. table> 
  42. <h5 id="currDist"> Current distance traveled:0.0KMh5> 
  43. <h5 id="totalDist">Total distance traveled:0.0KMh5> 
  44. div> 
  45. <script> 
  46.       var totalDistance = 0.0; 
  47.       var lastLat; 
  48.       var lastLong; 
  49.       var latitude; 
  50.       var longitude; 
  51. /*function getGeolacation(){ 
  52.     if(navigator.geolocation){ 
  53.         alert("Support the html5 Geolacation API"); 
  54.         navigator.geolocation.getCurrentPosition(updateLocation); 
  55.         } 
  56.     }*/ 
  57.     function updateLocation(position){ 
  58.          latitude = position.coords.latitude; 
  59.          longitude = position.coords.longitude; 
  60.         var accuracy = position.coords.accuracy; 
  61.         var timestamp = position.timestamp; 
  62.         document.getElementById("latitude").innerHTML =latitude ; 
  63.         document.getElementById("longitude").innerHTML = longitude
  64.         document.getElementById("accuracy").innerHTML = accuracy
  65.         document.getElementById("timestamp").innerHTML = timestamp
  66.         if(accuracy >= 500){ 
  67.           updateStatus("need more accure vslues to calculate distance."); 
  68.           return; 
  69.         } 
  70.         //计算距离 
  71.         if((lastLat != null)&&(lastLong != null)){ 
  72.            var CurrentDistance = distance(latitude,longitude,lastLat,lastLong); 
  73.            document.getElementById("currDist").innerHTML = "Current distance traveled"+CurrentDistance+"KM"; 
  74.            totalDistance  += CurrentDistance; 
  75.            document.getElementById("totalDist").innerHTML="=Total distance traveled "+totalDistance+"KM"; 
  76.         } 
  77.         lastLat = latitude
  78.         lastLong = longitude
  79.         updateStatus("Loacation Successfully uodated!!!"); 
  80.         initialize(latitude,longitude); 
  81.         } 
  82.         function handleLocationError(error){ 
  83.           switch(error.code){ 
  84.            case 0 : 
  85.            uodateStatus("There was an error while retrieving your Location "+error.message); 
  86.            break; 
  87.             case 1 : 
  88.            uodateStatus("the user prevented this page from your Location "+error.message); 
  89.             break; 
  90.             case 2 : 
  91.            uodateStatus("the browser was unable to determine your Location "+error.message); 
  92.             break; 
  93.             case 3 : 
  94.            uodateStatus("the browser timed out before retrieving your Location "+error.message); 
  95.             break; 
  96.           } 
  97.         } 
  98.       function toRadians(degree){ 
  99.         return degree = Math.PI / 180; 
  100.       } 
  101.       function distance(latitude1, longitude1, latitude2, longitude2){ 
  102.       var R = 6371
  103.       var deltatidude = toRadians(latitude2- latitude1); 
  104.       var dellongitude = toRadians(longitude2 - longitude1); 
  105.       latitude1  = toRadians(latitude1); 
  106.       latitude2  = toRadians(latitude2); 
  107.       var a = Math.sin(deltalatitude/2)*Math.cos(deltalatitude/2)*Math.cos(latitude1)*Math.cos(latitude2)*Math.sin(dellongitude/2)*Math.sin(dellongitude/2); 
  108.       var c2*Math.atan2(Math.sqrt(a),Math.sqrt(1-a)); 
  109.       var d = R*c; 
  110.       return d; 
  111.       } 
  112.       //R是地球的半径,单位是KM 
  113.       function updateStatus(message){ 
  114.       document.getElementById("status").innerHTML = message
  115.       } 
  116.       function loadDemo(){ 
  117.         if(navigator.geolocation){ 
  118.         updateStatus("your browser is support html5 geolacation"); 
  119.         navigator.geolocation.watchPosition(updateLocation,handleLocationError,{maximumAge:20000}); 
  120.         } 
  121.       } 
  122.   function initialize(la,longi) { 
  123.        var latlng = new google.maps.LatLng(la, longi); 
  124.        var div = document.createElement("div"); 
  125.        var str = "我的当前位置"
  126.        var myOptions = { 
  127.         zoom: 18, 
  128.         center: latlng, 
  129.         mapTypeId: google.maps.MapTypeId.TERRAIN 
  130.         }; 
  131.         var map = new google.maps.Map(document.getElementById("map_canvas"), 
  132.         myOptions); 
  133.         var marker = new google.maps.Marker({ 
  134.            position:latlng, 
  135.            map:map, 
  136.            title:"My Location" 
  137.         }); 
  138.    } 
  139.       window.addEventListener("load",loadDemo, true); 
  140.       script> 
  141.       <div id="map_canvas" style="width:100%; height:100%">div> 
  142.  body> 
  143. html> 

 

相关内容

热门资讯

华为公布半导体领域重大突破 5月25日,2026国际电路与系统研讨会在上海举行,华为公司董事、半导体业务部总裁何庭波在题为《半导...
全焦段4K实况照片+8000m... 一、前言:全能无短板!荣耀600 Pro闪亮登场 现如今的中高端手机市场,可以说是卷得五花八门。大家...
强烈信号,家长们现在要连夜排队... 北京家长凌晨3点排队,只为孩子能上个中职学校。5月10日这天,是北京昌平职业技术学校自主招生面试日:...
自主选课+实战赋能,北京地铁公... 5月25日,据北京地铁公司消息,为精准破解员工岗位实训短板、满足个性化成长需求,近期北京地铁公司优化...
荣耀600 Pro测评:“销量... 荣耀的数字系列是一款典型的“销量大于网上声量”的产品。 在过去这些年,荣耀的数字系列一直都是3000...
美团、腾讯参投!机器人公司天机... 图片来源:广东天机智能系统有限官网 5月25日,天机机器人公众号发布消息称,广东天机智能系统有限公司...
伊朗外交部:中国在推动停火谈判... 伊朗外交部发言人巴加埃25日在伊朗首都德黑兰召开的记者会上谈及地区局势时表示,中国在推动地区停火与谈...
谷歌对“AI投毒”重拳出击 文 | 字母AI 当你某天一觉醒来,发现自己被困在布满屏幕的房间里,每个屏幕中播放的不是你感兴趣的...
斥资约5亿元!中国前首富钟睒睒... 快科技5月25日消息,当固态电池被视为动力电池的“终极路线”,中国前首富悄悄入局。 近日,农夫山泉创...
浑南智造闪耀“东北超”开幕式 ... 近日,备受瞩目的“东北超”开幕式震撼启幕。来自浑南区的新松机器人自动化股份有限公司携工业机器人SR2...