2017-06-18 98 views
0

我試圖獲取緯度&經度從谷歌地圖地方搜索框從Google地方獲取經度和緯度搜索框

你可以找到整個代碼here

我對JavaScript沒有深入的瞭解,所以我見過一些實際上並不工作的解決方案,我想我可能放錯取得lat和lng的代碼! 所以請幫我弄清楚我應該在哪裏實際解決方案。

我已經試過

place.geometry.location.lat() 
place.geometry.location.lng() 

它在某種程度上不工作!

我想這些緯度和經度,以HTML元素 這樣我就可以把他們作爲形式的PHP動作頁,然後發送到MySQL數據庫.. 是否有一個捷徑,幫我直接發送到MySQL DB或直接PHP?

+0

爲了將來的評論,我建議你添加所有相關的代碼以及jfiddle或snipit,而不是鏈接到任意的庫,比如google api的例子。祝你好運。 –

回答

0

沒有剩下的代碼,我只能告訴你我用表單來收集地址。使用php 7和json_decode()。有一個更有效的方式來做到這一點,但它適用於我。希望這可以幫助。

//create string holding address to run through google geolocation API for Lat/Long 
//make sure to clean your posts, I use functions from another page that cleans htmls tags, trims and removes slashes and/or runs through a reg ex to replace and/or remove unwanted entries for the particular fields type. 
     $address = clean_post($_POST['cust_street']).' '.clean_post($_POST['cust_city']).' '.clean_post($_POST['cust_state']).', '.clean_post($_POST['cust_zip']); 

     // Get JSON results from this request 
     $url = 'https://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($address).'&sensor=false&key='.APIKEY; 
     $geo = file_get_contents($url); 

     // Convert the JSON to an array 
     $geo = json_decode($geo, true); 

     if ($geo['status'] == 'OK') { 
      // Get Lat & Long 
      $latitude = $geo['results'][0]['geometry']['location']['lat']; 
      $longitude = $geo['results'][0]['geometry']['location']['lng']; 

     }else{ 
      $latitude = NULL; 
      $longitude = NULL; 
      $sessData['status']['type'] = 'alert alert-warning'; 
      $sessData['status']['msg'] = 'Sorry, but it seems the address you entered is not being recognized by Google Maps API service.'; 
      //die or exit, do something on failure 
     } 

然後,您可以將這些變量添加到您的數據庫插入/更新和/或根據需要回顯出來。這個代碼是通過我正在處理的一個applet運行的,它需要一個輸入到客戶表單中的給定地址,然後處理該地址,獲取Lat和Long,然後在成功時將這些數據放入我通過插入到我的數組中D B。然後,我可以通過電話在稍後的日期在地圖上查找經緯度。

$custData = array(
      'users_id' => $usr_id, 
      'cust_first_name' => clean_post($cust_first_name), 
      'cust_last_name' => clean_post($cust_last_name), 
      'cust_email' => clean_email($cust_email), 
      'cust_street' => clean_post($cust_street), 
      'cust_city' => clean_post($cust_city), 
      'cust_state' => clean_post($cust_state), 
      'cust_zip' => clean_post($cust_zip), 
      'cust_phone1' => clean_phone($cust_phone2), 
      'cust_lat' => clean_phone($latitude), 
      'cust_long' => clean_float($longitude), 
      //etc,etc,etc 

     ); 
      //$cust = new User(); further up in page 
      //insertCust() function comes from user class stored in class.user.php 
      $insert = $cust->insertCust($custData); 

      //set status based on data insert 
      if($insert){ 
       //set some session variables and store them before any header redirects. 
       $_SESSION['status']['type'] = 'alert alert-success'; 
       $_SESSION['status']['msg'] = 'You have registered '.$custName.' successfully!'; 

      } 

順便說一句,我目前正在經歷上XSS安全這個文件,並發現它是最有幫助的:XSS_Filter_Evasion_Cheat_Sheet 希望這有助於。