Php 谷歌地图API V3未显示地图
好的,我有一个PHP脚本,它处理文件上传,特别是KML文件,提取上传文件的中心点和位置,将这些细节保存在文本文件中,然后通过“header”加载另一个页面。然后,另一个页面使用AJAX调用来设置从文本文件中收集的变量名,用于KML文件的位置和中心点,以便调用Google Maps API在地图上显示KML层 这是应该发生的事情,但却什么也没有出现。我浏览我的KML文件,点击上传按钮,显示一个空白页面。当我检查我的服务器时,文件被上传,相关细节被写入各自的文本文件中,所以当我尝试调用GoogleMapsAPI时,显然出现了一些问题 PHP上传和文件信息保存:Php 谷歌地图API V3未显示地图,php,html,ajax,google-maps-api-3,kml,Php,Html,Ajax,Google Maps Api 3,Kml,好的,我有一个PHP脚本,它处理文件上传,特别是KML文件,提取上传文件的中心点和位置,将这些细节保存在文本文件中,然后通过“header”加载另一个页面。然后,另一个页面使用AJAX调用来设置从文本文件中收集的变量名,用于KML文件的位置和中心点,以便调用Google Maps API在地图上显示KML层 这是应该发生的事情,但却什么也没有出现。我浏览我的KML文件,点击上传按钮,显示一个空白页面。当我检查我的服务器时,文件被上传,相关细节被写入各自的文本文件中,所以当我尝试调用GoogleM
<?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");
}
?>
infoArea是内联帧的ID
有人知道它为什么不加载吗?我对web开发完全陌生,边学习边学习,如果代码太糟糕,我很抱歉,我肯定我犯了一些明显的错误。非常感谢。
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB8P7CzxiwQFf-RdK9QVRpH9se8AsMSsjEsensor=false"></script>
你在这里漏掉了一个符号。。。应该是:
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=[[YOUR-GMAPS-KEY]]&sensor=false"></script>
(其中,[[YOUR-GMAPS-KEY]]是您的谷歌地图密钥-“AIzaSyB8P7CzxiwQFf-RdK9QV…”)尝试在您的密钥之后和传感器=false参数之前添加一个符号“&” 另外 var kmlCoords=getCoords()
是javascript,但函数getCoords()是PHP。Javascript无法执行PHP函数。错误可能是由于未遵循此格式造成的: src=”https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY“ 请参阅,在“您的API密钥”之后不应出现“sensor=false”。
试试看。希望它能起作用。哦,天哪,这是一个多么愚蠢的错误,至少这一点是一个快速修复。但是仍然没有显示任何内容:-/getCoords也是我的AJAX请求中的一个javascript函数。鉴于PHP和javascript无法相互看到,这有关系吗?我已经将PHP中的getCoords()更改为findCoords(),但仍然没有显示任何内容。我明白了。。。但是javascript函数getCoords()不返回任何内容。尝试添加return kmlCoords。如果您想发布一个指向活动页面的链接,那么我们可以通过适当的调试器运行它。在这方面,我的浏览器比我的眼球要好得多。:-)我有Firebug,它告诉我“谷歌是未定义的”。愤怒不包含有效的KML标记。可能还有其他问题,但这是第一个跳出的问题。请不要破坏您的答案。
onload="initialize()"
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB8P7CzxiwQFf-RdK9QVRpH9se8AsMSsjEsensor=false"></script>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=[[YOUR-GMAPS-KEY]]&sensor=false"></script>