Google地图API无效

时间:2017-04-13 12:23:54

标签: api google-maps

<!DOCTYPE html>
<?php
    /* lat/lng data will be added to this array */
    try{$work=$_GET["service"];}catch(Exception $e){echo 'Authorization Failed.Map may Misbehave or Buggy.<a href="submitbug.php">Click here to report this</a>';}
    $locations=array();
    $uname="root";
    $pass="";
    $servername="localhost";
    $dbname="bcremote";
    $db=new mysqli($servername,$uname,$pass,$dbname);
    $query =  $db->query('SELECT * FROM location');
    while( $row = $query->fetch_assoc() ){
        $name = $row['uname'];
        $longitude = $row['longitude'];                              
        $latitude = $row['latitude'];
        $link=$row['link'];
        /* Each row is added as a new array */
        $locations[]=array( 'name'=>$name, 'lat'=>$latitude, 'lng'=>$longitude, 'lnk'=>$link );
    }
    //echo $locations[0]['name'].": In stock: ".$locations[0]['lat'].", sold: ".$locations[0]['lng'].".<br>";
    //echo $locations[1]['name'].": In stock: ".$locations[1]['lat'].", sold: ".$locations[1]['lng'].".<br>";
?>
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAfFN8NuvYyNkewBVMsk9ZNIcUWDEqHg2U&callback=initMap()"
    async defer></script>
<script>
       //var myLatLng = {lat: -25.363, lng: 131.044};
       function initMap() {
        var myLatLng = {lat: -25.363, lng: 131.044};

        var map = new google.maps.Map(document.getElementById('map'), {
          center: myLatLng,
          scrollwheel: false,
          zoom: 10
         });

        <?php for($i=0;$i<sizeof($locations);$i++)
        { ?>
         var marker = new google.maps.Marker({
          map: map,
          position: {lat: <?php echo $locations[$i]['lat']?>,lng: <?php echo $locations[$i]['lng']?>},
          title: 'Service',
          url: 'https://<?php echo $locations[$i]['lnk']?>'
        });


        google.maps.event.addListener(marker, 'click', function() {
        window.location.href = marker.url;
        });
        <?php } ?>
       }
</script>
<body>
<div id="map"></div>
</body>

我有这样的代码。我认为代码端没有任何错误可能在逻辑方面。请帮我过来。我需要在接下来的2天内提交这个项目。我知道如何解决这个问题吗?

高级谢谢, Kavin

1 个答案:

答案 0 :(得分:0)

似乎问题出在谷歌地图密钥和我处理它的方式。现在它完美无缺。一旦我更新了它,它就开始工作了。该代码也在GitHub存储库的帮助下更改。

 <?php
        $locations=array();
        $work=$_GET["service"];
        $uname="root";
        $pass="";
        $servername="localhost";
        $dbname="bcremote";
        $db=new mysqli($servername,$uname,$pass,$dbname);
        $query =  $db->query("SELECT * FROM location where work='$work'");
        //$number_of_rows = mysql_num_rows($db);  
        //echo $number_of_rows;

        while( $row = $query->fetch_assoc() ){
            $name = $row['uname'];
            $longitude = $row['longitude'];                              
            $latitude = $row['latitude'];
            $link=$row['link'];
            /* Each row is added as a new array */
            $locations[]=array( 'name'=>$name, 'lat'=>$latitude, 'lng'=>$longitude, 'lnk'=>$link );
        }


        //echo $locations[0]['name'].": In stock: ".$locations[0]['lat'].", sold: ".$locations[0]['lng'].".<br>";
        //echo $locations[1]['name'].": In stock: ".$locations[1]['lat'].", sold: ".$locations[1]['lng'].".<br>";
    ?>
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=AIzaSyAfFN8NuvYyNkewBVMsk9ZNIcUWDEqHg2U"></script> 
    <script type="text/javascript">
    var map;
    var Markers = {};
    var infowindow;
    var locations = [
        <?php for($i=0;$i<sizeof($locations);$i++){ $j=$i+1;?>
        [
            'AMC Service',
            '<p><a href="<?php echo $locations[0]['lnk'];?>">Book this Person Now</a></p>',
            <?php echo $locations[$i]['lat'];?>,
            <?php echo $locations[$i]['lng'];?>,
            0
        ]<?php if($j!=sizeof($locations))echo ","; }?>
    ];
    var origin = new google.maps.LatLng(locations[0][2], locations[0][3]);

    function initialize() {
      var mapOptions = {
        zoom: 9,
        center: origin
      };

      map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

        infowindow = new google.maps.InfoWindow();

        for(i=0; i<locations.length; i++) {
            var position = new google.maps.LatLng(locations[i][2], locations[i][3]);
            var marker = new google.maps.Marker({
                position: position,
                map: map,
            });
            google.maps.event.addListener(marker, 'click', (function(marker, i) {
                return function() {
                    infowindow.setContent(locations[i][1]);
                    infowindow.setOptions({maxWidth: 200});
                    infowindow.open(map, marker);
                }
            }) (marker, i));
            Markers[locations[i][4]] = marker;
        }

        locate(0);

    }

    function locate(marker_id) {
        var myMarker = Markers[marker_id];
        var markerPosition = myMarker.getPosition();
        map.setCenter(markerPosition);
        google.maps.event.trigger(myMarker, 'click');
    }

    google.maps.event.addDomListener(window, 'load', initialize);

    </script>
    <body id="map-canvas">