代码之家  ›  专栏  ›  技术社区  ›  Tormy Van Cool

谷歌地图API:静态地图返回错误,没有可用地图

  •  0
  • Tormy Van Cool  · 技术社区  · 8 年前

    我在谷歌上开了一个账户 https://console.developers.google.com/cloud-resource-manager 创建一个使用Google Maps静态API的键。

    我在AMP页面上使用了该键,但在AMP页面上效果很好,在普通HTML页面上,Google的API返回了一个错误。

    使用Chrome的开发工具进行调查,它返回MissingKeyMapError,这是没有意义的,因为同一个密钥正在该站点的AMP版本上运行。

    我调试了这个网页(由PHP生成),我看到:所有地理数据,都是由CURL等从Google正确检索到的。。。

    只是Google的Javascript没有返回地图。。。

    我附上了Chrome JS控制台的错误代码截图: enter image description here

    下面是包含my Key的变量(写入setup.php文件)

    $key\U gigs=“\\UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUMKEY \\UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU”;

    这里我使用的类用于AMP以及普通HTML页面:

    <?php
    class geo {
    
        public function CoOrdinates($address_) {
    
        global $key_gigs; // Setup.php
        //echo $address_; echo $key_gigs; exit; // debug purpose
        $curl_ = curl_init();
        $url_map = "https://maps.googleapis.com/maps/api/geocode/json?address=$address_&sensor=false&key=$key_gigs";
        curl_setopt($curl_, CURLOPT_URL, $url_map);
        curl_setopt($curl_, CURLOPT_HEADER, false);
        curl_setopt($curl_, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($curl_, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl_, CURLOPT_ENCODING, "");
        $curlData = curl_exec($curl_);
        curl_close($curl_);
    
        $data = json_decode($curlData, true);
        //print_r($data);   exit; // debug purpose
        $ret_ = [
            'city' => $data["results"][0]["address_components"][2]["long_name"],
            'country' => $data["results"][0]["address_components"][5]["long_name"],
            'iso' => strtolower($data["results"][0]["address_components"][5]["short_name"]),
    
            'lat' => $data["results"][0]["geometry"]["location"]["lat"],
            'lon' => $data["results"][0]["geometry"]["location"]["lng"],
    
            'time-zone' => $this->TimeZone($data["results"][0]["geometry"]["location"]["lat"],$data["results"][0]["geometry"]["location"]["lng"]),
        ];
        //print_r($ret_); exit; // debug purpose
        return $ret_ ;
        }
    
        public function TimeZone($lat_,$lon_) {
            //echo $lat_." ".$lon_; exit;
    
            $query_json = "https://maps.googleapis.com/maps/api/timezone/json?location={$lat_},{$lon_}&timestamp=0&sensor=false";
            $json_timezone = file_get_contents($query_json);
            $data_time_zone = json_decode($json_timezone, true);
            return $time_zone = $data_time_zone["timeZoneId"];
        }
    }
    ?>
    

    这里是生成页面的PHP代码中感兴趣的部分(位于页面的预处理器中)

    $address = add_plus($Address.", ".$Number.",".$Code."+".$City.",".$Country);
    
    // Creates and manages object from Class geo();
    $geo = new geo();
    $coordinates = $geo->CoOrdinates($address);
    $city = $coordinates['city'];
    $country = $coordinates['country'];
    $iso = $coordinates['iso'];
    $lat = $coordinates['lat'];
    $lon = $coordinates['lon'];
    $time_zone = $coordinates['time-zone'];
    // GEO Debug
    //echo $city." ".$country." ".$iso." ".$lat." ".$lon." ".$time_zone; exit;
    

    这里是Google JS的元标签。以下代码行位于页面本身中:

    <script type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=false"></script>
    

    这里是可视化代码。$VICENT等变量由页面的预处理器生成,数据取自dB

        <!-- Map style="width: 60vw; height: 33.8vw;" -->
        <section>
                <h2 class="h_special"><? echo $Name; ?> - EVENTO</h2>
                <div id="gmap" style=""></div>
                <script type="text/javascript">
            var myLatLng = {lat: <? echo $lat; ?>, lng: <? echo $lon; ?>};
            var myOptions = {
                zoom: <? echo $zoom; ?>,
                center: new google.maps.LatLng(<? echo $lat; ?>, <? echo $lon; ?>),
                // mapTypeId: google.maps.MapTypeId.ROADMAP
                // mapTypeId: google.maps.MapTypeId.SATELLITE
                mapTypeId: google.maps.MapTypeId.HYBRID
                // mapTypeId: google.maps.MapTypeId.TERRAIN
            };
            var map = new google.maps.Map(document.getElementById("gmap"), myOptions);
            var marker = new google.maps.Marker({position: myLatLng, map: map, title: '<? echo $venue; ?>'});
                </script>
        </section>
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   Preston    8 年前

    作为 Javascript API error code documentation 正如您的屏幕截图所示,之所以会出现MissingKeyMapError,是因为您的Javascript API加载URL中缺少API键,而与 Static Maps API :

    <script type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=false"></script>
    

    您需要向该URL添加一个API密钥,作为 Getting Started documentation 说。你也 do not need the sensor parameter :

    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=YOUR_KEY"></script>
    
    推荐文章