Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

GeoMashup plugin - get address from custom fields WordPress

Hey,

I'm thinking about using the Geomashup plugin to display maps across my website. I'm setting up a 'local guide' which will contain pages for small businesses including a map.

Ideally I'd like geomashup to get it's data for the map from custom fields (as the data is captured from users in the front-end and added to the custom fields).

The custom fields I currently have setup are:

guide-address
guide-postcode

Would it be possible to somehow grab the data from these fields and use it to add the location to the map?

It might also be worth mentioning that the title / name of the place is entered into the title field for the WordPress post.

Thanks.
DW


EDIT 02/01/12:
I don't necessarily have to use GeoMashup for this, if anyone has any suggestions to other ways I could display a map, using the custom field data I'm open to suggestions.

Answers (2)

2012-01-02

Julio Potier answers:

Hello

This can be done with wordpress : [[LINK href="http://codex.wordpress.org/Geodata"]]http://codex.wordpress.org/Geodata[[/LINK]]

And check this in your GeoMashup plugin settings :
<blockquote>Copy Geodata Meta Fields [x] Copy coordinates to and from geodata meta fields, for integration with the Geolocation and other plugins.</blockquote>
So your meta fields names are wrong i guess.

See you !


Dale Williams comments:

Thanks for the reply, the only problem is geo_latitude and geo_longitude are required in order for this to work?

I guess my question is can these two fields be populated and the geo_latitude / geo_longitude be generated using the data from my 'guide-address' field?


Julio Potier comments:

What kind of data contains your "guide-address" field ? Can you paste an example here ? Thx


Dale Williams comments:

Sure,

the guide-address field is in the format of:
Bristol Zoo Gardens, Clifton, Bristol

and the guide-postcode field is a standard UK postcode so:
BS8 3HA


Julio Potier comments:

Damn, i think you have to get the L/L datas from google maps API and then update your meta fields, this is not a simple script line and easy :/

Could you return back in time and create the required fields like geomashup needs it ?
See what i mean ?


Dale Williams comments:

I could as this is a new setup, but I would prefer that the user enters the address in the format I have shown in guide-address.

The users submitting data from the front end need the process to be easy and the audience I'm targeting the website at won't know how to find the L/L for Google Maps, so it really needs to get the info from the address and postcode.


Julio Potier comments:

Ok this is a user entry, i understand your need mmm i'll think about it and try to figure it out.
But i have to be honest : i won't and can not spent many time for $10, with this price, i can give you ways, ideas but no produce full code.
I can do it (full code) for more of course, contact me via private message (clic my name/avatar and clic "send message")

See you sson


Julio Potier comments:

Look, i found this for you :
function getLatLong($address){
if (!is_string($address))die("All Addresses must be passed as a string");
$_url = sprintf('http://maps.google.com/maps?output=js&q=%s',rawurlencode($address));
$_result = false;
if($_result = file_get_contents($_url)) {
if(strpos($_result,'errortips') > 1 || strpos($_result,'Did you mean:') !== false) return false;
preg_match('!center:\s*{lat:\s*(-?\d+\.\d+),lng:\s*(-?\d+\.\d+)}!U', $_result, $_match);
$_coords['lat'] = $_match[1];
$_coords['long'] = $_match[2];
}
return $_coords;
}


Than i picked the <strong>Dylan Kuhn</strong> function code and modified :
function my_action_added_post_meta( $meta_id, $post_id, $meta_key, $meta_value ) {
if ( 'guide-postcode' == $meta_key ) {
$full_address = get_post_meta( $post_id, 'guide-address', true ) . ', ' . $meta_value;
$location = getLatLong( $full_address );
update_post_meta( $post_id, 'geo_latitude', $location['lat'] );
update_post_meta( $post_id, 'geo_longitude', $location['long'] );
}
}
add_action( 'added_post_meta', 'my_action_added_post_meta', 10, 4 );
add_action( 'added_postmeta', 'my_action_added_post_meta', 10, 4 );


I did not test it ! Sorry

2012-01-02

Dylan Kuhn answers:

If you can add a custom field that contains both the address and the postcode separated by a comma, you can enter the name of that field in Geo Mashup's "Geocode Custom Field" setting. Geo Mashup will then try to convert that field into a location when the field is saved.

The trouble with multiple fields is that you generally don't know the order they'll be saved in, but both must be present before geocoding. If you knew that guide-postcode would always be saved after guide-address, some code like this in functions.php might do it for you:


function my_action_added_post_meta( $meta_id, $post_id, $meta_key, $meta_value ) {
if ( class_exists( 'GeoMashupDB' ) and 'guide-postcode' == $meta_key ) {
$full_address = get_post_meta( $post_id, 'guide-address', true ) . ', ' . $meta_value;
$location = GeoMashupDB::blank_location( ARRAY_A );
GeoMashupDB::geocode( $full_address, $location );
GeoMashupDB::set_object_location( 'post', $post_id, $location, null );
}
}
add_action( 'added_post_meta', 'my_action_added_post_meta', 10, 4 );
add_action( 'added_postmeta', 'my_action_added_post_meta', 10, 4 ); // Covers AJAX calls


Dale Williams comments:

Thanks for the suggestion, I gave this a go, I tried with the code you provided and also by combining the guide-address and guide-postcode fields but still no luck.


Dylan Kuhn comments:

If the combined field is a possibility, let's pursue that. Can you give me examples of a combined address and postcode you're testing with? I'm assuming you're using Google as a map provider too - geocoding is more limited if you're using OpenLayers.


Dale Williams comments:

Also, I don't necessarily have to use GeoMashup for this, if anyone has any suggestions to other ways I could display a map, using the custom field data I'm open to suggestions.


Dale Williams comments:

Sure a couple mor examples of how the info would look from a combined field:

Great Western Dockyard, Bristol, BS1 6TY
Bristol Zoo Gardens, Clifton, Bristol, BS8 3HA
Welsh Back, Bristol, BS1 4RR
Glass house, Cabot Circus, Bristol, BS1 3BX
Broad Quay, Bristol, BS1 4DA


Dylan Kuhn comments:

This is working for me:

1. Set the Geocode Custom Field to "geocodeme" (http://cl.ly/1l0G3i3g030E2W0z2e2y)
2. Create a test post, add a "geocodme" custom field with a value of "Bristol Zoo Gardens, Clifton, Bristol, BS8 3HA" (http://cl.ly/2a1y3o2Q2Y412A363t27)
3. Publish the post or refresh to see the location (http://cl.ly/3z3R1n3b38441i080x33)


Dylan Kuhn comments:

See if I can make those image links clickable:

1. [[LINK href="http://cl.ly/1l0G3i3g030E2W0z2e2y"]]geo mashup setting[[/LINK]]
2. [[LINK href="http://cl.ly/2a1y3o2Q2Y412A363t27"]]test post custom field[[/LINK]]
3. [[LINK href="http://cl.ly/3z3R1n3b38441i080x33"]]post location[[/LINK]]


Dale Williams comments:

Sorry, just tried this again, not sure what I missed last time but this appears to be working well under a single field!