简体   繁体   中英

Getting Latitude and Longitude from Google Places Search box

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

you can find the whole code here .

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.

I've tried

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

it somehow doesn't work!

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?

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
        }

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!';

            }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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