2016-11-08 97 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 
      }); 
     } 
    }); 
}