Fundamentos de Google Maps

创建基础的谷歌地图

此例创建以英国伦敦为中心的谷歌地图:

Ejemplo

<!DOCTYPE html>
<html>
<body>
<h1>我的第一个谷歌地图</h1>
<div id="googleMap" style="width:100%;height:400px;"></div>
<script>
function myMap() {
var mapProp= {
  center: new google.maps.LatLng(51.508742,-0.120850),
  zoom:5,
};
var map = new google.maps.Map(document.getElementById("googleMap"),mapProp);
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_KEY&callback=myMap"></script>
</body>
</html>

接下来 ,我们会一步步研究上面这个例子。

地图容器和尺寸

地图需要一个 HTML 元素来存放地图:

<div id="googleMap" style="width:100%;height:400px"></div>

同时还要设置地图的大小。

创建一个函数来设置地图属性

function myMap() {
var mapProp = {
    center: new google.maps.LatLng(51.508742,-0.120850),
    zoom:5,
};
var map = new google.maps.Map(document.getElementById("googleMap"),mapProp);
}

mapProp Las variables definen las propiedades del mapa.

center La propiedad especifica la posición central del mapa (utilice las coordenadas de latitud y longitud).

zoom La propiedad especifica el nivel de zoom del mapa (intente cambiar el nivel de zoom del mapa).

Vea esta línea:

var map=new google.maps.Map(document.getElementById("googleMap"), mapProp); 

Usa los parámetros pasados (mapProp) Crear un nuevo mapa dentro del elemento <div> con id="googleMap".

Múltiples mapas

El siguiente ejemplo define cuatro mapas de tipos de mapas diferentes:

Ejemplo

var map1 = new google.maps.Map(document.getElementById("googleMap1"), mapOptions1);
var map2 = new google.maps.Map(document.getElementById("googleMap2"), mapOptions2);
var map3 = new google.maps.Map(document.getElementById("googleMap3"), mapOptions3);
var map4 = new google.maps.Map(document.getElementById("googleMap4"), mapOptions4);

Clave API gratuita de Google

Google permite a los sitios web llamar gratuitamente a cualquier API de Google, miles de veces al día.

Visite el siguiente enlace para obtener instrucciones sobre cómo obtener la clave API:

https://developers.google.com/maps/documentation/javascript/get-api-key

Google Maps necesita estar en key Encontrar esta clave API en los parámetros:

<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_KEY&callback=myMap"></script>