【问题标题】:retrieve value from mysql database to be used as value in javascript从 mysql 数据库中检索值以用作 javascript 中的值
【发布时间】:2015-11-21 19:44:30
【问题描述】:

我正在制作地图,我想使用数据库中的值作为 javascript 中的坐标。请看下面:

我想通过从数据库中获取数据来更改经度和纬度的默认值,但我不知道如何。

    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <title>Google Maps</title>
    <script src="http://maps.google.com/maps/api/js?sensor=false"     t      type="text/javascript"></script>
    <script type="text/javascript">
    function LatiPoint(options)
    {
        this.UserName = "";
        this.JobType = "";
        this.UserMood = "";
        this.Latitude = "";
        this.Longitude = "";
        this.Pic = "";
        this.Color = "red";
        this.OpenInfoWindowInitial = false;

        if (options != null)
        {
            this.UserName = options.UserName;
            this.JobType = options.JobType;
            this.UserMood = options.UserMood;
            this.Latitude = options.Latitude;
            this.Longitude = options.Longitude;
            this.Pic = options.Pic;
            this.Color = ( options.Color == null ? "red" : options.Color );
            this.OpenInfoWindowInitial = ( options.OpenInfoWindowInitial == null   ? false : options.OpenInfoWindowInitial );
        }
    }
    </script>

    <script type="text/javascript">
        var LatiPts = new Array();
        LatiPts[0] = new LatiPoint({UserName:'Dukot',JobType:'Balulang Cagayan     De Oro',UserMood:'drinking beer',Latitude:8.4542363,Longitude:124.63189769999997,Color:'yellow',OpenInfoWindowInitial:true,Pic:''});
        LatiPts[1] = new LatiPoint({UserName:'Alot',JobType:'Cagayan De Oro',UserMood:'with classmates',Latitude:8.458353831903118,Longitude:124.63627706511227,Color:'yellow',OpenInfoWindowInitial:true,});
        LatiPts[2] = new LatiPoint({UserName:'Lindongan',JobType:'SM Cagayan De Oro',UserMood:'feeling bored',Latitude:8.456188949479728,Longitude:124.62177167875973,Color:'yellow',OpenInfoWindowInitial:true,});
        LatiPts[3] = new LatiPoint({UserName:'Galal',JobType:'Carmen Cagayan De Oro',UserMood:'beer',Latitude:8.467607505884205,Longitude:124.62271581633297,Color:'yellow',OpenInfoWindowInitial:true,});
        LatiPts[4] = new LatiPoint({UserName:'Belen',JobType:'Cagayan De Oro',UserMood:'beer',Latitude:8.46332028090713,Longitude:124.63537584288326,Color:'yellow',OpenInfoWindowInitial:true,});


    </script>

    <script type="text/javascript">
    //<![CDATA[
        var infoWindows = [];
        var markers = [];

        // A function to create the marker and set up the event window
        function createMarker(map, point, title, icon) {
            var marker = new google.maps.Marker();
            marker.setMap(map);
            marker.setPosition(point);
            marker.setTitle(title);
            marker.setIcon(icon)

            google.maps.event.addListener(marker, 'click', function() {
                for (var i = 0; i < infoWindows.length; i++)
                    infoWindows[i].setZIndex(0);

                infoWindows[marker.counter].setZIndex(1);
                infoWindows[marker.counter].open(marker.getMap(), marker);
            });            

            return marker;
        }

        function createInfoWindow(html)
        {
            var infoWindow = new google.maps.InfoWindow();
            infoWindow.setContent(html);
            infoWindow.setZIndex(0);        

            return infoWindow;
        }

        function initialize() {

            // Create the Google Map
            var map = new google.maps.Map(document.getElementById("map"));
            map.setMapTypeId(google.maps.MapTypeId.ROADMAP);
            map.setCenter(new google.maps.LatLng(0,0));

            var bounds = new google.maps.LatLngBounds();

            for (var i = 0; i < LatiPts.length; i++) {
                var lpt = LatiPts[i];
                var point = new google.maps.LatLng(lpt.Latitude, lpt.Longitude);
                var html = "<span style='font-family:Arial;font-size:9pt'>";

                html += "<span style='font-size:12pt;font-weight:bold'>" + lpt.UserName + "</span><br />";
                html += lpt.UserMood.substring(0, 30) + "<br/>";
                html += lpt.JobType.substring(0, 30) + "<br/>";


                html += "</span>";

                var imgPath
                if(lpt.Pic != "") {
                    imgPath = lpt.Pic
                } else {
                    imgPath = "http://maps.gstatic.com/intl/en_ALL/mapfiles/marker.png"
                }
                //alert("imgPath = " + imgPath + " Pic: " + lpt.Pic)
                var icon = new google.maps.MarkerImage(imgPath);
                //icon.shadowSize = GSize.ZERO;

                var infoWindow = createInfoWindow(html);
                infoWindow.open();

                var marker = createMarker(map, point, lpt.UserName, icon);
                marker.setZIndex(0);
                marker.counter = i;


            if (LatiPts[i].OpenInfoWindowInitial)
                {
                    infoWindow.open(marker.getMap(), marker);
                }
                infoWindows.push(infoWindow);

                markers.push(marker);


                bounds.extend(point);

                            }

            map.setCenter(bounds.getCenter());
            map.fitBounds(bounds);

        }

        function handleNoFlash(code)
        {
            if ( code == GStreetviewPanorama.ErrorValues.FLASH_UNAVAILABLE )
                alert( 'You need flash player to view the panorama.' );
        }

        function convertLatLngToString(latlng)
        {
            var hour = parseInt(latlng)
            var min = parseInt((latlng - hour) * 60)
            var second = (((latlng - hour) * 60) - min) * 60

            return (hour + "&deg; " + min + "&#39; " + second.toFixed(2) + "&#34; ");
        }

    //]]>
    </script>    

    <style type="text/css">
        /*give the body height:100% so that its child
        elements can have percentage heights*/

        body{ margin:0;padding:0;height:100% }

        div.fullscreen{
            display:block;


            position:absolute;
            top:0;
            left:0;


            width:100%;
            height:100%;
            z-index: 9999;
            margin: 0;
            padding: 0;
            background: inherit;
        }

    </style>
</head>
<body onload="initialize()">
    <div id="map" class="fullscreen" style="width:100%; height:100%"></div>
    <div id="streetview" style="width: 650px; height: 400px; display: none;"></div>
    <noscript>
        <b>JavaScript must be enabled in order for you to use Google Maps.</b> 
    </noscript>

</body>
</html>

【问题讨论】:

  • 请出示您的 PHP 代码!
  • 您说您正在制作地图,但代码仅显示了一个功能。如果你能用完整的代码完成问题,帮助你很容易。
  • 我试图发布所有代码,但它不允许我这样做。它说错误。无论如何,以扩大我的问题。我想自定义纬度和经度的值,您可以在代码的最后两行看到。那里的值是我设置的默认值。我想要的是从数据库中获取一个值。应获取数据库中的任何其他输入,并应进行循环以创建上面类似代码的新行(最后两行)。我希望我说得通:)谢谢!
  • 不要发布全部您的代码。请提供一个 Minimal, Complete, Tested and Readable example 来证明您正在尝试做的事情。
  • 给你,我粘贴了整个代码。我希望现在很清楚。我需要你们的帮助 :) 和平!

标签: javascript php mysql database google-maps


【解决方案1】:

你可以这样做:

this.UserName = ""; 希望能帮助到你 :)

【讨论】:

  • 谢谢法鲁克。我会试试这个。 :)
  • 但是如果我使用与 javascript 相同的页面从我的数据库中获取。人们可以看到我的登录详细信息吗?请问我该如何解决这个问题?谢谢!
  • 如果你想在JS中使用一些东西,它对用户是可见的,所以不要乱用JS中有价值的东西。不,它对用户不可见,它将在页面加载时在服务器上解析。用户只会看到 this.UserName = "User007";
  • 好的。您能否向我展示有关如何从我的表中获取数据的整个代码。如果我有多个用户名值怎么办?我想要的结果是使用我数据库中的值在地图上创建一个标记。所以预计会有多个标记:)请帮助我:)
  • 阅读:wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers 多个用户名由 while 或 for 循环处理返回的数据行。因此,您将它们一一拿到并随心所欲地处理它们:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-02
  • 1970-01-01
相关资源
最近更新 更多