【问题标题】:Send lat lng from variable to google maps将 lat lng 从变量发送到谷歌地图
【发布时间】:2017-11-17 03:01:42
【问题描述】:

我正在尝试使用 Google 地图 API(我是新手)编写一个简单的代码,其想法是从 JSON 变量方向获取纬度和经度到 var uluru。 我有以下代码: 我哪里错了?

提前致谢。

<!DOCTYPE html>
<html>
  <head>
    <style>
       #map {
        height: 400px;
        width: 100%;
       }
    </style>
  </head>
  <body>
    <h3>My Google Maps Demo</h3>
    <div id="map"></div>
    <script>


      function initMap() {


      var direccion=[
        {
          lugar:'CICESE',
          lat:31.8675375,
          lng: -116.6686867
        },
        {
          lugar: 'Casa',
          lat:31.8915163,
          lng:-116.6879557
        }
      ];


        var uluru = {lat: direccion.lat, lng: direccion.lng};//{lat: -25.363, lng: 131.044};
        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 4,
          center: uluru
        });
        var marker = new google.maps.Marker({
          position: uluru,
          map: map
        });
      }
    </script>
    <script async defer
    src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDgMyVeh8DwebGfZHjiNjyle4xFe9pKSdc&callback=initMap">
    </script>
  </body>
</html>

【问题讨论】:

  • 请查看How to Ask 并编辑您的帖子以包含有关您预期会发生什么以及正在发生什么的信息。它失败了吗?你得到什么错误代码?
  • 好的,我已经改变了我的问题。

标签: html json api maps


【解决方案1】:

你的方向是json数组,你需要通过索引访问如下:

var uluru = {lat: direccion[0].lat, lng: direccion[0].lng};

请参阅下面的示例:

<!DOCTYPE html>
<html>
  <head>
    <style>
       #map {
        height: 400px;
        width: 100%;
       }
    </style>
  </head>
  <body>
    <h3>My Google Maps Demo</h3>
    <div id="map"></div>
    <script>


      function initMap() {


      var direccion=[
        {
          lugar:'CICESE',
          lat:31.8675375,
          lng: -116.6686867
        },
        {
          lugar: 'Casa',
          lat:31.8915163,
          lng:-116.6879557
        }
      ];


        var uluru = {lat: direccion[0].lat, lng: direccion[0].lng};//{lat: -25.363, lng: 131.044};
        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 4,
          center: uluru
        });
        var marker = new google.maps.Marker({
          position: uluru,
          map: map
        });
      }
    </script>
    <script async defer
    src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDgMyVeh8DwebGfZHjiNjyle4xFe9pKSdc&callback=initMap">
    </script>
  </body>
</html>

【讨论】:

  • 非常感谢,我没有注意到。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-13
  • 1970-01-01
  • 2013-09-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多