【问题标题】:markers are not showing on the map标记未显示在地图上
【发布时间】:2017-01-09 17:13:03
【问题描述】:

我有这个代码可以从数据库中获取 lat 和 lng,并在带有标记的谷歌地图上显示它,但它不显示任何标记。

<!doctype html>
<html>
<head>
    <meta charset='utf-8'/>
    <title>Google Maps</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
    <script src='https://maps.google.com/maps/api/js?key=AIzaSyAKcut1aeRFdjPTS5QPefgbrnQIAVkGuis' type='text/javascript'></script>
    <script type='text/javascript'>
        (function(){

            var map,marker,latlng,bounds,infowin;
            /* initial locations for map */
            var _lat=14.676;
            var _lng=121.0437;



            function showMap(){
                /* set the default initial location */
                latlng={ lat: _lat, lng: _lng };

                bounds = new google.maps.LatLngBounds();
                infowin = new google.maps.InfoWindow();

                /* invoke the map */
                map = new google.maps.Map( document.getElementById('map'), {
                   center:latlng,
                   zoom: 10
                });

                /* show the initial marker */
                marker = new google.maps.Marker({
                   position:latlng,
                   map: map,
                   title: 'Hello World!'
                });
                bounds.extend( marker.position );


                /* I think you can use the jQuery like this within the showMap function? */
                $.ajax({
                    url: get.php,
                    data: {'ajax':true },
                    dataType: 'json',
                    success: function( data, status ){
                        $.each( data, function( i,item ){
                            /* add a marker for each location in response data */ 
                            addMarker( item.lat, item.lng, item.name );
                        });
                    },
                    error: function(){
                        output.text('There was an error loading the data.');
                    }
                });                 
            }

            /* simple function just to add a new marker */
            function addMarker(lat,lng,title){
                marker = new google.maps.Marker({/* Cast the returned data as floats using parseFloat() */
                   position:{ lat:parseFloat( lat ), lng:parseFloat( lng ) },
                   map:map,
                   title:title
                });

                google.maps.event.addListener( marker, 'click', function(event){
                    infowin.setContent(this.title);
                    infowin.open(map,this);
                    infowin.setPosition(this.position);
                }.bind( marker ));

                bounds.extend( marker.position );
                map.fitBounds( bounds );
            }


            document.addEventListener( 'DOMContentLoaded', showMap, false );
        }());
    </script>
    <style>
        html, html body, #map{ height:100%; width:100%; padding:0; margin:0; }
    </style>
</head>
<body>
    <div id='map'></div>
</body>

这是我从数据库中获取数据的get.php。

$mysql ="SELECT lat,lng FROM `coordinates`";
$result = mysqli_query($connect, $mysql);
if (!empty($result))
{

while ($row=mysqli_fetch_array($result))
{
    $latlng[] = array(
    'lat' => $row['lat'], 
    'lng' => $row['lng'],
    );

  }
 }

  mysqli_close($connect);

 header('Content-Type:application/json');
  echo json_encode($latlng);
 ?>

这个功能正在运行并从数据库中获取经纬度和经度,但标记仍未显示在谷歌地图中。谁能告诉我为什么?

【问题讨论】:

    标签: php mysql ajax google-maps google-maps-api-3


    【解决方案1】:

    您没有从数据库中选择 "name",而是使用 item.name 作为 addMarker() 函数的参数。

    所以把你的mysql查询改成:

    "SELECT lat,lng,name FROM `coordinates`";
    

    然后添加到数组中:

    $latlng[] = array(
    'lat' => $row['lat'], 
    'lng' => $row['lng'],
    'name' => $row['name']
    );
    

    我尝试了您的 javascript 代码,看起来添加标记在没有 ajax 的情况下也可以工作。请在您的问题中添加 JSON 字符串。

    如果您在使用 AJAX 添加标记时仍有问题,您应该发送 JSON(只需在浏览器中打开 get.php)并检查开发者控制台中是否有任何错误 - 对于 firefox (CTRL +SHIFT+K)。

    【讨论】:

    • 它仍然没有向地图添加标记。您认为问题出在“url”中的 ajax 中吗?路径是否正确?我的“get.php”在线保存/存储在免费托管网站上。
    • 在不使用 ajax 的情况下它是如何为您工作的?我可以看到代码吗?
    • @Monds 知道问题出在哪里的最好方法是在浏览器中检查 get.php 文件。您应该看到 JSON 字符串或错误。尝试从您的地址托管在浏览器上打开 get.php 并发送它所写的内容。
    • @Monds 我只使用你的 javascript 代码,仅此而已,如果我将 item.name 替换为“example”,效果会很好。所以问题一定出在php文件中或在javascript中读取json。我们真的需要看看 get.php 在你的浏览器中写了什么。没有信息,真的很难找到解决方案。因此,只需将 www.youraddress.com/get.php(或只是您文件的 url)放到您的浏览器中,然后查看返回的文件是什么
    • @Monds 如果您在主机上有 get.php 文件,您应该在 ajax 地址中拥有类似:monds.com/get.php 或类似的东西,而不仅仅是 get.php
    猜你喜欢
    • 2018-03-30
    • 1970-01-01
    • 2021-07-07
    • 2015-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多