Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Geocode Address #2

Open
agrigg opened this issue Jul 31, 2019 · 3 comments
Open

Geocode Address #2

agrigg opened this issue Jul 31, 2019 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@agrigg
Copy link

agrigg commented Jul 31, 2019

It would be great to save the latitude and longitude for the address when it is saved / changed using the Google Geocoding API.

Something like using the below geocode method and then saving those additional values in Craft with the address info.

// function to geocode address, it will return false if unable to geocode address
function geocode($address){

    // url encode the address
    $address = urlencode($address);
     
    // google map geocode api url
    $url = "https://maps.googleapis.com/maps/api/geocode/json?address={$address}&key=YOUR_API_KEY";
 
    // get the json response
    $resp_json = file_get_contents($url);
     
    // decode the json
    $resp = json_decode($resp_json, true);
 
    // response status will be 'OK', if able to geocode given address 
    if($resp['status']=='OK'){
 
        // get the important data
        $lati = isset($resp['results'][0]['geometry']['location']['lat']) ? $resp['results'][0]['geometry']['location']['lat'] : "";
        $longi = isset($resp['results'][0]['geometry']['location']['lng']) ? $resp['results'][0]['geometry']['location']['lng'] : "";
        $formatted_address = isset($resp['results'][0]['formatted_address']) ? $resp['results'][0]['formatted_address'] : "";
         
        // verify if data is complete
        if($lati && $longi && $formatted_address){
         
            // put the data in the array
            $data_arr = array();            
             
            array_push(
                $data_arr, 
                    $lati, 
                    $longi, 
                    $formatted_address
                );
             
            return $data_arr;
             
        }else{
            return false;
        }    
    }
    else{
        echo "<strong>ERROR: {$resp['status']}</strong>";
        return false;
    }
}
@sjelfull sjelfull self-assigned this Aug 3, 2019
@sjelfull sjelfull added the enhancement New feature or request label Aug 3, 2019
@sjelfull
Copy link
Member

sjelfull commented Aug 3, 2019

Thanks for the feedback, I think this is a good idea. Feel free to submit a PR if possible.

@agrigg
Copy link
Author

agrigg commented Aug 3, 2019

Cool. I'm working on a PR. I see you already have a geocode method, it is just calling it every time it renders the map. I have most of this in place, but I'm stuck on where to do the save. You have any suggestions on how to hook in? I posted a question on Stack Exchange about it here: https://craftcms.stackexchange.com/questions/31128/geocode-address-when-saving-custom-field-type-in-plugin

@agrigg
Copy link
Author

agrigg commented Aug 7, 2019

Created a PR here: #3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants