简体   繁体   English

将PHP / MySQL与Google Maps结合使用不起作用

[英]Using PHP/MySQL with Google Maps not working

I saw other posts about this theme but not help me. 我看到了有关该主题的其他帖子,但对我没有帮助。 I use article https://developers.google.com/maps/articles/phpsqlajax_v3 but this code not working I have file that convert data from mysql query to xml 我使用文章https://developers.google.com/maps/articles/phpsqlajax_v3,但是此代码不起作用,我有将数据从mysql查询转换为xml的文件

 function parseToXML($htmlStr)
{
    $xmlStr=str_replace('<','&lt;',$htmlStr);
    $xmlStr=str_replace('>','&gt;',$xmlStr);
    $xmlStr=str_replace('"','&quot;',$xmlStr);
    $xmlStr=str_replace("'",'&#39;',$xmlStr);
    $xmlStr=str_replace("&",'&amp;',$xmlStr);
    return $xmlStr;
}

$contragent = db::rows('SELECT nameid, status, coord_lat, coord_lng FROM sp_agent'); // this raw return value in array



for($item = 0; $item < count($contragent); $item ++){
    echo '<contragent ';
    echo 'nameid="' . parseToXML($contragent[$item]['nameid']) . '" ';
    echo 'status="' . parseToXML($contragent[$item]['status']) . '" ';
    echo 'lat="' . $contragent[$item]['coord_lat'] . '" ';
    echo 'lng="' . $contragent[$item]['coord_lng'] . '" ';
    echo '/>';
}

javascript code: JavaScript代码:

<script type="text/javascript">
        //<![CDATA[

        var customIcons = {
            restaurant: {
                icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png'
            },
            bar: {
                icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png'
            }
        };
        var map;
        function load() {
            map = new google.maps.Map(document.getElementById("map"), {
                center: new google.maps.LatLng(41.556616, 74.546518),
                zoom: 13,
                mapTypeId: 'roadmap'
            });
            var infoWindow = new google.maps.InfoWindow;

            // Change this depending on the name of your PHP file
            downloadUrl("phpsqlajax_genxml.php", function(data) {
                var xml = data.responseXML;
                var markers = xml.documentElement.getElementsByTagName("contragent");
                for (var i = 0; i < markers.length; i++) {
                    var name = markers[i].getAttribute("name");
                    var address = markers[i].getAttribute("address");
                    var type = markers[i].getAttribute("type");
                    var point = new google.maps.LatLng(
                        parseFloat(markers[i].getAttribute("lat")),
                        parseFloat(markers[i].getAttribute("lng")));
                    var html = "<b>" + name + "</b> <br/>" + address;
                    var icon = customIcons[type] || {};
                    var marker = new google.maps.Marker({
                        map: map,
                        position: point,
                        icon: icon.icon
                    });
                    bindInfoWindow(marker, map, infoWindow, html);
                }
            });
        }

        function bindInfoWindow(marker, map, infoWindow, html) {
            google.maps.event.addListener(marker, 'click', function() {
                infoWindow.setContent(html);
                infoWindow.open(map, marker);
            });
        }

        function downloadUrl(url, callback) {
            var request = window.ActiveXObject ?
                new ActiveXObject('Microsoft.XMLHTTP') :
                new XMLHttpRequest;

            request.onreadystatechange = function() {
                if (request.readyState == 4) {
                    request.onreadystatechange = doNothing;
                    callback(request, request.status);
                }
            };

            request.open('GET', url, true);
            request.send(null);
        }

        function doNothing() {}

        google.maps.event.addListener(marker, 'click', function() {
            infowindow.open(map,marker);
        });

    </script>

in result: i saw only map without markers. 结果是:我只看到没有标记的地图。 Why ? 为什么呢 I don't know.. Help me please 我不知道..请帮帮我

<?php
    $result = db::rows('SELECT nameid, status, coord_lat, coord_lng FROM sp_agent');
    $json = json_encode($result);
?>

<script>
    var map;
    var locations = <?=$json?>;
    console.log('helo 1 = '+locations[0]['coord_lat']);
    console.log('helo 2 = '+locations[0]['coord_lng']);
    function initialize() {
        var mapOptions = {
            zoom: 8,
            center: new google.maps.LatLng(42.862101,74.610686)
        };
        map = new google.maps.Map(document.getElementById('map-canvas'),mapOptions);

        addMarker(42.862101,74.610684);
    }

    google.maps.event.addDomListener(window, 'load', initialize);
    function addMarker(x,y){
        var myLatlng = new google.maps.LatLng(x,y);
        var mapOptions = {
            zoom: 4,
            center: myLatlng
        }
        var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
        var contentString = '<div id="content">'+
            '<div id="siteNotice">'+
            '</div>'+
            '<h1 id="firstHeading" class="firstHeading">Uluru</h1>'+
            '<div id="bodyContent">'+
            '<p><b>Uluru</b>, also referred to as <b>Ayers Rock</b>, is a large ' +
            'sandstone rock formation in the southern part of the '+
            'Northern Territory, central Australia. It lies 335&#160;km (208&#160;mi) '+
            'south west of the nearest large town, Alice Springs; 450&#160;km '+
            '(280&#160;mi) by road. Kata Tjuta and Uluru are the two major '+
            'features of the Uluru - Kata Tjuta National Park. Uluru is '+
            'sacred to the Pitjantjatjara and Yankunytjatjara, the '+
            'Aboriginal people of the area. It has many springs, waterholes, '+
            'rock caves and ancient paintings. Uluru is listed as a World '+
            'Heritage Site.</p>'+
            '<p>Attribution: Uluru, <a href="http://en.wikipedia.org/w/index.php?title=Uluru&oldid=297882194">'+
            'http://en.wikipedia.org/w/index.php?title=Uluru</a> '+
            '(last visited June 22, 2009).</p>'+
            '</div>'+
            '</div>';
        var infowindow = new google.maps.InfoWindow({
            content: contentString
        });

        var marker;
        for (i = 0; i < locations.length; i++) {
            marker = new google.maps.Marker({
                position: new google.maps.LatLng(locations[i]['coord_lat'], locations[i]['coord_lng']),
                map: map
            });

            google.maps.event.addListener(marker, 'click', (function(marker, i) {
                return function() {
                    infowindow.setContent(locations[i]['nameid']);
                    infowindow.open(map, marker);
                }
            })(marker, i));
        }
    }
</script>

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

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