Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/391.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 谷歌地图不在地图上显示标记_Javascript_Google Maps Api 3 - Fatal编程技术网

Javascript 谷歌地图不在地图上显示标记

Javascript 谷歌地图不在地图上显示标记,javascript,google-maps-api-3,Javascript,Google Maps Api 3,我的谷歌地图有一个问题,它根本没有显示任何标记。标记的数据来自wordpress post和自定义字段 我为var位置做了一个控制台日志,数据被传递到此点。还为var j做了一个控制台日志,我正在那里undefined 标记不显示可能是什么问题 <?php $args = array('post_type'=> 'investments'); $the_query = new WP_Query( $args ); if ( have_posts()

我的谷歌地图有一个问题,它根本没有显示任何标记。标记的数据来自wordpress post和自定义字段

我为
var位置
做了一个控制台日志,数据被传递到此点。还为
var j
做了一个控制台日志,我正在那里
undefined

标记不显示可能是什么问题

    <?php 
     $args = array('post_type'=> 'investments'); $the_query = new WP_Query( $args );
        if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();

        $investmentsData[$row]['meta'] = get_post_meta( $post->ID, 'investments_location_details', true );
        $investmentsData[$row]['investment'] = [
          'id' => get_the_id(),
          'title' => get_the_title(),
        ];

          foreach ($investmentsData as $key => $value) {
            $investment = $value['tabs'];
            $meta = $value['meta'];
          }

          wp_localize_script('jquery-core', 'investmentsData', $investmentsData);

          ?>
          <div id="map"></div>

          <script type="text/javascript">
             var map = new google.maps.Map(document.getElementById('map'), {
                 zoom: 11,
                 center: new google.maps.LatLng(54.233985, 16.8903183),
                 mapTypeId: google.maps.MapTypeId.ROADMAP
             });
             var infowindow = new google.maps.InfoWindow();
             var marker, i;
             var locations = <?php echo json_encode($investmentsData) ?>;
             var j = locations.length;

             function initMap() {
                 for (i = 0; i < j; i++) {

                    marker = new google.maps.Marker({
                       position: new google.maps.LatLng(investmentsData[i].lat, investmentsData[i].lng),
                       map: map,
                    });

                     google.maps.event.addListener(marker, 'click', (function(marker, i) {
                         return function() {
                             infowindow.setContent(investmentsData[i].title);
                             infowindow.open(map, marker);
                         }
                     })(marker, i));
                 }
             };
         </script>

      <?php
        endwhile;
        endif;
        wp_reset_postdata();
        wp_reset_query();
      ?>
当我对
investmentsData[I]执行控制台日志时,lat
我得到了这个
Uncaught引用错误:investmentsData未定义
并且对于
位置[I]。lat
我得到了这个
Uncaught类型错误:无法读取未定义的属性“lat”

我像这样诱导API

根据对答案的评论进行更新

位置变量是一个对象:

var locations = {"":{"meta":{"lat":"54.2427318","lng":"16.8870907"},"investment":{"id":386,"title":"Termoizolacja obiekt\u00f3w u\u017cyteczno\u015bci publicznej na terenie..."}}};
代码的问题:

  • lat
    lng
    位置[i].meta
    对象的属性
  • title
    位置[i]的属性。投资
    对象
  • 您没有调用
    initMap
  • 地图的中心和缩放设置为无法看到标记
  • 更新

    根据实时页面上的信息,
    位置
    是一个对象,而不是数组:

    var locations = {"":{"meta":{"lat":"54.2427318","lng":"16.8870907"},"investment":{"id":386,"title":"Termoizolacja obiekt\u00f3w u\u017cyteczno\u015bci publicznej na terenie Gminy K\u0119pice"}}}; 
    
    通过以下方式处理:

    for (var obj in locations) {
      marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[obj].meta.lat, locations[obj].meta.lng),
        map: map,
      });
    
      google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
          infowindow.setContent(locations[obj].investment.title);
          infowindow.open(map, marker);
        }
      })(marker, i));
    }
    

    代码片段:

    html,
    身体,
    #地图{
    身高:100%;
    宽度:100%;
    边际:0px;
    填充:0px
    }
    
    函数initMap(){
    var map=new google.maps.map(document.getElementById('map'){
    缩放:7,
    中心:新google.maps.LatLng(54.23398516.8903183),
    mapTypeId:google.maps.mapTypeId.ROADMAP
    });
    var infowindow=new google.maps.infowindow();
    var标记,i;
    变量位置={
    "": {
    “元”:{
    “lat”:“54.2427318”,
    “液化天然气”:“16.8870907”
    },
    “投资”:{
    “id”:386,
    “标题”:“Termoizolacja obiekt\u00f3w u\u017cyteczno\u015bci publicznej na terenie Gminy K\u0119pice”
    }
    }
    };
    用于(位置中的var obj){
    marker=新的google.maps.marker({
    位置:新建google.maps.LatLng(位置[obj].meta.lat,位置[obj].meta.lng),
    地图:地图,
    });
    log(“标记位置:+marker.getPosition().toUrlValue(6));
    google.maps.event.addListener(标记,'click',(函数(标记,i){
    返回函数(){
    infowindow.setContent(locations[obj].investment.title);
    信息窗口。打开(地图、标记);
    }
    })(marker,i));
    }
    };
    initMap();
    
    以下是一个工作示例。具有适当的函数调用和演示数据

    函数initMap(){
    变量位置=[{
    投资:{
    id:192,
    标题:“职位标题1”
    },
    元:{
    拉丁语:“54.3765137”,
    液化天然气:“18.5707837”
    }
    },
    {
    投资:{
    身份证号码:193,
    标题:“职位标题2”
    },
    元:{
    拉丁语:“54”,
    液化天然气:“18”
    }
    }
    ];
    var map=new google.maps.map(document.getElementById('map'){
    缩放:6,
    中心:新google.maps.LatLng(54.23398516.8903183),
    mapTypeId:google.maps.mapTypeId.ROADMAP
    });
    var infowindow=new google.maps.infowindow();
    var标记,i;
    var j=位置。长度;
    对于(i=0;i
    
    #地图{
    高度:200px;
    }
    
    
    您可能需要将
    投资数据[i]
    替换为
    位置[i]
    因为您没有定义此
    investmentsData
    Javascript变量。您如何调用
    initMap
    函数?仅如代码所示,所以您不会在任何地方调用此函数。感谢您的帮助:)@Dariusz这里表示感谢的方式是向上投票或接受答案。当我使用您的位置时,一切正常好的当我输入
    var位置时,我没有任何标记。但是我在consol.log上看到了所有的数据。有什么问题吗?我问了好几次,想让他们演示一下这个问题。我猜你问题中的信息不足以重现你的问题。如果您在页面上查看源代码,那么这一行将向浏览器发送什么:
    var locations=?这就是我要找的:
    var locations={”“:{“meta”:{“lat”:“54.2427318”,“lng”:“16.8870907”},“投资”:{“id”:386,“title”:“Termoizolacja obiekt\u00f3w u\u017cyteczno\u015bci publicznej na terenie Gminy K\u0119pice”}更新了答案中的代码片段以处理此问题。感谢您的更新。当我将
    var位置
    从您的代码片段更改为
    时,还有一件事然后标记消失了
    
    for (var obj in locations) {
      marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[obj].meta.lat, locations[obj].meta.lng),
        map: map,
      });
    
      google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
          infowindow.setContent(locations[obj].investment.title);
          infowindow.open(map, marker);
        }
      })(marker, i));
    }