从Google商家信息搜索框获取纬度和经度

时间:2017-06-18 00:37:37

标签: javascript php mysql google-maps google-places-api

我正在尝试获得Latitude&来自 Google地图搜索的经度。

您可以找到整个代码here

我对Javascript没有深入的了解,所以我看到了几个实际上无法正常工作的解决方案,我想我可能会错误地放置获取lat和lng的代码! 所以请帮我弄清楚我应该在哪里提出解决方案。

我试过

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

它以某种方式不起作用!

我想将这些lat和lng发送给HTML元素 所以我可以将它们作为表单发送到PHP操作页面然后发送到mysql数据库.. 是否有快捷方式可以帮助我直接将它们直接发送到mysql DB或PHP?

1 个答案:

答案 0 :(得分: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 希望这可以帮助。