Up to 50 percent off

Google map on header with all my companies

This topic contains 7 reply and 3 voices, and was last updated by noomia 11 years, 7 months ago
Viewing 7 Posts - 1 through 7 (of 7 total)
Author Posts
April 18, 2013 at 2:06 pm 2464
noomia Hi guys ! I'm trying to create something similar to this theme right now : http://preview.ait-themes.com/directory/wp1/ I have a custom post type wich is called "companies". The company creation is allowed on the front end and have a google map field. What I want is show a google map on the header of my site wich show all the companies thanks to the address users entered on the company front end form. Thanks a lot :) !
April 18, 2013 at 2:37 pm 2467
Tareq Hasan Tareq Hasan

You can use a shortcode: `[wpuf-meta name=”meta_key_name” type=”map”]` or the function `wpuf_shortcode_map( ‘meta_key’, $post->ID );`

April 18, 2013 at 3:01 pm 2472
noomia noomia

In fact, I need 1 maps, with all the companies … Is it doing the trick :s ?

April 18, 2013 at 3:09 pm 2475
noomia noomia

Ok I have figured it out… But I need to get only the lat value and the lng value of the google map field… How could I do that ?

April 18, 2013 at 3:15 pm 2477
Tareq Hasan Tareq Hasan

Sorry, I thought you just needed the map for one post. In that case, you need to get all posts and fetch the custom field. Lat and long are saved in the same custom field, separated by a comma. Using `explode( ‘,’, $value )` will give you array of lat and long.

April 18, 2013 at 3:37 pm 2480
noomia noomia

Thanks Tareq,

With a mix of this post and what you told me, I was able to achieve my goal 🙂 !

Thanks !

April 18, 2013 at 8:33 pm 2485
Mahi Mahi

Hello @noomia,

If possible please share your solution with us 🙂 ?

Cheers.

April 19, 2013 at 8:09 am 2523
noomia noomia

Hi !

So, according to this post and what Tareq told me, here is what I did :

First of all, put the following html code where you want to have your Google Map (I choose the header.php file) :
`

`

Then, you need to create a .js file that will initialize the google maps. Let’s call it maps.js :

`
(function($) {
function initialise_map() {
var locations_str = php_args.locations.replace(/”/g, ‘”‘);
var locations = $.parseJSON(locations_str);

var latlng = new google.maps.LatLng(48.856667, 2.350833);
var myOptions = {
zoom: 2,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};

var map = new google.maps.Map(document.getElementById(“map_canvas”), myOptions);

var bounds = new google.maps.LatLngBounds();
for (var i = 0; i < locations.length; i++) { addMarker(locations[i], map, bounds); } if (locations.length > 1) {
map.fitBounds(bounds);
}
}

function addMarker(location, map, bounds) {
var locationLatLng = new google.maps.LatLng(location.latitude,location.longitude);
bounds.extend(locationLatLng);
var marker = new google.maps.Marker({
position: locationLatLng,
map: map,
title:location.title
});
}

$(function() {
initialise_map();
});

})(jQuery);
`
The `getElementById(“map_canvas”)` part refers to the `

` which will contains the Google Map.

Now, this is the important part. In your function.php file, put these lines of code :
`
/**
* Google Map
*
*/
define(‘GOOGLE_MAPS_V3_API_KEY’, ‘XXXXXXXXXXXX’);

add_action(‘wp_enqueue_scripts’, ‘companies_map_scripts’);

function companies_map_scripts() {
wp_register_script(‘googlemaps’, ‘http://maps.googleapis.com/maps/api/js?hl=en&key=&#8217; . GOOGLE_MAPS_V3_API_KEY . ‘&sensor=false’, false, ‘3’);
wp_enqueue_script(‘googlemaps’);

wp_register_script( ‘maps’, get_bloginfo(‘stylesheet_directory’) . “/js/maps.js”, array(‘jquery’, ‘googlemaps’), false, false );
wp_enqueue_script(‘maps’);

$locations = array();

$location_query = new WP_Query( array(
‘post_type’ => ‘companies’,
‘posts_per_page’ => -1
) );

while ( $location_query->have_posts() ) {
$location_query->the_post();

$address = get_post_meta( get_the_ID(), ‘address’, true );
list( $def_lat, $def_long ) = explode( ‘,’, $address );

$locations[] = array(
‘title’ => get_the_title(),
‘latitude’ => $def_lat,
‘longitude’ => $def_long,
‘id’ => get_the_ID()
);
}

wp_reset_postdata();

wp_localize_script(‘maps’, ‘php_args’, array(
‘locations’ => json_encode($locations)
));
}
`

You have to change :

– `XXXXXXXXXXXX` : change it with your Google API Key.
– `wp_register_script( ‘maps’, get_bloginfo(‘stylesheet_directory’) . “/js/maps.js”, array(‘jquery’, ‘googlemaps’), false, false );` and `wp_enqueue_script(‘maps’);` : if you have called the script maps.js with an other name, change it there too.
– `’post_type’ => ‘companies’` : Put your custom post type name here.
– `$address = get_post_meta( get_the_ID(), ‘address’, true );` : Change address by the name of your google map field in your custom post type.

Finally, add these lines to your style.css :
`
/* =Maps
———————————————– */
#map_canvas {
width: 100%;
height: 452px;
}
`

Viewing 7 Posts - 1 through 7 (of 7 total)