简体   繁体   English

Google Maps API V3未显示地图

[英]Google Maps API V3 not showing map

Okay so I have a PHP script which handles file uploads, KML files specifically, extracts the center point and the location of the uploaded file, saves these details in text files, and then loads another page via "header". 好的,我有一个PHP脚本来处理文件上传,特别是KML文件,提取文件的中心和位置,将这些详细信息保存在文本文件中,然后通过“页眉”加载另一个页面。 This other page then uses AJAX calls to set variable names as gathered from the text files, for the location of the KML file and the center point, in order to call the Google Maps API to show the KML layer upon a map. 然后,此另一页使用AJAX调用来设置从文本文件收集的变量名称,以获取KML文件的位置和中心点,以便调用Google Maps API在地图上显示KML图层。

That is what is supposed to happen, but instead nothing appears. 那是应该发生的,但是什么也没有出现。 I browse for my KML file, hit the upload button, and a blank page is shown. 我浏览我的KML文件,点击上传按钮,然后显示空白页面。 When I check my server, the file as been uploaded and the relevant details written into their respective text files, so obviously something is going wrong when I try to call the Google Maps API. 当我检查服务器时,已上传的文件以及相关详细信息已写入各自的文本文件中,因此当我尝试调用Google Maps API时显然出了问题。

PHP upload and file info saving: PHP上传和文件信息保存:

<?php
//Check that we have a file
if((!empty($_FILES["UploadedFile"])) && ($_FILES['UploadedFile']['error'] == 0)) {
  //Check if the file is JPEG image and it's size is less than 3Mb
  $filename = basename($_FILES['UploadedFile']['name']);
  $ext = substr($filename, strrpos($filename, '.') + 1);
  if (($ext == "kml") && ($_FILES["UploadedFile"]["size"] < 3145728)) {
    //Determine the path to which we want to save this file
      $newname = dirname(__FILE__).'/kml/'.$filename;
      //Check if the file with the same name is already exists on the server
      if (!file_exists($newname)) {
        //Attempt to move the uploaded file to it's new place
        if ((move_uploaded_file($_FILES['UploadedFile']['tmp_name'],$newname))) {
           findCenter($newname);
           saveName($newname);
        } else {
           echo "Error: A problem occurred during file upload!";
        }
      } else {
         echo "Error: File ".$_FILES["UploadedFile"]["name"]." already exists";
      }
  } else {
     echo "Error: Only .kml files under 3Mb are accepted for upload";
  }
} else {
 echo "Error: No file uploaded";
}

function findCenter($file) {
   $kmlContent = file_get_contents($file);

   $startName = "#sh_green-circle";
   $endName = "#sh_red-circle";
   $startCoords = getCoords($kmlContent, $startName);
   $endCoords = getCoords($kmlContent, $endName);

   $startLat = substr($startCoords, strrpos($startCoords, ',') +1);
   $startLong = substr($startCoords, 0, strrpos($startCoords, ','));

   $endLat = substr($endCoords, strrpos($endCoords, ',') +1);
   $endLong = substr($endCoords, 0, strrpos($endCoords, ','));

   $midLat = ($startLat+$endLat)/2;
   $midLong = ($startLong+$endLong)/2;

   $midCoord = "$midLat,$midLong";

   $saveCenter = "kmlcenter.txt";
   $fh = fopen($saveCenter, 'w') or die ("Can't create file");
   $stringData = $midCoord;
   fwrite($fh, $stringData);
   fclose($fh);
}

function getCoords($kml, $name) {
   $startSearch = strpos($kml, $name);
   $midSearch = strpos($kml, "<coordinates>", $startSearch+1);
   $endSearch = strpos($kml, "</coordinates>", $midSearch+1);

   $longlat = substr($kml, $midSearch+13, $endSearch-($midSearch+13));
   return $longlat;
}

function saveName($filename) {
   $saveFile = "kmlfilename.txt";
   $fh = fopen($saveFile, 'w') or die("Can't create file");
   $stringData = "$filename";
   fwrite($fh, $stringData);
   fclose($fh);
   header("Location: initgmaps.html");
}
?>

Map intitialisation: 地图初始化:

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB8P7CzxiwQFf-RdK9QVRpH9se8AsMSsjEsensor=false"></script>
<script type="text/javascript">
function initialize() {
  var kmlName = getName()
  var kmlCoords = getCoords()
  var mapcenter = new google.maps.LatLng(kmlCoords);
  var myOptions = {
    zoom: 11,
    center: mapcenter,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }

  var map = new google.maps.Map(document.getElementById("infoArea"), myOptions);

  var kmlLayer = new google.maps.KmlLayer(kmlName);
  kmlLayer.setMap(map);
}
</script>

Relevant AJAX functions: 相关的AJAX功能:

<script language="javascript" type="text/javascript">
<!-- 
//Browser Support Code
function getName(){
    var ajaxRequest;  // The variable that makes Ajax possible!
    var kmlName;
    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Your browser does not support AJAX.");
                return false;
            }
        }
    }
       // Create a function that will receive data sent from the server
       ajaxRequest.onreadystatechange = function(){
            if(ajaxRequest.readyState == 4 && ajaxRequest.status == 200){
               kmlName = ajaxRequest.responseText;   
               }
       }
       ajaxRequest.open("GET", "kmlfilename.txt", false);
       ajaxRequest.send(null);
}
//-->

<script language="javascript" type="text/javascript">
<!-- 
//Browser Support Code
function getCoords(){
    var ajaxRequest;  // The variable that makes Ajax possible!
    var kmlCoords;
    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Your browser does not support AJAX.");
                return false;
            }
        }
    }
       // Create a function that will receive data sent from the server
       ajaxRequest.onreadystatechange = function(){
            if(ajaxRequest.readyState == 4 && ajaxRequest.status == 200){
               kmlCoords = ajaxRequest.responseText;   
               }
       }
       ajaxRequest.open("GET", "kmlcenter.txt", false);
       ajaxRequest.send(null);
}
//-->

And of course in the body of initgmaps.html I have: 当然,在initgmaps.html的正文中,我有:

onload="initialize()"

infoArea is the ID for my inline frame. infoArea是我的嵌入式框架的ID。

Can anyone see why it doesn't load? 谁能看到为什么它不加载? I'm completely and utterly new to web development, learning as I go, so sorry if the code is terrible and I'm sure I've made some obvious errors. 我对Web开发完全陌生,正在学习,对不起,如果代码太糟糕了,我敢肯定我犯了一些明显的错误。 Many thanks. 非常感谢。

<script type="text/javascript"
  src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB8P7CzxiwQFf-RdK9QVRpH9se8AsMSsjEsensor=false"></script>

You have missed an ampersand here... Should be: 您在这里错过了&号...应该是:

<script type="text/javascript"
  src="https://maps.googleapis.com/maps/api/js?key=[[YOUR-GMAPS-KEY]]&sensor=false"></script>

(Where [[YOUR-GMAPS-KEY]] is your Google Maps Key - " AIzaSyB8P7CzxiwQFf-RdK9QV... ".) [[YOUR-GMAPS-KEY]]是您的Google Maps密钥-“ AIzaSyB8P7CzxiwQFf-RdK9QV ... ”。)

Try adding an ampersand '&' after your key and before the sensor=false parameter. 尝试在密钥之后和sensor = false参数之前添加与号'&'。

Additionally 另外

var kmlCoords = getCoords() var kmlCoords = getCoords()

is javascript, but function getCoords() is PHP. 是javascript,但函数getCoords()是PHP。 Javascript cannot execute a PHP function. Javascript无法执行PHP函数。

the error might be caused by not following this format: 该错误可能是由于不遵循以下格式引起的:

src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY" src =“ https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY”

see, after "YOUR_API_KEY" there shouldn't be "sensor=false". 看到,在“ YOUR_API_KEY”之后不应有“ sensor = false”。 try it. 试试吧。 hope it works. 希望它能工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM