2016-05-26 47 views
0

我正在創建一個用戶管理系統。在註冊過程中,用戶輸入他們的個人信息,包括地址屬性。PHP。在註冊步驟中使用geocode google API

<?php 
      include("database.php"); 
     $error = ""; 
      if(isset($_POST['submit'])) 
      { 
       $username = mysql_real_escape_string($_POST['username']); 
       $name = mysql_real_escape_string($_POST['name']); 
       $address = mysql_real_escape_string($_POST['address']); 
       $passwordConfirm = $_POST['passwordConfirm']; 
       $privacy = $_POST['privacy']; 

       //validare i valori inseriti dall'utente 

       if(!filter_var($email, FILTER_VALIDATE_EMAIL)) 
       { 
       $error = "Inserisci una email valida "; 
       } 

       else if (strlen($password < 8)) { 
       $error = "La password deve contenere almeni 8 caratteri"; 
       } 

       else if ($password != $passwordConfirm) 
       { 
       $error = "Le password devono coincidere!"; 
       } 

       else { 
       $error = "Ti sei appena registrato"; 
       } 

$sql = "INSERT INTO users(username, name, surname, affiliation, department,address,position,email,web,telephone,mobile,password,privacy) VALUES('$username','$name','$surname','$affiliation','$department','$address','$position','$email','$web','$telephone','$mobile','$password','$privacy')"; 
       mysqli_query($database,$sql) or die(mysqli_error($database)); 
     header("location:index.php"); 
       } 
     echo "$error"; 
     ?> 

感謝'geocode.php'我得到了地址的地理座標。我設法實現了用戶註冊後發生地理定位的版本。通過這種方式(geocode.php)

<?php 
     session_start(); 
     include("database.php"); 
     if(isset($_SESSION['username'])){ 
      $current_user = $_SESSION['username']; 
      $sql = "SELECT address FROM users WHERE username='$current_user'"; 
      $result = mysqli_query($database,$sql); 
      $row = mysqli_fetch_array($result); 
      } 

     $address = urlencode($row['address']); 
     $request_url = "http://maps.googleapis.com/maps/api/geocode/xml?address=".$address."&sensor=true"; 
     $xml = simplexml_load_file($request_url) or die("url not loading"); 
     $status = $xml->status; 
     if ($status=="OK"){ 
      $lat = $xml->result->geometry->location->lat; 
      $lng = $xml->result->geometry->location->lng; 
      $latlng = "$lat,$lng"; 
      } 
     if(isset($_SESSION['username'])){ 
      $temp=$_SESSION['username']; 
      $sql1 = "UPDATE users SET lat='$lat',lng='$lng' WHERE username='$temp'"; 
      mysqli_query($database,$sql1); 
      } 
    ?> 

我希望我有一些建議,以確保該API調用用戶註冊過程中谷歌地圖發生。

回答

0

我不是100%確定你在找什麼。 根據我的理解,您希望在註冊過程中添加地理位置代碼。

您可以做的是在消毒地址後添加以下代碼。

if($address !='') 
{ 
    $address_code = urlencode($row['address']); 
    $request_url = "http://maps.googleapis.com/maps/api/geocode/xml?address=".$address_code."&sensor=true"; 
    $xml = simplexml_load_file($request_url) or die("url not loading"); 
    $status = $xml->status; 
    if ($status=="OK"){ 
     $lat = $xml->result->geometry->location->lat; 
     $lng = $xml->result->geometry->location->lng; 
     $latlng = "$lat,$lng"; 
     } 
    // Now you have latitude and Longitude saved in $lat and $lang respectively. You can add this in your insert query. 
} 

我希望這是有道理的。