new locations array organisation
This commit is contained in:
@@ -2,7 +2,11 @@
|
|||||||
# MAP
|
# MAP
|
||||||
|
|
||||||
todo:
|
todo:
|
||||||
- redisgn + and - for zoom -> color and thickness
|
- gather same-places events
|
||||||
|
- add info-window
|
||||||
|
- add filter options
|
||||||
|
- deal with bad address
|
||||||
|
- redsign + and - for zoom -> color and thickness
|
||||||
|
|
||||||
- [changed plugin directory in wp](https://wordpress.stackexchange.com/questions/120075/how-to-change-location-of-the-plugins-wordpress-themes-folder)
|
- [changed plugin directory in wp](https://wordpress.stackexchange.com/questions/120075/how-to-change-location-of-the-plugins-wordpress-themes-folder)
|
||||||
- googlemap api key : AIzaSyCvdGV2ssD4ov4a9CuIlQhoJyz5gWWiSvE
|
- googlemap api key : AIzaSyCvdGV2ssD4ov4a9CuIlQhoJyz5gWWiSvE
|
||||||
@@ -19,6 +23,7 @@ todo:
|
|||||||
- [my post on wordpress stack](https://wordpress.stackexchange.com/questions/410735/i-dont-understand-how-symlinks-in-plugin-work)
|
- [my post on wordpress stack](https://wordpress.stackexchange.com/questions/410735/i-dont-understand-how-symlinks-in-plugin-work)
|
||||||
- [maps api in php](http://www.learningaboutelectronics.com/Articles/Google-maps-API-JSON-PHP.php)
|
- [maps api in php](http://www.learningaboutelectronics.com/Articles/Google-maps-API-JSON-PHP.php)
|
||||||
- [google maps api url parameters](https://developers.google.com/maps/documentation/javascript/url-params)
|
- [google maps api url parameters](https://developers.google.com/maps/documentation/javascript/url-params)
|
||||||
|
- [google maps api references](https://developers.google.com/maps/documentation/javascript/reference)
|
||||||
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ Author URI:
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* inclusions :
|
* inclusions :
|
||||||
*/
|
*/
|
||||||
@@ -23,6 +25,8 @@ require_once(dirname(__FILE__) . '/settings/mp_optionnals.php');
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* global variables :
|
* global variables :
|
||||||
*/
|
*/
|
||||||
@@ -50,6 +54,11 @@ $mp_cluster_size_factor = 2.5;
|
|||||||
if (isset($mp_settings_cluster_size_factor))
|
if (isset($mp_settings_cluster_size_factor))
|
||||||
$mp_cluster_size_factor = $mp_settings_cluster_size_factor;
|
$mp_cluster_size_factor = $mp_settings_cluster_size_factor;
|
||||||
|
|
||||||
|
$mp_coordinates_default = (object)["lat" => 46.227638, "lng" => 2.213749]; // france
|
||||||
|
if (isset($mp_settings_coordinates_default))
|
||||||
|
$mp_coordinates_default = $mp_settings_coordinates_default;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -65,6 +74,8 @@ add_filter('script_loader_tag', 'mp_tag_scripts', 10, 2);
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* when 'shortcode' found in page, enqueue scripts and styles,
|
* when 'shortcode' found in page, enqueue scripts and styles,
|
||||||
* run php script, and replace shortcode by return value
|
* run php script, and replace shortcode by return value
|
||||||
@@ -91,14 +102,20 @@ add_shortcode('lejourduprof_map', 'mp_add_div');
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* when a post is published, check its coordinates
|
* when a post is published, check its coordinates
|
||||||
*/
|
*/
|
||||||
function post_published_coordinates($id, $post) {
|
function post_published_coordinates($id, $post) {
|
||||||
$coordinates = mp_get_coordinates($id);
|
$coordinates_valid = true;
|
||||||
if ( ! add_post_meta( $id, 'coordinates', $coordinates, true ) ) {
|
$coordinates = mp_get_coordinates($id, $coordinates_valid);
|
||||||
|
|
||||||
|
if ( ! add_post_meta( $id, 'coordinates', $coordinates, true ) )
|
||||||
update_post_meta( $id, 'coordinates', $coordinates );
|
update_post_meta( $id, 'coordinates', $coordinates );
|
||||||
}
|
|
||||||
|
if ( ! add_post_meta( $id, 'coordinates_valid', $coordinates_valid, true ) )
|
||||||
|
update_post_meta( $id, 'coordinates_valid', $coordinates_valid );
|
||||||
}
|
}
|
||||||
add_action( 'publish_post', 'post_published_coordinates', 10, 2 );
|
add_action( 'publish_post', 'post_published_coordinates', 10, 2 );
|
||||||
|
|
||||||
|
|||||||
@@ -12,20 +12,18 @@ function mp_php_to_js($php_var, $js_var_name) {
|
|||||||
|
|
||||||
function mp_add_to_scripts() {
|
function mp_add_to_scripts() {
|
||||||
global $mp_icon_size;
|
global $mp_icon_size;
|
||||||
global $mp_icon_url;
|
|
||||||
global $mp_icon_cluster_url;
|
|
||||||
global $mp_icon_color;
|
global $mp_icon_color;
|
||||||
global $mp_cluster_size_factor;
|
global $mp_cluster_size_factor;
|
||||||
$events_unsorted = mp_get_published_events();
|
global $mp_coordinates_default;
|
||||||
// $events = mp_sort_events($events_unsorted);
|
$events = mp_get_published_events();
|
||||||
$events = $events_unsorted;
|
$locations = mp_sort_events($events);
|
||||||
|
|
||||||
wp_add_inline_script('mp_init_map', mp_php_to_js($events, 'events'), 'before');
|
//wp_add_inline_script('mp_init_map', mp_php_to_js($events, 'events'), 'before');
|
||||||
wp_add_inline_script('mp_init_map', mp_php_to_js($mp_icon_url, 'icon_url'), 'before');
|
wp_add_inline_script('mp_init_map', mp_php_to_js($locations, 'locations'), 'before');
|
||||||
wp_add_inline_script('mp_init_map', mp_php_to_js($mp_icon_cluster_url, 'icon_cluster_url'), 'before');
|
|
||||||
wp_add_inline_script('mp_init_map', mp_php_to_js($mp_icon_size, 'icon_size'), 'before');
|
wp_add_inline_script('mp_init_map', mp_php_to_js($mp_icon_size, 'icon_size'), 'before');
|
||||||
wp_add_inline_script('mp_init_map', mp_php_to_js($mp_icon_color, 'icon_color'), 'before');
|
wp_add_inline_script('mp_init_map', mp_php_to_js($mp_icon_color, 'icon_color'), 'before');
|
||||||
wp_add_inline_script('mp_init_map', mp_php_to_js($mp_cluster_size_factor, 'cluster_size_factor'), 'before');
|
wp_add_inline_script('mp_init_map', mp_php_to_js($mp_cluster_size_factor, 'cluster_size_factor'), 'before');
|
||||||
|
wp_add_inline_script('mp_init_map', mp_php_to_js($mp_coordinates_default, 'coordinates_default'), 'before');
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|||||||
@@ -1,11 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
// https://stackify.com/how-to-log-to-console-in-php/
|
// https://stackify.com/how-to-log-to-console-in-php/
|
||||||
function mp_console_log($output, $with_script_tags = true) {
|
function mp_console_log($output) {
|
||||||
$js_code = 'console.log(' . json_encode($output, JSON_HEX_TAG) .
|
$js_code = 'console.log(' . json_encode($output, JSON_HEX_TAG) . ');';
|
||||||
');';
|
|
||||||
if ($with_script_tags) {
|
|
||||||
$js_code = '<script>' . $js_code . '</script>';
|
$js_code = '<script>' . $js_code . '</script>';
|
||||||
}
|
|
||||||
echo $js_code;
|
echo $js_code;
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
|||||||
@@ -1,28 +1,38 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
function mp_get_coordinates($id) {
|
function mp_get_coordinates($id, &$valid) {
|
||||||
global $mp_api_key;
|
global $mp_api_key;
|
||||||
|
global $mp_coordinates_default;
|
||||||
|
$event = (object)[];
|
||||||
|
$errors = 0;
|
||||||
|
$coordinates = null;
|
||||||
|
|
||||||
// get address informations
|
// get address
|
||||||
$fields = array(
|
$fields = array(
|
||||||
"adresse",
|
"adresse",
|
||||||
"ville",
|
"ville",
|
||||||
"pays",
|
"pays",
|
||||||
);
|
);
|
||||||
$event = (object)[];
|
|
||||||
foreach($fields as $field) {
|
foreach($fields as $field) {
|
||||||
$address_part = get_field($field, $id);
|
$address_section = get_field($field, $id);
|
||||||
if ($address_part == '')
|
$event->$field = $address_section;
|
||||||
return null;
|
if (strlen($address_section) == 0)
|
||||||
$event->$field = $address_part;
|
$errors++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check errors
|
||||||
|
if ($errors > 0)
|
||||||
|
$valid = false;
|
||||||
|
else
|
||||||
|
$valid = true;
|
||||||
|
|
||||||
// concat as an address
|
// concat as an address
|
||||||
$address = $event->adresse . ","
|
$address = $event->adresse . ","
|
||||||
. $event->ville . ","
|
. $event->ville . ","
|
||||||
. $event->pays;
|
. $event->pays;
|
||||||
|
|
||||||
// get coordinates from google maps api
|
// get coordinates from google maps api
|
||||||
|
if ($errors < 3) {
|
||||||
$geolocation = 'https://maps.googleapis.com/maps/api/geocode/json'
|
$geolocation = 'https://maps.googleapis.com/maps/api/geocode/json'
|
||||||
. '?address=' . urlencode($address)
|
. '?address=' . urlencode($address)
|
||||||
. '&key=' . $mp_api_key;
|
. '&key=' . $mp_api_key;
|
||||||
@@ -31,6 +41,12 @@ function mp_get_coordinates($id) {
|
|||||||
// extract coordinates from json
|
// extract coordinates from json
|
||||||
$content = json_decode($jsoncontent);
|
$content = json_decode($jsoncontent);
|
||||||
$coordinates = $content->results[0]->geometry->location;
|
$coordinates = $content->results[0]->geometry->location;
|
||||||
|
}
|
||||||
|
|
||||||
|
// if null, set to default (by default, france)
|
||||||
|
if ($coordinates == null)
|
||||||
|
$coordinates = $mp_coordinates_default;
|
||||||
|
|
||||||
return $coordinates;
|
return $coordinates;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -40,6 +40,11 @@
|
|||||||
34: "adresse_courriel"
|
34: "adresse_courriel"
|
||||||
--- 35: "prenom"
|
--- 35: "prenom"
|
||||||
--- 36: "nom"
|
--- 36: "nom"
|
||||||
|
|
||||||
|
ADDED BY THIS PLUGIN
|
||||||
|
|
||||||
|
--- 37: "coordinates"
|
||||||
|
--- 38: "coordinates_valid"
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -102,12 +107,14 @@ function mp_get_published_posts() {
|
|||||||
// );
|
// );
|
||||||
// $post_list = get_posts($post_args);
|
// $post_list = get_posts($post_args);
|
||||||
// foreach ($post_list as $post) {
|
// foreach ($post_list as $post) {
|
||||||
|
// //if ( get_field("nom", $post->ID) == "Rakhimov") {
|
||||||
// wp_update_post(array(
|
// wp_update_post(array(
|
||||||
// 'ID' => $post->ID,
|
// 'ID' => $post->ID,
|
||||||
// //'post_status' => 'draft',
|
// //'post_status' => 'draft',
|
||||||
// 'post_status' => 'publish',
|
// 'post_status' => 'publish',
|
||||||
// ));
|
// ));
|
||||||
// }
|
// //};
|
||||||
|
// };
|
||||||
|
|
||||||
$get_posts_args = array(
|
$get_posts_args = array(
|
||||||
'numberposts' => -1,
|
'numberposts' => -1,
|
||||||
@@ -126,6 +133,8 @@ function mp_fill_fields_value($id) {
|
|||||||
* get_field is an ACF function
|
* get_field is an ACF function
|
||||||
* in "pure" worpdress use :
|
* in "pure" worpdress use :
|
||||||
* get_post_meta or get_post_custom_values
|
* get_post_meta or get_post_custom_values
|
||||||
|
* - https://developer.wordpress.org/reference/functions/get_post_meta/
|
||||||
|
* - https://developer.wordpress.org/reference/functions/get_post_custom_values/
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// add fields
|
// add fields
|
||||||
@@ -140,6 +149,7 @@ function mp_fill_fields_value($id) {
|
|||||||
"prenom",
|
"prenom",
|
||||||
"nom",
|
"nom",
|
||||||
"coordinates",
|
"coordinates",
|
||||||
|
"coordinates_valid",
|
||||||
);
|
);
|
||||||
$event = (object)[];
|
$event = (object)[];
|
||||||
foreach($fields as $field) {
|
foreach($fields as $field) {
|
||||||
@@ -162,14 +172,8 @@ function mp_get_published_events() {
|
|||||||
$event->id = $post->ID;
|
$event->id = $post->ID;
|
||||||
$event->title = $post->post_title;
|
$event->title = $post->post_title;
|
||||||
$event = mp_fill_fields_value($post->ID);
|
$event = mp_fill_fields_value($post->ID);
|
||||||
// only add to array if address is complete
|
|
||||||
if ( isset($event->coordinates) )
|
|
||||||
array_push($events, $event);
|
array_push($events, $event);
|
||||||
else
|
|
||||||
mp_console_log("event address incomplete:");
|
|
||||||
mp_console_log($event);
|
|
||||||
}
|
}
|
||||||
mp_console_log($events);
|
|
||||||
return $events;
|
return $events;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,44 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
//function mp_sort_events($events_unsorted) {
|
function mp_coord_already_exist(&$coordinates, &$locations) {
|
||||||
//
|
foreach ($locations as $location) {
|
||||||
// if ( isset($event->coordinates) )
|
if ($location->coordinates->lat == $coordinates->lat)
|
||||||
// array_push($events, $event);
|
if ($location->coordinates->lng == $coordinates->lng)
|
||||||
// else
|
return $location;
|
||||||
// mp_console_log("event address incomplete:");
|
}
|
||||||
// mp_console_log($event);
|
return null;
|
||||||
//}
|
}
|
||||||
|
|
||||||
|
function mp_sort_n_insert(&$event, &$locations) {
|
||||||
|
$coordinates = $event->coordinates;
|
||||||
|
|
||||||
|
$already_exist = mp_coord_already_exist($coordinates, $locations);
|
||||||
|
if ($already_exist) {
|
||||||
|
// add event to events[]
|
||||||
|
array_push($already_exist->events, $event);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// create new location object
|
||||||
|
$location = (object)[];
|
||||||
|
$location->events = [];
|
||||||
|
|
||||||
|
// add coordinates to the location
|
||||||
|
$location->coordinates = $coordinates;
|
||||||
|
// add first event to events[]
|
||||||
|
array_push($location->events, $event);
|
||||||
|
// add this location to locations[]
|
||||||
|
array_push($locations, $location);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function mp_sort_events($events) {
|
||||||
|
$locations = [];
|
||||||
|
|
||||||
|
foreach ($events as $event) {
|
||||||
|
mp_sort_n_insert($event, $locations);
|
||||||
|
};
|
||||||
|
|
||||||
|
return $locations;
|
||||||
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|||||||
@@ -2,9 +2,14 @@ function mp_init_map() {
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* following variable are created by mp_add_to_script.php
|
* following variable are created by mp_add_to_script.php
|
||||||
* - let events = [{}, {}...]
|
* - let locations = [
|
||||||
* - let icon_url = ""
|
* {
|
||||||
* - let icon_cluster_url = ""
|
* coord: {}
|
||||||
|
* events: [{}, ...]
|
||||||
|
* },
|
||||||
|
* ...
|
||||||
|
* ]
|
||||||
|
* - let coordinates_default = {lat: ,lng: }
|
||||||
* - let icon_color = ""
|
* - let icon_color = ""
|
||||||
* - let icon_size = [x, y]
|
* - let icon_size = [x, y]
|
||||||
* - let cluster_size_factor = Number
|
* - let cluster_size_factor = Number
|
||||||
@@ -15,10 +20,11 @@ function mp_init_map() {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
//print_error("error");
|
//print_error("error");
|
||||||
console.log("hello");
|
console.log("locations:");
|
||||||
|
console.log(locations);
|
||||||
|
|
||||||
// default map center to france
|
// default map center to france
|
||||||
let map_center = {lat:46.227638, lng:2.213749};
|
let map_center = coordinates_default;
|
||||||
let map_options = {
|
let map_options = {
|
||||||
/* map options : https://developers.google.com/maps/documentation/javascript/reference/map#MapOptions */
|
/* map options : https://developers.google.com/maps/documentation/javascript/reference/map#MapOptions */
|
||||||
disableDefaultUI: true,
|
disableDefaultUI: true,
|
||||||
@@ -33,7 +39,6 @@ function mp_init_map() {
|
|||||||
//draggable: "true", //deprecated
|
//draggable: "true", //deprecated
|
||||||
center: map_center,
|
center: map_center,
|
||||||
}
|
}
|
||||||
|
|
||||||
let svg_icon = window.btoa(`
|
let svg_icon = window.btoa(`
|
||||||
<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">
|
<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">
|
||||||
<circle cx="50%" cy="50%" r="27%" stroke="#ba197a" stroke-width="26" fill="#ffffff80" />
|
<circle cx="50%" cy="50%" r="27%" stroke="#ba197a" stroke-width="26" fill="#ffffff80" />
|
||||||
@@ -44,12 +49,6 @@ function mp_init_map() {
|
|||||||
url: `data:image/svg+xml;base64,${svg_icon}`,
|
url: `data:image/svg+xml;base64,${svg_icon}`,
|
||||||
scaledSize: new google.maps.Size(icon_size[0], icon_size[1]),
|
scaledSize: new google.maps.Size(icon_size[0], icon_size[1]),
|
||||||
};
|
};
|
||||||
let label_options = {
|
|
||||||
//text: String(count),
|
|
||||||
text: "?",
|
|
||||||
//color: icon_label_color,
|
|
||||||
fontSize: "12px",
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -59,12 +58,11 @@ function mp_init_map() {
|
|||||||
|
|
||||||
let map = new google.maps.Map(document.getElementById("ljdp_map"), map_options);
|
let map = new google.maps.Map(document.getElementById("ljdp_map"), map_options);
|
||||||
let markers = [];
|
let markers = [];
|
||||||
for (ev of events) {
|
for (loc of locations) {
|
||||||
marker = new google.maps.Marker({
|
marker = new google.maps.Marker({
|
||||||
position: ev.coordinates,
|
position: loc.coordinates,
|
||||||
map: map,
|
map: map,
|
||||||
icon: icon_options,
|
icon: icon_options,
|
||||||
//label: label_options,
|
|
||||||
});
|
});
|
||||||
markers.push(marker);
|
markers.push(marker);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -29,4 +29,13 @@
|
|||||||
//$mp_settings_cluster_size_factor = 4;
|
//$mp_settings_cluster_size_factor = 4;
|
||||||
|
|
||||||
|
|
||||||
|
/* ************************************
|
||||||
|
coordonnees par defaut
|
||||||
|
(pour centrer la carte globale,
|
||||||
|
et en cas de mauvaises adresses)
|
||||||
|
valeur par defaut "france"
|
||||||
|
************************************ */
|
||||||
|
//$mp_settings_coordinates_default = {lat:35.746512, lng:-39.462891}; // oceant atlantique nord
|
||||||
|
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|||||||
@@ -7,9 +7,4 @@
|
|||||||
/* cle api de google maps */
|
/* cle api de google maps */
|
||||||
$mp_api_key = 'AIzaSyCvdGV2ssD4ov4a9CuIlQhoJyz5gWWiSvE';
|
$mp_api_key = 'AIzaSyCvdGV2ssD4ov4a9CuIlQhoJyz5gWWiSvE';
|
||||||
|
|
||||||
/* nom des fichiers image situe dans le dossier "images" */
|
|
||||||
/* pour les icones des marqueurs sur la carte */
|
|
||||||
$mp_icon_file = 'marker.png';
|
|
||||||
$mp_icon_cluster_file = 'marker_cluster.png';
|
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|||||||
Reference in New Issue
Block a user