doumindang2416 2016-03-16 10:53
浏览 41

使用json从数据库检索纬度经度到php页面

am trying to plot latitude and longitude from database(mysql) to php page. so far i been able to display the marker without json with the code:

<?
$dbname            ='insert mysql database name'; //Name of the database
$dbuser            ='insert mysql user name'; //Username for the db
$dbpass            ='insert mysql password'; //Password for the db
$dbserver          ='insert mysql database server address'; //Name of the mysql server
 
$dbcnx = mysql_connect ("$dbserver", "$dbuser", "$dbpass");
mysql_select_db("$dbname") or die(mysql_error());
?>
<html>
 <head>
 <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
 <title>Google Map API V3 with markers</title>
 <style type="text/css">
 body { font: normal 10pt Helvetica, Arial; }
 #map { width: 350px; height: 300px; border: 0px; padding: 0px; }
 </style>
 <script src="http://maps.google.com/maps/api/js?v=3&sensor=false" type="text/javascript"></script>
 <script type="text/javascript">
 //Sample code written by August Li
 var icon = new google.maps.MarkerImage("http://maps.google.com/mapfiles/ms/micons/blue.png",
 new google.maps.Size(32, 32), new google.maps.Point(0, 0),
 new google.maps.Point(16, 32));
 var center = null;
 var map = null;
 var currentPopup;
 var bounds = new google.maps.LatLngBounds();
 function addMarker(lat, lng, info) {
 var pt = new google.maps.LatLng(lat, lng);
 bounds.extend(pt);
 var marker = new google.maps.Marker({
 position: pt,
 icon: icon,
 map: map
 });
 var popup = new google.maps.InfoWindow({
 content: info,
 maxWidth: 300
 });
 google.maps.event.addListener(marker, "click", function() {
 if (currentPopup != null) {
 currentPopup.close();
 currentPopup = null;
 }
 popup.open(map, marker);
 currentPopup = popup;
 });
 google.maps.event.addListener(popup, "closeclick", function() {
 map.panTo(center);
 currentPopup = null;
 });
 }
 function initMap() {
 map = new google.maps.Map(document.getElementById("map"), {
 center: new google.maps.LatLng(0, 0),
 zoom: 14,
 mapTypeId: google.maps.MapTypeId.ROADMAP,
 mapTypeControl: false,
 mapTypeControlOptions: {
 style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR
 },
 navigationControl: true,
 navigationControlOptions: {
 style: google.maps.NavigationControlStyle.SMALL
 }
 });
 <?
 $query = mysql_query("SELECT * FROM poi_example");
 while ($row = mysql_fetch_array($query)){
 $name=$row['name'];
 $lat=$row['lat'];
 $lon=$row['lon'];
 $desc=$row['desc'];
 echo ("addMarker($lat, $lon,'<b>$name</b><br/>$desc');
");
 }
 ?>
 center = bounds.getCenter();
 map.fitBounds(bounds);
 
 }
 </script>
 </head>
 <body onload="initMap()" style="margin:0px; border:0px; padding:0px;">
 <div id="map"></div>
 </html>

Thus i tried with json but its not displaying anything below is the code i tried:

<?
$dbname            ='insert mysql database name'; //Name of the database
$dbuser            ='insert mysql user name'; //Username for the db
$dbpass            ='insert mysql password'; //Password for the db
$dbserver          ='insert mysql database server address'; //Name of the mysql server

$dbcnx = mysql_connect ("$dbserver", "$dbuser", "$dbpass");
mysql_select_db("$dbname") or die(mysql_error());
?>
<html>
 <head>
 <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
 <title>Google Map API V3 with markers</title>
 <style type="text/css">
 body { font: normal 10pt Helvetica, Arial; }
 #map { width: 350px; height: 300px; border: 0px; padding: 0px; }
 </style>
 <script src="http://maps.google.com/maps/api/js?v=3&sensor=false" type="text/javascript"></script>
 <script type="text/javascript">
 //Sample code written by August Li
 var icon = new google.maps.MarkerImage("http://maps.google.com/mapfiles/ms/micons/blue.png",
 new google.maps.Size(32, 32), new google.maps.Point(0, 0),
 new google.maps.Point(16, 32));
 var center = null;
 var map = null;
 var currentPopup;
 var bounds = new google.maps.LatLngBounds();
 function addMarker(lat, lng, info) {
 var pt = new google.maps.LatLng(lat, lng);
 bounds.extend(pt);
 var marker = new google.maps.Marker({
 position: pt,
 icon: icon,
 map: map
 });
 var popup = new google.maps.InfoWindow({
 content: info,
 maxWidth: 300
 });
 google.maps.event.addListener(marker, "click", function() {
 if (currentPopup != null) {
 currentPopup.close();
 currentPopup = null;
 }
 popup.open(map, marker);
 currentPopup = popup;
 });
 google.maps.event.addListener(popup, "closeclick", function() {
 map.panTo(center);
 currentPopup = null;
 });
 }
 function initMap() {
 map = new google.maps.Map(document.getElementById("map"), {
 center: new google.maps.LatLng(0, 0),
 zoom: 14,
 mapTypeId: google.maps.MapTypeId.ROADMAP,
 mapTypeControl: false,
 mapTypeControlOptions: {
 style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR
 },
 navigationControl: true,
 navigationControlOptions: {
 style: google.maps.NavigationControlStyle.SMALL
 }
 });
 

 <?
 $query = mysql_query("SELECT * FROM poi_example");
 while ($row = mysql_fetch_array($query)){
 $name=$row['name'];
 $lat=$row['lat'];
 $lon=$row['lon'];
 $desc=$row['desc'];
$latlong = $lat.", ". $lon;
array_push($new, $latlong);
 //echo ("addMarker($lat, $lon,'<b>$name</b><br/>$desc');
");
 }
   echo json_encode($new);

 ?>

 var arr = <?php echo json_encode($new) ?>;
for(var i=0; i<arr.length;i++)
{
    var lat = Number(arr[i].split(",")[0].trim());
    var lon = Number(arr[i].split(",")[0].trim());
    var four = new google.maps.LatLng(lat,lon);
    var marker=new google.maps.Marker({
                position:four,
                map:map
            });
}
 center = bounds.getCenter();
 map.fitBounds(bounds);

 }
 </script>
 </head>
 <body onload="initMap()" style="margin:0px; border:0px; padding:0px;">
 <div id="map"></div>
 </html>

</div>
  • 写回答

1条回答 默认 最新

  • dongxichan8627 2016-03-16 11:36
    关注

    I strongly believe you should separate your logic on both ends.

    Your PHP Logic. Gets to work on its own, while you have your Frontend Logic JS working on its own. This way it's easier to solve and sort out what the problem is.

    Below is the solution i think you should look at. I do not have your database to play with. Hope you understand what i propose you should do to get it working.

    STEP 1

    PHP CODE - PHP File - yourphpfile.php

    <?php
    $dbname            ='u769748933_tr'; //Name of the database
    $dbuser            ='u769748933_ta'; //Username for the db
    $dbpass            ='adamas'; //Password for the db
    $dbserver          ='mysql.1freehosting.com'; //Name of the mysql server
    
    $dbcnx = mysql_connect ("$dbserver", "$dbuser", "$dbpass");
    mysql_select_db("$dbname") or die(mysql_error());
    
     $q = mysql_("SELECT * FROM poi_example");
     while ($row = mysql_fetch_array($q)){
     $name=$row['name'];
     $lat=$row['lat'];
     $lon=$row['lon'];
     $desc=$row['desc'];
    $latlong = $lat.", ". $lon;
    array_push($new, $latlong);
    
     }
    
       echo $new; // raw data, not json encoded
    
    
    ?>
    

    The above code should show your JSON encoded data when you point to it on your browser.

    STEP 2

    Your Javascript File(s)

    mymap.js file

         //Sample code written by August Li
     var icon = new google.maps.MarkerImage("http://maps.google.com/mapfiles/ms/micons/blue.png",
     new google.maps.Size(32, 32), new google.maps.Point(0, 0),
     new google.maps.Point(16, 32));
     var center = null;
     var map = null;
     var currentPopup;
     var bounds = new google.maps.LatLngBounds();
     function addMarker(lat, lng, info) {
     var pt = new google.maps.LatLng(lat, lng);
     bounds.extend(pt);
     var marker = new google.maps.Marker({
     position: pt,
     icon: icon,
     map: map
     });
     var popup = new google.maps.InfoWindow({
     content: info,
     maxWidth: 300
     });
     google.maps.event.addListener(marker, "click", function() {
     if (currentPopup != null) {
     currentPopup.close();
     currentPopup = null;
     }
     popup.open(map, marker);
     currentPopup = popup;
     });
     google.maps.event.addListener(popup, "closeclick", function() {
     map.panTo(center);
     currentPopup = null;
     });
     }
     function initMap() {
     map = new google.maps.Map(document.getElementById("map"), {
     center: new google.maps.LatLng(0, 0),
     zoom: 14,
     mapTypeId: google.maps.MapTypeId.ROADMAP,
     mapTypeControl: false,
     mapTypeControlOptions: {
     style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR
     },
     navigationControl: true,
     navigationControlOptions: {
     style: google.maps.NavigationControlStyle.SMALL
     }
     });
    
     var arr = 'youphpfile.php';// or url
    for(var i=0; i<arr.length;i++)
    {
        var lat = Number(arr[i].split(",")[0].trim());
        var lon = Number(arr[i].split(",")[0].trim());
        var four = new google.maps.LatLng(lat,lon);
        var marker=new google.maps.Marker({
                    position:four,
                    map:map
                });
    }
     center = bounds.getCenter();
     map.fitBounds(bounds);
    
     }
    

    STEP 3 HTML File.

    <html>
     <head>
     <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
     <title>Google Map API V3 with markers</title>
     <style type="text/css">
     body { font: normal 10pt Helvetica, Arial; }
     #map { width: 350px; height: 300px; border: 0px; padding: 0px; }
     </style>
     <script src="http://maps.google.com/maps/api/js?v=3&sensor=false" type="text/javascript"></script>
    <script src="mymap.js"></script>
    </head>
     <body onload="initMap()" style="margin:0px; border:0px; padding:0px;">
     <div id="map"></div>
     </html>
    

    Looking at what i propose you do. You do not need to encode the data from the PHP file. Then, next you get the data into the JS end of things.

    I hope this helps.

    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算