当前位置: 首页 > 面试题库 >

Google Maps-来自extern json的多个标记

程修雅
2023-03-14
问题内容

我必须向Google地图添加多个标记,但数据位于extern json文件中。

目前我正在这样运行

var json = [
  {
    "title": "Stockholm",
    "lat": 59.3,
    "lng": 18.1,
    "description": "Stockholm is the capital and the largest city of Sweden and constitutes the most populated urban area in Scandinavia with a population of 2.1 million in the metropolitan area (2010)"
  },
  {
    "title": "Oslo",
    "lat": 59.9,
    "lng": 10.8,
    "description": "Oslo is a municipality, and the capital and most populous city of Norway with a metropolitan population of 1,442,318 (as of 2010)."
  },
  {
    "title": "Copenhagen",
    "lat": 55.7,
    "lng": 12.6,
    "description": "Copenhagen is the capital of Denmark and its most populous city, with a metropolitan population of 1,931,467 (as of 1 January 2012)."
  }
];



for (var i = 0, length = json.length; i < length; i++) {
  var data = json[i],
      latLng = new google.maps.LatLng(data.lat, data.lng);

  // Creating a marker and putting it on the map
  var marker = new google.maps.Marker({
    position: latLng,
    map: map,
    title: data.title
  });
}

现在,我试图将Json文件排除到另一个文件中,但是Sadyl我无法使它工作;(

$.getJSON("foo.txt", function(json1) {

});


for (var i = 0, length = json.length; i < length; i++) {
  var data = json[i],
      latLng = new google.maps.LatLng(data.lat, data.lng);

  // Creating a marker and putting it on the map
  var marker = new google.maps.Marker({
    position: latLng,
    map: map,
    title: data.title
  });
}

foo.txt

{
    "title": "Stockholm",
    "lat": 59.3,
    "lng": 18.1,
    "description": "Stockholm is the capital and the largest city of Sweden and constitutes the most populated urban area in Scandinavia with a population of 2.1 million in the metropolitan area (2010)"
  },
  {
    "title": "Oslo",
    "lat": 59.9,
    "lng": 10.8,
    "description": "Oslo is a municipality, and the capital and most populous city of Norway with a metropolitan population of 1,442,318 (as of 2010)."
  },
  {
    "title": "Copenhagen",
    "lat": 55.7,
    "lng": 12.6,
    "description": "Copenhagen is the capital of Denmark and its most populous city, with a metropolitan population of 1,931,467 (as of 1 January 2012)."
  }

谢谢你的帮助


问题答案:

您的代码中有两个问题。您的json文件[在开头和]结尾都缺少。您的JavaScript也是错误的,您想对的回调中的json进行操作getJSON。您的问题的代码是:

$.getJSON("foo.txt", function(json1) {
    $.each(json1, function(key, data) {
        var latLng = new google.maps.LatLng(data.lat, data.lng); 
        // Creating a marker and putting it on the map
        var marker = new google.maps.Marker({
            position: latLng,
            map: map,
            title: data.title
        });
    });
});

编辑:

这是一个基于google
maps教程
的工作示例。您需要正确的文件foo.txt

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
      html { height: 100% }
      body { height: 100%; margin: 0; padding: 0 }
      #map_canvas { height: 100% }
    </style>
    <script type="text/javascript"
      src="http://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&sensor=true">
    </script>
    <script type="text/javascript">
      var map;
      function initialize() {
        var mapOptions = {
          center: new google.maps.LatLng(58, 16),
          zoom: 7,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById("map_canvas"),
            mapOptions);
      }
    </script>
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>    
  </head>
  <body onload="initialize()">
    <div id="map_canvas" style="width:100%; height:100%"></div>
    <script type="text/javascript">
      $(document).ready(function() {
        $.getJSON("foo.txt", function(json1) {
          $.each(json1, function(key, data) {
            var latLng = new google.maps.LatLng(data.lat, data.lng); 
            // Creating a marker and putting it on the map
            var marker = new google.maps.Marker({
                position: latLng,
                title: data.title
            });
            marker.setMap(map);
          });
        });
      });
    </script>
  </body>
</html>


 类似资料:
  • 我有代码允许我输入带有纬度和经度的位置名称,然后地图用标记显示位置。 我遇到的问题是,我希望地图显示所有位置,每个位置都有自己的标记。 下面是我用来获取地图上显示的一个位置的代码 非常感谢您的帮助 我没有错误只是显示了一个位置,我已添加到Firebasedatabase

  • 我必须允许用户输入多个邮政编码,从数据库中检索纬度和经度,然后构建一个包含它们的巨大多边形。 我需要在代码中修改什么才能将所有这些较小的多边形变成一个较大的多边形?我在谷歌上搜索过答案,我所能找到的只是逐个构建每个邮政编码的多边形,但这仍然不能给我一个更大、单个多边形的最终结果。 目前,输入邮政编码后,程序从数据库中收集lat和long点,并将它们输入一个巨大的数组(确切地说是字符串[][]),然

  • 问题内容: 如何从MySQL的多个表中选择COUNT(*)? 如: 编辑: 目标是返回此: 问题答案: 您可以通过使用子查询来实现,每个tableCount一个子查询:

  • 如何修改此ffmpeg字符串以生成具有不同视频比特率的多个输出?这是为了在yadif=1消耗大量电力时节省时间。而且,无法让它在windows中接受yadif_cuda。 ffmpeg-y-f lavfi-i anullsrc=cl=mono:sample_rate=48000-i“test.mxf”-vf yadif=1-s 1920:1080-c:v h264-nvenc-强制关键帧“expr

  • 我正在构建一个数据库的模式。我有下面两张表。。。 我想创建一个名为hasTool的表,它使用外键约束具有两个表的主键。然而,除此之外,我还想使这两个字段都成为新表hasTool的复合键。我尝试了以下方法,但似乎没有产生预期的行为: 我想允许表允许多个实例的userID或工具ID,但不是两者,并表用户和工具上的现有值。 提前谢谢!

  • Flask Google Maps Easy to use Google Maps in your Flask application requires Jinja Flask A google api key get here Contribute To contribute with the project, clone it, create a virtualenv and install