从不同的表中获取地图位置并将其显示在具有不同颜色的一个地图上

时间:2015-01-29 05:16:29

标签: php

我的数据库中有超过2个表具有纬度和经度。我必须从所有这些表中获取lat和lon并在我的地图上找到它。 例如:表A显示红色的位置,     表B显示蓝色等位置....

这是我的地图脚本代码:

  <script type="text/javascript">
            var map;

            var center = new google.maps.LatLng(12.901164615652498, 75.22423071289063);
             var geocoder = new google.maps.Geocoder();
             var infowindow = new google.maps.InfoWindow();
             function init() {

                var mapOptions = {
                    zoom: 10,
                    center: center,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                }

                map = new google.maps.Map(document.getElementById("inner"), mapOptions);
                makeRequest('getLocation.php', function(data) {

            var data = JSON.parse(data.responseText);

            for (var i = 0; i < data.length; i++) {
                displayLocation(data[i]);
            }
        });

           }
        function displayLocation(location) {


        var content =   '<div class="infoWindow"><strong>'  + location.lat + '</strong>'
                        + '<br/>'     + location.lon
                         '</div>';

        if (parseInt(location.lat) == 0) {
            geocoder.geocode( { 'address': location.address }, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {

                    var marker = new google.maps.Marker({
                        map: map, 
                        position: results[0].geometry.location,

                        title: location.name
                    });

                    google.maps.event.addListener(marker, 'click', function() {
                        infowindow.setContent(content);
                        infowindow.open(map,marker);
                    });
                }
            });
        } else {
            var position = new google.maps.LatLng(parseFloat(location.lat), parseFloat(location.lon));
            var pinColor = "FE7569";
            var icon = {
        url: "http://www.google.com/intl/en_us/mapfiles/ms/micons/red-dot.png", //url
        scaledSize: new google.maps.Size(15, 15), //scaled size
        origin: new google.maps.Point(0,0), //origin
        anchor: new google.maps.Point(0, 0) //anchor
    };

            var marker = new google.maps.Marker({

                map: map, 
                position: position,
                 icon: icon,
                 title: location.name
            });

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

            function makeRequest(url, callback) {
        var request;
        if (window.XMLHttpRequest) {
            request = new XMLHttpRequest(); // IE7+, Firefox, Chrome, Opera, Safari
        } else {
            request = new ActiveXObject("Microsoft.XMLHTTP"); // IE6, IE5
        }
        request.onreadystatechange = function() {
            if (request.readyState == 4 && request.status == 200) {
                callback(request);
            }
        }
        request.open("GET", url, true);
        request.send();
    }

            </script>

这是我的getLocation.php:我加入了2个表,但我无法在地图上显示它。它在地图上只提供了1个表格位置。

try {

        $db = new PDO($dsn, $username, $password);
        $db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

         $sth = $db->query("SELECT incident_main.inc_GISlan as lat,incident_main.inc_GISlon as lon,source_main.source_GISlan as lat1,source_main.source_GISlon as lon1
FROM incident_main, source_main
WHERE incident_main.inc_id = source_main.source_id");

        $locations = $sth->fetchAll();

        echo json_encode( $locations );

    } catch (Exception $e) {
        echo $e->getMessage();
    }

请帮我解决这个问题。至于现在我试图解决2个表,将来我可能要处理很多表。 Plz帮忙!

1 个答案:

答案 0 :(得分:0)

你可以将init()函数编写为init1()两次,并为第二个表创建一个单独的文件来获取位置并调用init1()中的dat文件并给出不同的引脚颜色。并在任何需要的地方调用这两个函数。我认为这样可行。