2014-12-08 111 views
2

我有下面的代碼將地址轉換爲經度和緯度,但它只適用於單輸入給出輸入,但我有一個表單,允許用戶輸入地址,城市,州和國家在不同的線路,誰能告訴我怎樣可以轉換和保存給定的地址爲緯度和經度將地址轉換爲經度和緯度在php

<?php 
    ob_start(); 
    $con=mysqli_connect("localhost","root","","db"); 
    // Check connection 
    if (mysqli_connect_errno())  
     { 
      echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
     } 

    if($_POST) 
     { 
      $data_arr = geocode($_POST['address']); // get latitude, longitude and formatted address 
      if($data_arr)// if able to geocode the address 
       { 
        $latitude = $data_arr[0]; 
        $longitude = $data_arr[1]; 
        $formatted_address = $data_arr[2]; 
       } 
       $address = mysqli_real_escape_string($con, $_POST['address']); 
       $sql="INSERT INTO register_office (co_address,latitude,longitude) VALUES ('$address', '$latitude', '$longitude')"; 
       if (!mysqli_query($con,$sql)) 
        { 
         die('Error: ' . mysqli_error($con)); 
        } 
     } 
    mysqli_close($con);  
    ?> 
    <!DOCTYPE html> 
    <html lang="en"> 
     <head> 
     <meta charset="utf-8"> 
     <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
     <meta name="viewport" content="width=device-width, initial-scale=1"> 
     <meta name="description" content=""> 
     <meta name="author" content=""> 
    </head> 

    <body > 
    <form action="" method="post"> 
     <input type='text' name='addressline1' placeholder='address line 1' /> 
     <input type='text' name='addressline2' placeholder='address line 2' /> 
     <input type='text' name='addressline3' placeholder='address line 3' /> 
     <input type='text' name='city' placeholder='city' /> 
     <input type='text' name='state' placeholder='state' /> 
     <input type='text' name='country' placeholder='country' /> 
     <input type='submit' value='Geocode!' /> 
    </form> 

    <?php 

    // function to geocode address, it will return false if unable to geocode address 
    function geocode($address){ 

     // url encode the address 
     $address = urlencode($address); 

     // google map geocode api url 
     $url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address={$address}"; 

     // get the json response 
     $resp_json = file_get_contents($url); 

     // decode the json 
     $resp = json_decode($resp_json, true); 

     // response status will be 'OK', if able to geocode given address 
     if($resp['status']='OK'){ 

      // get the important data 
      $lati = $resp['results'][0]['geometry']['location']['lat']; 
      $longi = $resp['results'][0]['geometry']['location']['lng']; 
      $formatted_address = $resp['results'][0]['formatted_address']; 

      // verify if data is complete 
      if($lati && $longi && $formatted_address){ 

       // put the data in the array 
       $data_arr = array();    

       array_push(
        $data_arr, 
         $lati, 
         $longi, 
         $formatted_address 
        ); 

       return $data_arr; 

      }else{ 
       return false; 
      } 

     }else{ 
      return false; 
     } 
    } 
    ?> 
    </body> 
    </html> 

回答

3

你最好檢查值是否爲空,以及...在此之後,使用逗號,將它們連接起來,就像這樣:

$addressFields = array(
    $_POST['addressline1'], 
    $_POST['addressline2'], 
    $_POST['addressline1'], 
    $_POST['city'], 
    $_POST['state'], 
    $_POST['country'],  
); 

$addressData = implode(', ', array_filter($addressFields)); 

在這個片段中,array_filter()是用來過濾全是空的從陣列中tr tr。 implode()使用逗號,作爲'膠水'將數組項目連接在一起。
之後,使用geocode()方法,在你的榜樣:

$data_arr = geocode($addressData); 
1

添加地址部分一起,形成一個單一的線,並提交給谷歌地圖改爲API。

$address_parts = array($addressline1, $addressline2, $addressline3, $city, $state, $country); 
$address = implode(", ", $address_parts); // join the parts together divided by comma 
1

這聽起來像你應該能夠連接你的地址部分,然後發送呢?

E.g.

$address = $_POST['addressline1'] .','. $_POST['addressline2'] <<snip>> $_POST['country']; 

$data_arr = geocode($address); 

如果谷歌有重複逗號麻煩你可能需要建立一個有點if語句只擺在那實際上是對位設置(此時建立一個數組和內爆,它可能會更好)