【问题标题】:PHP XML for Google Maps InfoWindow谷歌地图信息窗口的 PHP XML
【发布时间】:2012-08-22 12:06:00
【问题描述】:

我正在尝试从我的 Google 地图中获取以下信息。我知道 JavaScript 是客户端,而不是像 PHP 这样的服务器端,所以它在 J​​S 运行之前就被加载了。问题是如果我要严格通过 PHP -> XML -> JS 将列 post_id 调用到 PHP 中,它不会加载链接。有没有办法只使用 PHP 来做到这一点,并让它从标记表中为地图上的每个位置标记提取位置 post_id?我也读过 AJAX 是可能的,但我对 AJAX 了解不多。

boxText.innerHTML = "<div class='mapname'>" + '<a href="<?php echo get_permalink( $post_id ); ?>">' + name + '</a>' + "</div><i>" + listtype + "</i> <br/>" + address + "<br/>" + phone;

谢谢!

【问题讨论】:

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


    【解决方案1】:

    这是一个完整的 PHP-Javascript AJAX 示例。只需使用名称保存文件 “myServerScript.php”并将浏览器指向它。 PHP 打印通过 AJAX 调用相同 PHP 脚本的 javascript,PHP 响应和 javascript 处理响应。

    确保您的 PHP 安装配置为接受短分隔符 &lt;?...?&gt;

    <?
    
    if(isset($_GET['msg'])){
        print "alert('AJAX response from PHP - Javascript said:". $_GET['msg']."')";
    }
    else{
        print <<<qq
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    
    <title>AJAX Example: (by Marcelo)</title>
    <script src="http://maps.google.com/maps/api/js?sensor=false"></script>
    
    
    <script>
    // Sorry, I can't be bothered typing "google.maps." every time. ;-)
    var G = google.maps;
    
    var zoom = 4;
    var centerPoint = new G.LatLng(40.178873, -96.767578);
    var container;
    var map;
    
    
    function ajaxLoad(url,callback,plain) {
        var http_request = false;
    
        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType && plain) {
                http_request.overrideMimeType('text/plain');
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }
        if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        http_request.onreadystatechange =  function() {
            if (http_request.readyState == 4) {
                if (http_request.status == 200) {
                    eval(callback(http_request));
                }
                else {
                    alert('Request Failed: ' + http_request.status);
                }
            }
        };
        http_request.open('GET', url, true);
        http_request.send(null);
    }
    var url = "myServerScript.php?msg=hello from javascrpt";
    ajaxLoad(url, parseResults, true);
    
    
    function parseResults(req){
        var text = req.responseText;
        eval(text);
    }
    
    function load() {
        container = document.getElementById('mapDiv');
    
        var myOptions = {
            zoom: zoom,
            center: centerPoint,
            mapTypeId: G.MapTypeId.ROADMAP,
            rotateControl: true,
            keyboardShortcuts:false
        }
        map = new G.Map(container, myOptions);
     google.maps.event.addListener(map,'click',function(mev){
    var url = "myServerScript.php?msg="+mev.latLng;
    ajaxLoad(url, parseResults, true);    
      });
    }
    
    window.onload = load;
    
    </script>
    </head>
    <body>
    Hello from PHP.
    <h2>Click on the map to send the lat/lon to PHP</h2>
    <div id="mapDiv" style="width: 800px; height: 450px;"></div>
    </body>
    </html>
    
    qq;
    
    }
    
    
    ?>
    

    Live version here

    【讨论】:

    • 抱歉耽搁了,我还在努力使用它,我会告诉你的!
    猜你喜欢
    • 1970-01-01
    • 2016-07-10
    • 2013-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多