Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript:使用json文件在google地图上添加标记_Javascript_Google Maps_Google Maps Api 3 - Fatal编程技术网

Javascript:使用json文件在google地图上添加标记

Javascript:使用json文件在google地图上添加标记,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,我正在尝试使用谷歌地图添加新加坡公交车站的标记。 我的代码: 请告诉我哪里出了问题。谢谢很可能是时间问题。从initialize函数调用asynchronous$.getJSON函数,或者处理映射在运行时未初始化的情况 var map; function initialize() { var mapOptions = { center: new google.maps.LatLng(1.290270, 103.851959), zoom: 14, mapTypeId:

我正在尝试使用谷歌地图添加新加坡公交车站的标记。 我的代码:


请告诉我哪里出了问题。谢谢

很可能是时间问题。从initialize函数调用asynchronous
$.getJSON
函数,或者处理映射在运行时未初始化的情况

var map;
function initialize() {
  var mapOptions = {
    center: new google.maps.LatLng(1.290270, 103.851959),
    zoom: 14,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };

  map = new google.maps.Map(document.getElementById("map_canvas"),
    mapOptions);
  $.getJSON("bus-stops.json", 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.name
      });
      marker.setMap(map);
    });
  });
}

很可能是时间问题。从initialize函数调用asynchronous
$.getJSON
函数,或者处理映射在运行时未初始化的情况

var map;
function initialize() {
  var mapOptions = {
    center: new google.maps.LatLng(1.290270, 103.851959),
    zoom: 14,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };

  map = new google.maps.Map(document.getElementById("map_canvas"),
    mapOptions);
  $.getJSON("bus-stops.json", 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.name
      });
      marker.setMap(map);
    });
  });
}

你能检查它是否正确地读取文件吗,即
json1
是否有值,以及
是否正确地循环所有你期望的行,如果你在其中放置
console.log(数据)
?我放置了一个console.log(数据),它在使用Mozilla上的firebug的控制台中没有显示任何内容。我猜json1没有得到任何值。请建议。您的jQuery文件正在加载吗?在代码中,您将文件引用为
jquery-latest.min.js
,这是不正确的(文件名中没有空格)。我再次检查,文件名中没有空格。我试图在浏览器中打开它,但它已正确打开,问题已得到解决,这是一个时间问题,回答如下。再次感谢!!你能检查它是否正确地读取文件吗,即
json1
是否有值,以及
是否正确地循环所有你期望的行,如果你在其中放置
console.log(数据)
?我放置了一个console.log(数据),它在使用Mozilla上的firebug的控制台中没有显示任何内容。我猜json1没有得到任何值。请建议。您的jQuery文件正在加载吗?在代码中,您将文件引用为
jquery-latest.min.js
,这是不正确的(文件名中没有空格)。我再次检查,文件名中没有空格。我试图在浏览器中打开它,但它已正确打开,问题已得到解决,这是一个时间问题,回答如下。再次感谢!!谢谢你,先生!这确实有帮助,我正确地理解了情节。再次感谢。谢谢你,先生!这确实有帮助,我正确地理解了情节。再次感谢。
var map;
function initialize() {
  var mapOptions = {
    center: new google.maps.LatLng(1.290270, 103.851959),
    zoom: 14,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };

  map = new google.maps.Map(document.getElementById("map_canvas"),
    mapOptions);
  $.getJSON("bus-stops.json", 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.name
      });
      marker.setMap(map);
    });
  });
}