使用href传递值

时间:2016-11-11 09:14:16

标签: php google-maps xampp

我在phpadmin上创建了一个数据库,我已经将信息从xml文件提取到数据库,我的数据库字段包括时间,纬度,经度,深度和幅度。我有一个表格,显示网页上的所有信息和我有一个包含链接的字段,同时点击链接,它应该指引我到谷歌地图api,它必须从数据库中提取纬度和经度信息,我有问题传递纬度值和经度自动,任何帮助。这是我的代码

<?php
    require('dbconnect.php');
    $query="SELECT * from earthquake_xml";
    $result= mysqli_query($dbc,$query);
    echo "Query Executed Successfully ";
    $numRow =mysqli_num_rows($result);
    echo "<br> Number Of Rows:".$numRow;
?>
<!DOCTYPE HTML>
<html>
<head>
    <style>
    table, th, td {
   border: 1px solid black;
  padding:10px;
}
table{
    border-collapse: collapse;
    text-align:center;

}
    </style>
</head>
<body>
<table>
    <tr>
        <td>Time</td>
        <td>Latitude</td>
        <td>Longitude</td>
        <td>Depth</td>
        <td>Magnitude</td>
        <td>Google Map </td>
    </tr>
    <?php
    if($numRow>0)
    {
        while($row=mysqli_fetch_assoc($result))
        {
            echo "<tr>";
            echo "<td>" .$row['Time'] . "</td>";
            echo "<td>" .$row['Latitude']. "</td>";
            echo "<td>" .$row['Longitude']. "</td>";
            echo "<td>" .$row['Depth']. "</td>";
            echo "<td>" .$row['Magnitude']. "</td>";
            echo "<td><a href='googleMap.php?longitude= '' '>Click here</a></td>";
            echo "</tr>";

        }




    }

    ?>

</table>

</body>
</html>

这是我的googleMap.php

 <!DOCTYPE HTML>
<HTML>
<head>
<title></title>
<style>
html,body{
    height:100%;
    margin:0;
    padding:0;
}
#map{
    height:100%;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
function initMap(){
    var myLatLng={lat:-36.5806,lng:-73.6355};
    var map=new google.maps.Map(document.getElementById('map'),{
    center:myLatLng,
    zoom:8
    });

    if(navigator.geolocation){
        navigator.geolocation.getCurrentPosition(function(position){
            var pos={
                lat:-36.5806,
                lng:-73.6355
            };

            var marker=new google.maps.Marker({
                position:pos,
                map:map,
            });

            map.setCenter(pos,marker)
        },function(){
            handleLocationError(true,map.getCenter());
        });
    }else{
        handleLocationError(false,map.getCenter)
    }
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=&callback=initMap">
</script>
</body>

</html>

2 个答案:

答案 0 :(得分:0)

在HTML页面中,您可以轻松地将纬度和经度作为查询字符串传递。

echo "<td><a href='googleMap.php?longitude=".$row['longitude']."&latitude=".$row['latitude']."'>Click here</a></td>";

要获得相同的纬度和经度,只需使用GET请求更新您的脚本。

function initMap(){
    var myLatLng={lat:'<?php echo $latitude ?>',lng:'<?php echo $longitude?>'};
    ....// so on
}

答案 1 :(得分:0)

您可以直接从php使用geolocation api。

您可以调用这样的网址:

https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=YOUR_API_KEY

并收到像这样的结构化json响应:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "1600",
               "short_name" : "1600",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Amphitheatre Parkway",
               "short_name" : "Amphitheatre Pkwy",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Mountain View",
               "short_name" : "Mountain View",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Santa Clara County",
               "short_name" : "Santa Clara County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "California",
               "short_name" : "CA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Stati Uniti",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "94043",
               "short_name" : "94043",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "1600 Amphitheatre Pkwy, Mountain View, CA 94043, Stati Uniti",
         "geometry" : {
            "location" : {
               "lat" : 37.4223664,
               "lng" : -122.084406
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 37.4237153802915,
                  "lng" : -122.0830570197085
               },
               "southwest" : {
                  "lat" : 37.42101741970851,
                  "lng" : -122.0857549802915
               }
            }
         },
         "place_id" : "ChIJ2eUgeAK6j4ARbn5u_wAGqWA",
         "types" : [ "street_address" ]
      }
   ],
   "status" : "OK"
}

您可以使用curl进行此调用。

参考。 https://developers.google.com/maps/documentation/geocoding/start

相关问题