Display map in custom form

HI guys,
i created a custom form to create new listing. right now i am using custom field for address that generates the map. However, i want to be able to display the map in my custom form same way it as it is in the admin area so users will be able to bring it up and choose location. i am pretty sure it can be done and i was looking around in the plugin php files but couldn't find the right code that displays the map. would you be able to drop a clue about this one?

  • fitoussi

    Hi Bigonroad,
    i hope it is not too long but i dropped the codes in here. One is the custom form i created for custom post type "Coupons". (i will create the same form for directory listing where i will need the map to be displayed). the second file is the function that generate the address for the map since i use different fields to create the address. Right now it works fine when i use the form to create new post and the address is creating a new map. However, as i mentioned i would like to have a live map in the custom form.

    custom form :

    <?php
    /*
    Template Name: new coupon form
    */
    ?>
    <?php
    
    	$error_m = "This is a required field";
    if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "new_post") {
    
    		if (empty ($_POST['title'])) {
    		// echo "<script>alert('some_message');</script>";
    		$t_e = 1 ;
    
    		} if
    		(empty ($_POST['description'])) {
    		$d_e = 1 ;
    
    		} if
    		(empty ($_POST['street'])) {
    		$s_e = 1 ;
    
    		} if
    		(empty ($_POST['city'])) {
    		$c_e = 1 ;
    
    		} if
    		(empty ($_POST['state'])) {
    		$st_e = 1 ;
    
    		} if
    		(empty ($_POST['zipcode'])) {
    		$z_e = 1 ;
    		}
    		if ( $t_e == 1 || $d_e == 1 || $s_e == 1 || $c_e == 1 || $st_e == 1 || $z_e == 1) {
    			 //	echo "<script>alert('12345');</script>";
    
    	 			$errors = 1;
    	 	}
    
    	if ($errors == 1) {
    		//	echo "<script>alert('errors');</script>";
    
    	} else { 
    
    	$title =  $_POST['title'];
    	$description = $_POST['description'];
    	$tags = $_POST['post_tags'];
    	$street = $_POST['street'];
    	$apt = $_POST['apt'];
    	$city = $_POST['city'];
    	$state = $_POST['state'];
    	$zipcode = $_POST['zipcode'];
    	$phone = $_POST['phone'];
    	$website = $_POST['website'];
    	// ADD THE FORM INPUT TO $new_post ARRAY
    	$new_post = array(
    	'post_title'	=>	$title,
    	'post_content'	=>	$description,
    /*	'post_category'	=>	array($_POST['cat']),  // Usable for custom taxonomies too  */
    	//'tax_input'	=> array( 'coupons_tags' => $tags),
    	'post_status'	=>	'pending',           // Choose: publish, preview, future, draft, etc.
    	'post_type'	=>	'coupons',  //'post',page' or use a custom post type if you want to
    	'street'	=>	$street
    	);
    
    	//SAVE THE POST
    	$pid = wp_insert_post($new_post);
    	// save taxonomies: post ID, form name, taxonomy name, if it appends(true) or rewrite(false)
    	wp_set_post_terms($pid, array($_POST['cat']),'coupons_categories',true);
    	wp_set_post_terms($pid, array($_POST['post_tags']),'coupons_tags',true); 
    
        //KEEPS OUR COMMA SEPARATED TAGS AS INDIVIDUAL
    //	wp_set_post_tags($pid, $_POST['post_tags']);
    	//wp_set_object_terms( $pid, '12','coupons_categories',true );
    
    	//REDIRECT TO THE NEW POST ON SAVE
    	$link = get_permalink( $pid );
    	wp_redirect( $link );
    
    	//ADD OUR CUSTOM FIELDS
    	add_post_meta($pid, 'street', $street, true);
    	add_post_meta($pid, 'apt', $apt, true);
    	add_post_meta($pid, 'city', $city, true);
    	add_post_meta($pid, 'state', $state, true);
    	add_post_meta($pid, 'zipcode', $zipcode, true);
    	add_post_meta($pid, 'phone', $phone, true);
    	add_post_meta($pid, 'website', $website, true);
    
    	//INSERT OUR MEDIA ATTACHMENTS
    	if ($_FILES) {
    		foreach ($_FILES as $file => $array) {
    		$newupload = insert_attachment($file,$pid);
    		// $newupload returns the attachment id of the file that
    		// was just uploaded. Do whatever you want with that now.
    		}
    
    	} // END THE IF STATEMENT FOR FILES
    }
    } // END THE IF STATEMENT THAT STARTED THE WHOLE FORM
    
    //POST THE POST
    do_action('wp_insert_post', 'wp_insert_post');
    
    ?>
    <?php get_header(); ?>
    
    <!-- <script>
    function textCounter( field, countfield, maxlimit ) {
      if ( field.value.length > maxlimit )
      {
        field.value = field.value.substring( 0, maxlimit );
        alert( 'Textarea value can only be 70 characters in length.' );
        return false;
      }
      else
      {
        countfield.value = maxlimit - field.value.length;
      }
    }</script> -->
    <script type="text/javascript">
    
    var alert_title='Input Restriction';
    
    function limitTextarea(el,maxLines,maxChar){
    if(!el.x){
    el.x=uniqueInt();
    el.onblur=function(){clearInterval(window['int'+el.x])}
    }
    window['int'+el.x]=setInterval(function(){
    var lines=el.value.replace(/\r/g,'').split('\n'),
    i=lines.length,
    lines_removed,
    char_removed;
    if(maxLines&&i>maxLines){
    alert('You can not enter\nmore than '+maxLines+' lines');
    lines=lines.slice(0,maxLines);
    lines_removed=1
    }
    if(maxChar){
    i=lines.length;
    while(i-->0)if(lines[i].length>maxChar){
    lines[i]=lines[i].slice(0,maxChar);
    char_removed=1
    }
    if(char_removed)alert('You can not enter more\nthan '+maxChar+' characters per line')
    }
    if(char_removed||lines_removed)el.value=lines.join('\n')
    },50);
    }
    
    function uniqueInt(){
    var num,maxNum=100000;
    if(!uniqueInt.a||maxNum<=uniqueInt.a.length)uniqueInt.a=[];
    do num=Math.ceil(Math.random()*maxNum);
    while(uniqueInt.a.hasMember(num))
    uniqueInt.a[uniqueInt.a.length]=num;
    return num
    }
    
    Array.prototype.hasMember=function(testItem){
    var i=this.length;
    while(i-->0)if(testItem==this[i])return 1;
    return 0
    };
    
    function set_ie_alert(){
    window.alert=function(msg_str){
    vb_alert(msg_str)
    }
    }
    
    </script>
    <script language="vbscript" type="text/vbs">
    
    set_ie_alert()
    
    Function vb_alert(msg_str)
    MsgBox msg_str,vbOKOnly+vbInformation+vbApplicationModal,alert_title
    End Function
    
    </script>
    
    	<div id="content" role="main">
    
    		<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    			<h1 class="entry-page-title"><?php the_title(); ?></h1>
    
    		<!-- add coupon form -->
    
    		<div class="new-coupon-form">
    			<form id="new_post" name="new_post" method="post" action="" class="add-coupon-form" enctype="multipart/form-data">
    			<!-- post name -->
    				<fieldset name="name">
    					<label for="title">Business Name:</label><div class='f_error'><?php if ($t_e == 1) { echo $error_m ;}  ?></div><br />
    					<input type="text" id="title" value="<?php echo $_POST['title']; ?>" tabindex="5" name="title" />
    				</fieldset>
    
    				<!-- post Category -->
    				<fieldset class="category">
    					<label for="cat">Category:</label>
    					<?php wp_dropdown_categories( 'tab_index=10&taxonomy=coupons_categories&hide_empty=0' ); ?>
    				</fieldset>
    
    				<!-- post Content -->
    				<fieldset class="content">
    					<label for="description">Discount Description:</label><div class='f_error'><?php if ($d_e == 1) { echo $error_m ;}  ?></div><br />
    					<textarea id="description" tabindex="15" name="description" cols="10" onfocus="limitTextarea(this,5,14)"><?php echo $_POST['description']; ?></textarea>
    				</fieldset>
    
    				<h3> Please enter the business's full address: </h3>
    
    				<fieldset class="street">
    					<label for="street">Street:</label><div class='f_error'><?php if ($s_e == 1) { echo $error_m ;}  ?></div><br />
    					<input type="text" value="<?php echo $_POST['street']; ?>" id="street" tabindex="20" name="street" />
    				</fieldset>
    
    				<fieldset class="apt">
    					<label for="apt">Apt/Suit Number:</label><div class='f_error'><?php if ($a_e == 1) { echo $error_m ;}  ?></div><br />
    					<input type="text" value="<?php echo $_POST['apt']; ?>" id="apt" tabindex="20" name="apt" />
    				</fieldset>
    
    				<fieldset class="city">
    					<label for="city">City:</label><div class='f_error'><?php if ($c_e == 1) { echo $error_m ;}  ?></div><br />
    					<input type="text" value="<?php echo $_POST['city']; ?>" id="city" tabindex="20" name="city" />
    				</fieldset>
    
    				<fieldset class="state">
    					<label for="state">State:</label><div class='f_error'><?php if ($st_e == 1) { echo $error_m ;}  ?></div><br />
    					<input type="text" value="<?php echo $_POST['state']; ?>" id="state" tabindex="20" name="state" />
    				</fieldset>
    
    				<fieldset class="zipcode">
    					<label for="zipcode">Zip Code:</label><div class='f_error'><?php if ($z_e == 1) { echo $error_m ;}  ?></div><br />
    					<input type="text" value="<?php echo $_POST['zipcode']; ?>" id="zipdode" tabindex="20" name="zipcode" />
    				</fieldset>
    
    				<fieldset class="phone">
    					<label for="phone">Phone Number:</label><br />
    					<input type="text" value="" id="phone" tabindex="20" name="phone" />
    				</fieldset>
    
    				<fieldset class="website">
    					<label for="website">Website Address:</label><br />
    					<input type="text" value="" id="website" tabindex="20" name="website" />
    				</fieldset>
    
    				<!-- images -->
    				<fieldset class="images">
    					<label for="bottle_rear">Choose Image:</label><br />
    					<input type="file" name="bottle_rear" id="bottle_rear" tabindex="30" />
    				</fieldset>
    
    				<!-- post tags -->
    				<fieldset class="tags">
    					<label for="post_tags">Additional Keywords (comma separated):</label><br />
    					<input type="text" value="" tabindex="35" name="post_tags" id="post_tags" />
    				</fieldset>
    
    				<fieldset class="submit">
    					<input type="submit" value="Post Coupon" tabindex="40" id="submit" name="submit" />
    				</fieldset>
    
    				<input type="hidden" name="action" value="new_post" />
    				<?php wp_nonce_field( 'new-post' ); ?>
    
    			</form>
    
    		</div> <!-- form-content -->
    
    		<!-- END OF FORM -->
    
    		</div><!-- #post-## -->
    	</div><!-- #content -->
    <?php get_footer(); ?>

    Address Function :

    ////////////// CREATE MAP USING ADDRESS FIELDS /////////////////////
    
    function create_map() {
    	global $post;
    	$street_arg = get_post_meta($post->ID, 'street' , TRUE);       // strret
    	$apt_arg = get_post_meta($post->ID, 'apt' , TRUE);      //apt
    	$city_arg = get_post_meta($post->ID, 'city' , TRUE); 	       // city
    	$state_arg = get_post_meta($post->ID, 'state' , TRUE);         // state
    	$zip_arg = get_post_meta($post->ID, 'zipcode' , TRUE);        // zip code
    	$address_arg = $street_arg . " " . $apt_arg . " " . $city_arg . " " . $state_arg . " " . $zip_arg;  // 
    
    	$old_address_arg = get_post_meta($post->ID, 'Address' , TRUE); 	// get old address
    	if ($old_address_arg != $address_arg) 				// compare old address to new address
    	delete_post_meta($post->ID, agm_map_created); 			// delete old address
    	update_post_meta($post->ID, 'address' , $address_arg); 		// update new address
    }
    add_action('edit_post', 'create_map');
    
    ////////////////////////////////////////////////////////////////////////////////

    How can i get the button that bring up the map into my form?

  • fitoussi

    Thanks Johnny,
    But i am not sure if you got me right. The form is just like a simple form. i didn't get the map to be displayed in my form which is actually what i am trying to do. right now all i have are the fields for the address that will generate the map after the post being created. anyway, i attached the screenshot of the form. (link will get you to under construction page.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.