【问题标题】:How to get google maps from city got from a PHP input form?如何从 PHP 输入表单中获取城市的谷歌地图?
【发布时间】:2013-08-19 12:50:05
【问题描述】:

我正在尝试根据表单提交的 IP 地址地理位置获取显示位置信息的 Google 地图。

下面的 PHP 代码是这样工作的:如果我在文本框中输入一个 IP 地址,它将给我城市和国家。我需要自动获取对应城市的谷歌地图:

 <html>
<body>
<?php
if( !defined('LOADED') )
   die('You cannot access this file directly!');
function countryCityFromIP($ipAddr)
{
    $url = "http://api.ipinfodb.com/v3/ip-city/?key=5cfaab6c5af420b7b0f88d289571b990763e37b66761b2f053246f9db07ca913&ip=$ipAddr&format=json";
    $d = file_get_contents($url);
    return json_decode($d , true);
}

if(isset($_REQUEST['submit'])){
   $ip=countryCityFromIP($_REQUEST['ip']);

   //print_r($ip);
   $myString = "The City for the entered IP Address= ";
   $myString2 = "The Country for the entered IP Address= ";
   echo "<p style='text-align: center; font-size: 25px; font-family: georgia,palatino; color: #202020;'>".$myString.$ip['cityName']."</p>";
   echo "<p style='text-align: center; font-size: 25px; font-family: georgia,palatino; color: #202020;'>".$myString2.$ip['countryName']."</p>";
}
?>
<form method="post">
<center><input type="text" name="ip" style="font-size:25pt;height:30px;width:400px"/></center>
<center><input type="submit" name="submit" value="Search IP Location" style="font-size:15pt;height:40px;width:200px"/></center>
</form>
</body>

【问题讨论】:

    标签: php google-maps geolocation


    【解决方案1】:

    您可以在其中获取所需地点的地图的代码。 查找 GOOGLE 地理编码以获取更多信息

    <!DOCTYPE html>
    <html>
      <head>
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
        <meta charset="utf-8">
        <title>Geocoding service</title>
        <link href="/maps/documentation/javascript/examples/default.css" rel="stylesheet">
        <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
        <script>
    var geocoder;
    var map;
    function initialize() {
      geocoder = new google.maps.Geocoder();
      var latlng = new google.maps.LatLng(-34.397, 150.644);
      var mapOptions = {
        zoom: 8,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      }
      map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
    }
    
    function codeAddress() {
      var address = document.getElementById('address').value;
      geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          map.setCenter(results[0].geometry.location);
          var marker = new google.maps.Marker({
              map: map,
              position: results[0].geometry.location
          });
        } else {
          alert('Geocode was not successful for the following reason: ' + status);
        }
      });
    }
    
    google.maps.event.addDomListener(window, 'load', initialize);
    
        </script>
      </head>
      <body>
        <div id="panel">
          <input id="address" type="textbox" value="Sydney, NSW">
          <input type="button" value="Geocode" onclick="codeAddress()">
        </div>
        <div id="map-canvas"></div>
      </body>
    </html>
    

    您可以根据需要修改此代码,

    var address = document.getElementById('address').value;
    

    在 var 地址中,您必须传递您的 $myString.$ip['cityName']. 变量。希望这会有所帮助

    【讨论】:

    • 但是代码中已经定义了经纬度。
    • @Arun 代码如果您为样式表添加完整的 url,则可以完美运行:developers.google.com/maps/documentation/javascript/examples/…
    • 是的,它是在 var latlang 中定义的,你也可以用你自己的纬度和对数来改变它。正如所说,这是为了让你开始,我没有为你提供完全烤好的蛋糕
    • 可以这样做,因为我不擅长编码...提前谢谢。
    猜你喜欢
    • 1970-01-01
    • 2010-11-09
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 2011-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多