Getting the map working
We have gone through all the setup aspects of getting the multiple marker map to work with directorypress. This is the last step in editing the submit listing form. In the wordpress control panel when clicking the theme editor, you need to find the submit listing PHP file. When you access this you now need to add the functions into this page, so that after submissions new entries can be added to your directory, as well as new markers placed on your map. Below we will include the code we have used, and what aspects you need to edit to suit your map.
FIND THIS CODE
First you need to fin this piece of code in the submit PHP file.
// MAP LOCATION
if( isset($_POST['Dir']['map_location']) && strlen($_POST['Dir']['map_location']) > 2){
add_post_meta($POSTID, "map_location", strip_tags($_POST['Dir']['map_location']));
}
PASTE THIS CODE
Now you can paste this code just below the above mentioned code.
$posttitle = $_POST['Dir']['title'];
$aurl = $_POST['Dir']['url'];
$d=0;
// SAVE CUSTOM FIELD DATA
if(is_array($_POST['custom']) ){
foreach($_POST['custom'] as $in_array){
add_post_meta($POSTID, $in_array['name'], $in_array['value']);
$databasefield[$d] = $in_array['name'];
$databasevalue[$d] = $in_array['value'];
//started editing for geocoding on submission
define("MAPS_HOST", "maps.google.com");
define("KEY", "
enter your API key from google here");
// Initialize delay in geocode speed
$delay = 0;
$base_url = "http://" . MAPS_HOST . "/maps/geo?output=xml" . "&key=
enter you API key from google here" . KEY;
$address = $databasevalue[2];
$id = $row["id"];
$request_url = $base_url . "&q=" . urlencode($address);
$xml = simplexml_load_file($request_url) or die("url not loading");
$status = $xml->Response->Status->code;
if (strcmp($status, "200") == 0) {
// Successful geocode
$geocode_pending = false;
$coordinates = $xml->Response->Placemark->Point->coordinates;
$coordinatesSplit = split(",", $coordinates);
// Format: Longitude, Latitude, Altitude
$lat = $coordinatesSplit[1];
$lng = $coordinatesSplit[0];
}
usleep($delay);
//ended editing for geocoding on submission
mysql_query("INSERT INTO map (posttitle,url,$databasefield[0],$databasefield[1],$databasefield[2],
$databasefield[3],$databasefield[4],lat,lng) VALUES ('$posttitle','$aurl','$databasevalue[0]','$databasevalue[1]','$databasevalue[2]',
'$databasevalue[3]','$databasevalue[4]','$coordinatesSplit[1]',
'$coordinatesSplit[0]')");
$d++;
EDIT THE BOLD
If you have followed this tutorial through all the steps, you will recognize some of the database fields we have created in this code. These are the fields you have to edit to correspond with your database fields. In addition, this is where we enter our API key for the Google Maps API v3. Once you have completed this, and you use the submit form, the map will function. As we appreciate we are not proffesional tutorial writers, please leave a comment with any issues, and we see if we can help.
