2013-08-19 58 views
0

我試圖從表單提交中獲取基於IP地址地理位置的位置信息的Google地圖。如何從PHP輸入表單獲取來自城市的谷歌地圖?

下面的PHP代碼工作是這樣的:如果我在文本框中輸入IP地址,它會給我城市和國家。我需要自動獲取相應城市的Google地圖:

<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> 

回答

2

代碼,您可以在其中獲取所需地點的地圖。在VAR地址 查找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> 

您可以manupulate這個代碼acording您的需求,

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

你必須通過你的$myString.$ip['cityName'].變量。希望這可以幫助

+0

但經度和緯度已在代碼中定義。 – Arun

+0

此代碼無效 – Arun

+0

@Arun代碼完美工作,如果您爲樣式表添加完整網址:https://developers.google.com/maps/documentation/javascript/examples/default.css – Grant

相關問題