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


Fetching the map location from different tables and displaying it on one map with different colours

我的数据库中有超过 2 个具有纬度和经度的表。我必须从所有这些表中获取纬度和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>

这是我的获取位置.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 张表来解决,将来我可能不得不处理很多表。请帮忙!

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