从Google地方信息搜索框中获取经度和纬度 [英] Getting Latitude and Longitude from Google Places Search box

查看:104
本文介绍了从Google地方信息搜索框中获取经度和纬度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取谷歌纵横来自 Google Maps Places Searchbox 的经度.

I'm Trying to get the Latitude & Longitude from Google maps Places Searchbox.

您可以在此处找到完整的代码.

you can find the whole code here.

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

I don't have a deep knowledge to Javascript so I've seen a couple of solutions that doesn't actually work and I think I might misplace the code that gets the lat and lng! So please help me to figure out where should I actually put the solution.

我尝试过

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

它不起作用!

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

I'd like to send these lat and lng to HTML element so I can send them as form to a PHP action Page and then to mysql Database.. Is there a shortcut that help me send them directly to the mysql DB or to PHP directly?

推荐答案

在没有其余代码的情况下,我只能向您展示我使用表单收集地址所做的事情.使用php 7和json_decode().有一种更有效的方法可以执行此操作,但是它对我有用.希望这会有所帮助.

Without the rest of your code, I can only show you what I have done using a form to collect an address. Using php 7 with json_decode(). There is a more efficient way to do this, but it works for me. Hope this helps.

//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.然后,我可以调用它,以便以后在地图上找到这些经纬度较高的客户.

Then you can add these variables to your DB insert/update and/or echo them out as needed. This very code is run through an applet I am working on that takes a given address entered into a customer form and then processes that address, gets Lat and Long and then upon success, places that into the array I am running through the insert into my DB. I can then call on that to locate that customers lat and long on a map at a later date.

$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安全性的文档,并发现它最有帮助:

Btw, I am currently going through this document on XSS security and have found it to be most helpful: XSS_Filter_Evasion_Cheat_Sheet Hope this helps.

这篇关于从Google地方信息搜索框中获取经度和纬度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆