new locations array organisation

This commit is contained in:
lenovo
2022-11-05 21:53:00 +01:00
parent 9424cca52e
commit 6fc3c78b71
10 changed files with 144 additions and 73 deletions

View File

@@ -2,7 +2,11 @@
# MAP
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)
- 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)
- [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 references](https://developers.google.com/maps/documentation/javascript/reference)
---

View File

@@ -10,6 +10,8 @@ Author URI:
/**
* inclusions :
*/
@@ -23,6 +25,8 @@ require_once(dirname(__FILE__) . '/settings/mp_optionnals.php');
/**
* global variables :
*/
@@ -50,6 +54,11 @@ $mp_cluster_size_factor = 2.5;
if (isset($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,
* 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
*/
function post_published_coordinates($id, $post) {
$coordinates = mp_get_coordinates($id);
if ( ! add_post_meta( $id, 'coordinates', $coordinates, true ) ) {
$coordinates_valid = true;
$coordinates = mp_get_coordinates($id, $coordinates_valid);
if ( ! add_post_meta( $id, 'coordinates', $coordinates, true ) )
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 );

View File

@@ -12,20 +12,18 @@ function mp_php_to_js($php_var, $js_var_name) {
function mp_add_to_scripts() {
global $mp_icon_size;
global $mp_icon_url;
global $mp_icon_cluster_url;
global $mp_icon_color;
global $mp_cluster_size_factor;
$events_unsorted = mp_get_published_events();
// $events = mp_sort_events($events_unsorted);
$events = $events_unsorted;
global $mp_coordinates_default;
$events = mp_get_published_events();
$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($mp_icon_url, 'icon_url'), '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($events, 'events'), '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_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_cluster_size_factor, 'cluster_size_factor'), 'before');
wp_add_inline_script('mp_init_map', mp_php_to_js($mp_coordinates_default, 'coordinates_default'), 'before');
}
?>

View File

@@ -1,11 +1,8 @@
<?php
// https://stackify.com/how-to-log-to-console-in-php/
function mp_console_log($output, $with_script_tags = true) {
$js_code = 'console.log(' . json_encode($output, JSON_HEX_TAG) .
');';
if ($with_script_tags) {
$js_code = '<script>' . $js_code . '</script>';
}
function mp_console_log($output) {
$js_code = 'console.log(' . json_encode($output, JSON_HEX_TAG) . ');';
$js_code = '<script>' . $js_code . '</script>';
echo $js_code;
}
?>

View File

@@ -1,36 +1,52 @@
<?php
function mp_get_coordinates($id) {
function mp_get_coordinates($id, &$valid) {
global $mp_api_key;
global $mp_coordinates_default;
$event = (object)[];
$errors = 0;
$coordinates = null;
// get address informations
// get address
$fields = array(
"adresse",
"ville",
"pays",
);
$event = (object)[];
foreach($fields as $field) {
$address_part = get_field($field, $id);
if ($address_part == '')
return null;
$event->$field = $address_part;
$address_section = get_field($field, $id);
$event->$field = $address_section;
if (strlen($address_section) == 0)
$errors++;
}
// check errors
if ($errors > 0)
$valid = false;
else
$valid = true;
// concat as an address
$address = $event->adresse . ","
. $event->ville . ","
. $event->pays;
// get coordinates from google maps api
$geolocation = 'https://maps.googleapis.com/maps/api/geocode/json'
. '?address=' . urlencode($address)
. '&key=' . $mp_api_key;
$jsoncontent = file_get_contents($geolocation);
if ($errors < 3) {
$geolocation = 'https://maps.googleapis.com/maps/api/geocode/json'
. '?address=' . urlencode($address)
. '&key=' . $mp_api_key;
$jsoncontent = file_get_contents($geolocation);
// extract coordinates from json
$content = json_decode($jsoncontent);
$coordinates = $content->results[0]->geometry->location;
}
// if null, set to default (by default, france)
if ($coordinates == null)
$coordinates = $mp_coordinates_default;
// extract coordinates from json
$content = json_decode($jsoncontent);
$coordinates = $content->results[0]->geometry->location;
return $coordinates;
}

View File

@@ -40,6 +40,11 @@
34: "adresse_courriel"
--- 35: "prenom"
--- 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);
// foreach ($post_list as $post) {
// wp_update_post(array(
// 'ID' => $post->ID,
// //'post_status' => 'draft',
// 'post_status' => 'publish',
// ));
// }
// //if ( get_field("nom", $post->ID) == "Rakhimov") {
// wp_update_post(array(
// 'ID' => $post->ID,
// //'post_status' => 'draft',
// 'post_status' => 'publish',
// ));
// //};
// };
$get_posts_args = array(
'numberposts' => -1,
@@ -126,6 +133,8 @@ function mp_fill_fields_value($id) {
* get_field is an ACF function
* in "pure" worpdress use :
* 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
@@ -140,6 +149,7 @@ function mp_fill_fields_value($id) {
"prenom",
"nom",
"coordinates",
"coordinates_valid",
);
$event = (object)[];
foreach($fields as $field) {
@@ -162,14 +172,8 @@ function mp_get_published_events() {
$event->id = $post->ID;
$event->title = $post->post_title;
$event = mp_fill_fields_value($post->ID);
// only add to array if address is complete
if ( isset($event->coordinates) )
array_push($events, $event);
else
mp_console_log("event address incomplete:");
mp_console_log($event);
array_push($events, $event);
}
mp_console_log($events);
return $events;
}

View File

@@ -1,12 +1,44 @@
<?php
//function mp_sort_events($events_unsorted) {
//
// if ( isset($event->coordinates) )
// array_push($events, $event);
// else
// mp_console_log("event address incomplete:");
// mp_console_log($event);
//}
function mp_coord_already_exist(&$coordinates, &$locations) {
foreach ($locations as $location) {
if ($location->coordinates->lat == $coordinates->lat)
if ($location->coordinates->lng == $coordinates->lng)
return $location;
}
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;
}
?>

View File

@@ -2,9 +2,14 @@ function mp_init_map() {
/*
* following variable are created by mp_add_to_script.php
* - let events = [{}, {}...]
* - let icon_url = ""
* - let icon_cluster_url = ""
* - let locations = [
* {
* coord: {}
* events: [{}, ...]
* },
* ...
* ]
* - let coordinates_default = {lat: ,lng: }
* - let icon_color = ""
* - let icon_size = [x, y]
* - let cluster_size_factor = Number
@@ -15,10 +20,11 @@ function mp_init_map() {
*/
//print_error("error");
console.log("hello");
console.log("locations:");
console.log(locations);
// default map center to france
let map_center = {lat:46.227638, lng:2.213749};
let map_center = coordinates_default;
let map_options = {
/* map options : https://developers.google.com/maps/documentation/javascript/reference/map#MapOptions */
disableDefaultUI: true,
@@ -33,7 +39,6 @@ function mp_init_map() {
//draggable: "true", //deprecated
center: map_center,
}
let svg_icon = window.btoa(`
<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" />
@@ -44,12 +49,6 @@ function mp_init_map() {
url: `data:image/svg+xml;base64,${svg_icon}`,
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 markers = [];
for (ev of events) {
for (loc of locations) {
marker = new google.maps.Marker({
position: ev.coordinates,
position: loc.coordinates,
map: map,
icon: icon_options,
//label: label_options,
});
markers.push(marker);
};

View File

@@ -29,4 +29,13 @@
//$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
?>

View File

@@ -7,9 +7,4 @@
/* cle api de google maps */
$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';
?>