PHP。在注册步骤中使用geocode google API

时间:2016-05-26 13:32:48

标签: php mysql

我正在创建一个用户管理系统。在注册过程中,用户输入他们的个人信息,包括地址属性。

    <?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";
        ?>

感谢&#39; geocode.php&#39;我得到了地址的地理坐标。我设法实现了一个在用户注册后发生地理定位的版本。这样(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调用。

1 个答案:

答案 0 :(得分: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.
}

我希望这是有道理的。