2016-11-08 99 views
-1

谷歌API使用JavaScript

var map; 
 

 
function initMap() { 
 
    map = new google.maps.Map(document.getElementById('map'), { 
 
    center: { 
 
     lat: 30.3434, 
 
     lng: 30.234 
 
    }, 
 
    zoom: 8 
 
    }); 
 
}
/* Always set the map height explicitly to define the size of the div 
 
     * element that contains the map. */ 
 

 
#map { 
 
    height: 100%; 
 
} 
 
/* Optional: Makes the sample page fill the window. */ 
 

 
html, 
 
body { 
 
    height: 100%; 
 
    margin: 0; 
 
    padding: 0; 
 
}
<div id="map"></div> 
 
<script src="https://maps.googleapis.com/maps/api/js?key=&callback=initMap" async defer></script>

如何輸入座標自動谷歌API的Java腳本?! 這裏是代碼

<!DOCTYPE html> 
<html> 
    <head> 
    <title>Simple Map</title> 
    <meta name="viewport" content="initial-scale=1.0"> 
    <meta charset="utf-8"> 
    <style> 
     /* Always set the map height explicitly to define the size of the div 
     * element that contains the map. */ 
     #map { 
     height: 100%; 
     } 
     /* Optional: Makes the sample page fill the window. */ 
     html, body { 
     height: 100%; 
     margin: 0; 
     padding: 0; 
     } 
    </style> 
    </head> 
    <body> 
    <div id="map"></div> 
    <script> 
     var map; 
     function initMap() { 
     map = new google.maps.Map(document.getElementById('map'), { 
      center: {lat:30.3434, lng:30.234}, 
      zoom: 8 
     }); 
     } 
    </script> 
    <script src="https://maps.googleapis.com/maps/api/js?key=&callback=initMap" 
    async defer></script> 
    </body> 
</html> 

我想「中心」,採取從一個txt文件中的座標,在我的電腦在作爲測試

+1

你能澄清你的問題?你是否願意知道你應該在哪裏寫你的座標? –

+0

自動您的意思是**動態**權利? –

+1

那麼你知道如何加載文件嗎? – epascarello

回答

0

假設你的文件是location.txt,內容是這樣的: lat;long,例如30.3434;30.234

你可以做你想要使用jQuery的:

function initMap() { 
    var myLat, myLng, myMap; 
    $.ajax({ 
     url : "location.txt", 
     dataType: "text", 
     success : function (data) { 
      var location = data.split(";"); 
      myLat=location[0]; 
      myLng=location[1] 
     }, 
     error: function(data) { 
      console.log("unable to retrieve data from location.txt") 
     }, 
     complete: function(data) { 
      //Called after success and error callbacks are executed. 
      myMap = new google.maps.Map(document.getElementById('map'), { 
       center: {lat:myLat, lng:myLng}, 
       zoom: 8 
      }); 
     } 
    }); 
}