• Resolved DenisCGN

    (@deniscgn)


    Hello,

    how can I use the results (“types” : [ “administrative_area_level_1”, “political” ]) I get from the Google Maps API (JSON)

    https://maps.googleapis.com/maps/api/geocode/json?latlng=48.7758459,9.1829321&key=GOOGLE_MAP_API

    I need this result and put it into an custom field called “PROVINCE”.

    Any suggestion how I can send the URL, get the response and save the wiched data into a custom field?

    update_post_meta( $post_id, 'province', $google_map_administrative_area_level_1;

    Can anyone help me in this case?

    Thanks so much,
    Denis

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    See if there’s a plugin that might help with that
    https://www.ads-software.com/plugins/search/geocode/

    Many will get coordinates based on a description, actual geocoding. You’re really doing reverse geocoding, given coordinates, get a place description.

    If existing plugins will not meet you needs, a solution can be custom coded. Making the request can be done with the WP_Http class. Decode the JSON response into a PHP array with json_decode(). Then you can save the value of an array element with update_post_meta() like you suggest.

    Thread Starter DenisCGN

    (@deniscgn)

    Hello,

    I did it this way.

    $url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=50.935173,6.953101&key=MyGooglekey";
    $data = @file_get_contents($url);
    $jsondata = array_reverse($jsondata[results][0][address_components]); // to get administrative_area_level_1
    echo $jsondata[2][long_name];

    Maybe its Not the best way, but it seems to work.

    Moderator bcworkz

    (@bcworkz)

    Cool, but I think you’ve neglected to include $jsondata = json_decode( $data ); in your snippet. It couldn’t work without it.

    Thread Starter DenisCGN

    (@deniscgn)

    Right ??

    $jsondata = json_decode($data, true);

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Use Results From Google Maps, HOW?’ is closed to new replies.