Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

In my WordPress admin I have an address field for each post (custom post type). The address is just a string.

I need to find the country and region for each post, which I have been doing somewhat successfully using JSON:

$address = //address string here
$url = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false";
$jsonurl = file_get_contents($url);
$geocodedinfo = json_decode($jsonurl, true);

However, as I need to get this info in one go for hundreds of posts at once, the approach needs a rethink.

Ideally I'd like to be able to edit a post, enter an address and retrieve the Google geocode data. This could either be into separate fields (country and region a MUST), or as a JSON string. As long as the results are saved into the database, then thats great.

Getting 'country' should be straightforward. 'Region' a little trickier as the Google response isn't always the same. Currently I've been doing the following, which usually gets me something that will do as 'region':

    foreach ($result["address_components"] as $address) {

       if (in_array("country", $address["types"])) {
          $country = $address["long_name"];
       }

       if (in_array("administrative_area_level_1", $address["types"])) {
          $locality = $address["long_name"];
       } elseif (in_array("administrative_area_level_2", $address["types"])) {
          $locality = $address["long_name"];
       } elseif (in_array("locality", $address["types"])) {
          $locality = $address["long_name"];
       } 


    }

I'd really appreciate some steer on this as I'm at a loss. I'm also a WordPress beginner :)

UPDATE

Just to help illustrate what I'm after. I want to enter the address string in a field, click search, and retrieve region and country, just like below:

enter image description here

share|improve this question
    
Hi, plugin recommendation is off-topic, see faq. Maybe, you can rephrase(re-investigate) the Question to try do develop a custom solution. –  brasofilo Apr 3 '13 at 11:30
    
Okay, will do. Just didn't want to reinvent the wheel if there was something out there already :S Cheers. –  Chris Apr 3 '13 at 11:37
    
@brasofilo I was initially going to suggest the same, given that this is not only asking for a recommendation, but upon closer inspection, the rest would be a much better fit for Stack Overflow, it's pretty much a question about the Google Geocoding API, whether in WP context or not. –  Johannes Pille Apr 3 '13 at 11:39
    
Cheers guys. I've updated the question to hopefully fit within the rules. I guess there are 2 parts to it. 1) In admin retrieve the geocode (country, region) and 2) save to a database field, maybe postmeta? –  Chris Apr 3 '13 at 11:43
    
Chris, @JohannesPille has a point here. What is the problem on WP side? Handling hundreds of entries? And I suppose this has to be done only once for older posts, and checked/updated on each save_post. Where exactly are you applying the actual code? –  brasofilo Apr 3 '13 at 11:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.