繁体   English   中英

从Google地方信息搜索框中获取经度和纬度

[英]Getting Latitude and Longitude from Google Places Search box

我正在尝试从Google Maps地方搜索框获取经度和纬度。

您可以在此处找到整个代码。

我对Java语言没有很深的了解,因此我看到了一些实际上无效的解决方案,我想我可能会放错获取lat和lng的代码! 因此,请帮助我弄清楚应该将解决方案实际放在哪里。

我试过了

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

它以某种方式不起作用!

我想将这些经纬度发送给HTML元素,以便将它们作为表单发送到PHP操作页面,然后发送给mysql数据库。是否有快捷方式可以帮助我直接将它们发送到mysql数据库或直接发送给PHP ?

如果没有其余的代码,我只能向您展示我使用表单收集地址所完成的工作。 使用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希望这会有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM