Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Google Maps für Einsatzorte #114

Open
wants to merge 13 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Einsatzorte können mittels Google Maps API dargestellt werden.
In den Einstellungen kann dies (de)aktiviert werden.
Zusätzliches Feld mit Koordinate, welche via Button bestimmt wird.
Im Admin kann der Marker verschoben werden.
  • Loading branch information
jhbriese committed Oct 23, 2016
commit 05ea58cd23c046b5e2d32adcab73fd31d596da02
13 changes: 13 additions & 0 deletions src/Model/IncidentReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ public static function getMetaFields()
'einsatz_einsatzort' => array(
'label' => 'Einsatzort'
),
'einsatz_location' => array(
'label' => 'Goolemaps Position'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Google legt wert auf eine korrekte Schreibweise der Produkte: Google Maps anstatt Googlemaps, sowieso ist da ein Typo drin (das g fehlt bei Google).

),
'einsatz_einsatzleiter' => array(
'label' => 'Einsatzleiter'
),
Expand Down Expand Up @@ -194,6 +197,16 @@ public function getLocation()
return $this->getPostMeta('einsatz_einsatzort');
}

/**
* Gibt den eingetragenen Einsatzort als googlemaps koordinate zurück
*
* @return string
*/
public function getGmapsLocation()
{
return $this->getPostMeta('einsatz_location');
}

/**
* Gibt die Einsatznummer zurück
*
Expand Down
97 changes: 95 additions & 2 deletions src/einsatzverwaltung-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ class Admin
*/
private $core;

/**
* @var Options
*/
private $options;

/**
* @var Utilities
*/
Expand All @@ -23,11 +28,13 @@ class Admin
* Constructor
*
* @param Core $core
* @param Options $options
* @param Utilities $utilities
*/
public function __construct($core, $utilities)
public function __construct($core, $options, $utilities)
{
$this->core = $core;
$this->options = $options;
$this->utilities = $utilities;
$this->addHooks();
}
Expand Down Expand Up @@ -124,6 +131,8 @@ public function enqueueEditScripts($hook)
array(),
Core::VERSION
);

wp_enqueue_script( 'einsatzvw_GoogleMap' );
}

/**
Expand Down Expand Up @@ -167,10 +176,11 @@ public function displayMetaBoxEinsatzdetails($post)
$einsatzort = $report->getLocation();
$einsatzleiter = $report->getIncidentCommander();
$mannschaftsstaerke = $report->getWorkforce();
$gmapslocation = $report->getGmapsLocation();

$names = Data::getEinsatzleiterNamen();
echo '<input type="hidden" id="einsatzleiter_used_values" value="' . implode(',', $names) . '" />';
echo '<table><tbody>';
echo '<table style="width: 100%;"><tbody>';

$this->echoInputText(
'Einsatznummer',
Expand Down Expand Up @@ -202,6 +212,11 @@ public function displayMetaBoxEinsatzdetails($post)
esc_attr($einsatzort)
);

if($this->options->isGMapActivate())
{
$this->echoGMap($gmapslocation);
}

$this->echoInputText(
'Einsatzleiter',
'einsatzverwaltung_einsatzleiter',
Expand Down Expand Up @@ -236,6 +251,84 @@ private function echoInputText($label, $name, $value, $placeholder = '', $size =
echo '/></td></tr>';
}

/**
* Generiert eine Googlemaps Karte zur bestimmung der Koordinate
*/
private function echoGMap($location)
{
if($location != "")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wäre empty() an dieser Stelle nicht besser geeignet?

{
$latLon = explode(",",$location);
}
else
{
$latLon = array("","");
}
$DeflatLon = explode(",",$this->options->getGMapDefaultPos());

echo '<tr><td>';
echo '<label for="einsatzverwaltung_location">Koordinate</label>';
echo '</td><td style="width: 100%;">';
echo '<input type="text" id="einsatzverwaltung_location" name="einsatzverwaltung_location" value="' . $location . '" size="20" readonly/>';
echo '<a class="button" id="einsatzverwaltung_get_location"><i class="fa fa-map-marker"></i></a>';
echo '</td></tr>';
echo '<tr><td colspan="2">';
echo '<div id="map-canvas" style="height: 400px;"></div>';
echo '</td></tr>';
echo '<script>';
echo 'function initializeMap() {';
echo 'var defLat = "' . $DeflatLon[0] . '";';
echo 'var defLon = "' . $DeflatLon[1] . '";';
echo 'var latlon = new google.maps.LatLng(defLat, defLon);';
echo 'var mapOptions = {';
echo 'zoom: 13,';
echo 'center: latlon,';
echo 'scaleControl: true';
echo '};';
echo 'map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);';
echo 'var lat = "' . $latLon[0] . '";';
echo 'var lon = "' . $latLon[1] . '";';
echo 'if( lat != "" && lon != "" ) {';
echo 'setMarker( new google.maps.LatLng(lat, lon), map );';
echo '}';
echo 'var geocoder = new google.maps.Geocoder();';
echo 'document.getElementById("einsatzverwaltung_get_location").addEventListener("click", function() {';
echo 'var address = document.getElementById("einsatzverwaltung_einsatzort").value;';
echo 'if( address ) {';
echo 'geocodeAddress(geocoder, map, address);';
echo '} else {';
echo 'alert("Keine Addresse angegebenen: " + status);';
echo '}';
echo '});';
echo '}';
echo 'function setMarker( position, resultsMap ) {';
echo 'var marker = new google.maps.Marker({';
echo 'map: resultsMap,';
echo 'position: position,';
echo 'draggable: true,';
echo 'scaleControl:true';
echo '});';
echo 'resultsMap.setCenter(marker.getPosition());';
echo 'document.getElementById("einsatzverwaltung_location").value=marker.getPosition().toUrlValue();';
echo 'marker.addListener("drag", function(){';
echo 'document.getElementById("einsatzverwaltung_location").value=marker.getPosition().toUrlValue();';
echo '});';
echo '}';
echo 'function geocodeAddress(geocoder, resultsMap, address) {';
echo 'geocoder.geocode({"address": address}, function(results, status) {';
echo 'if (status === google.maps.GeocoderStatus.OK) {';
echo 'var location = results[0].geometry.location;';
echo 'setMarker( location, resultsMap);';
echo 'resultsMap.setZoom(16);';
echo '} else {';
echo 'alert("Konnte Position nicht ermitteln: " + status);';
echo '}';
echo '});';
echo '}';
echo 'google.maps.event.addDomListener(window, "load", initializeMap);';
echo '</script>';
}

/**
* Gibt eine Checkbox für die Metabox aus
*
Expand Down
22 changes: 19 additions & 3 deletions src/einsatzverwaltung-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,12 @@ class Core
* @var Data
*/
private $data;

/**
* @var Options
*/
private $options;

/**
* @var Utilities
*/
Expand All @@ -261,7 +261,7 @@ public function __construct()
$this->options = new Options($this->utilities);
$this->utilities->setDependencies($this->options);

new Admin($this, $this->utilities);
new Admin($this, $this->options, $this->utilities);
$this->data = new Data($this, $this->utilities, $this->options);
new Frontend($this, $this->options, $this->utilities);
new Settings($this, $this->options, $this->utilities, $this->data);
Expand Down Expand Up @@ -323,6 +323,7 @@ public function onDeactivation()
public function onInit()
{
$this->registerTypes();
$this->registerScripts();
$this->addRewriteRules();
if ($this->options->isFlushRewriteRules()) {
flush_rewrite_rules();
Expand Down Expand Up @@ -354,6 +355,21 @@ private function registerTypes()
register_taxonomy('alarmierungsart', 'einsatz', $this->argsAlarmierungsart);
}

/**
* Registriert externe Scripts
*/
private function registerScripts()
{
if( $this->options->isGMapActivate() ) {
/* Google Maps */
$protocal = is_ssl() ? 'https://' : 'http:https://';
$url = add_query_arg( array(
'key' => $this->options->getGMapAPI(),
), "{$protocal}maps.googleapis.com/maps/api/js");
wp_register_script( 'einsatzvw_GoogleMap', $url );
}
}

private function addRewriteRules()
{
global $wp_rewrite;
Expand Down
6 changes: 5 additions & 1 deletion src/einsatzverwaltung-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ public function savePostdata($postId, $post)
// Einsatzort validieren
$einsatzort = sanitize_text_field($_POST['einsatzverwaltung_einsatzort']);

// Einsatzposition validieren
$location = sanitize_text_field($_POST['einsatzverwaltung_location']);

// Einsatzleiter validieren
$einsatzleiter = sanitize_text_field($_POST['einsatzverwaltung_einsatzleiter']);

Expand All @@ -214,6 +217,7 @@ public function savePostdata($postId, $post)
// Metadaten schreiben
update_post_meta($postId, 'einsatz_einsatzende', $einsatzende);
update_post_meta($postId, 'einsatz_einsatzort', $einsatzort);
update_post_meta($postId, 'einsatz_location', $location);
update_post_meta($postId, 'einsatz_einsatzleiter', $einsatzleiter);
update_post_meta($postId, 'einsatz_mannschaft', $mannschaftsstaerke);
update_post_meta($postId, 'einsatz_fehlalarm', $fehlalarm);
Expand Down Expand Up @@ -283,7 +287,7 @@ public function onPublish($postId, $post)
$this->utilities->removePostFromCategory($postId, $category);
}
}

// Zwischenspeicher wird nur in der Entwurfsphase benötigt
delete_post_meta($postId, '_einsatz_timeofalerting');
}
Expand Down
53 changes: 50 additions & 3 deletions src/einsatzverwaltung-frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Frontend
* @var Options
*/
private $options;

/**
* @var Utilities
*/
Expand Down Expand Up @@ -69,6 +69,7 @@ public function enqueueStyleAndScripts()
Core::VERSION
);
wp_add_inline_style('einsatzverwaltung-frontend', ReportList::getDynamicCss());
wp_enqueue_script( 'einsatzvw_GoogleMap' );
}

/**
Expand Down Expand Up @@ -151,6 +152,52 @@ public function getEinsatzberichtHeader($post, $mayContainLinks = true, $showArc
return "";
}

/**
* Erzeugt eine Google Map des Einsatzortes
*
* @return string Code zum Erzeugen der Google-Map mit Markierten einsatzort
*/
public function getEinsatzberichtMap($post)
{
$report = new IncidentReport($post);
$location = $report->getGmapsLocation();
if($this->options->isGMapActivate() && $location)
{
$latLon = explode(",",$location);
$mapstring = "<style>#map-canvas {height: 300px; position: relative; overflow: hidden; transform: translateZ(0px); background-color: rgb(229, 227, 223);}</style>";
//$mapstring .= "<script type='text/javascript' src='https://maps.googleapis.com/maps/api/js?key=AIzaSyD2R-hm2pLaedH1YOlBs2uQwQn8xfK_hgo'></script>";
$mapstring .= "<script>";
$mapstring .= "var map;";
$mapstring .= "function initialize() {";
$mapstring .= " var latLon = new google.maps.LatLng(" . $latLon[0] . "," . $latLon[1] . ");";
$mapstring .= " var mapOptions = {";
$mapstring .= " zoom: 13,";
$mapstring .= " center: latLon";
$mapstring .= " };";
$mapstring .= " map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); ";
$mapstring .= " var marker = new google.maps.Marker({";
$mapstring .= " map: map,";
$mapstring .= " position: latLon";
$mapstring .= " });";
$mapstring .= " var contentString = '" . $report->getLocation() . "';";
$mapstring .= " var infowindow = new google.maps.InfoWindow({";
$mapstring .= " content: contentString";
$mapstring .= " });";
$mapstring .= " marker.addListener('click', function() {";
$mapstring .= " infowindow.open(map, marker);";
$mapstring .= " });";
$mapstring .= " infowindow.open(map, marker);";
$mapstring .= "}";
$mapstring .= "google.maps.event.addDomListener(window, 'load', initialize);";
$mapstring .= "</script>";
$mapstring .= "<div class='einsatzliste-map'>";
$mapstring .= "<div id='map-canvas'></div>";
$mapstring .= "</div>";
$mapstring .= "<div style='clear:both'></div>";

return "<p>$mapstring</p>";
}
}

/**
* Erzeugt eine Zeile für die Einsatzdetails
Expand Down Expand Up @@ -193,7 +240,7 @@ public function renderContent($content)
$header = $this->getEinsatzberichtHeader($post, true, true);
$content = $this->prepareContent($content);

return $header . '<hr>' . $content;
return $header . $this->getEinsatzberichtMap($post) . '<hr>' . $content;
}


Expand Down Expand Up @@ -319,7 +366,7 @@ public function addReportsToQuery($query)
} else {
$postTypes = array('post');
}

// Einsatzberichte nur zusammen mit Beiträgen abfragen
if (!in_array('post', $postTypes)) {
return;
Expand Down
30 changes: 30 additions & 0 deletions src/einsatzverwaltung-options.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class Options
'einsatzvw_flush_rewrite_rules' => false,
'einsatzvw_category' => false,
'einsatzvw_loop_only_special' => false,
'einsatzvw_gmap' => false,
'einsatzvw_gmap_api' => '',
'einsatzvw_gmap_default_pos' => '53.523463,9.482329',
);

/**
Expand Down Expand Up @@ -151,6 +154,33 @@ public function getExcerptTypeFeed()
return $this->utilities->sanitizeExcerptType($option);
}

/**
* @return string
*/
public function getGMapAPI()
{
$option = $this->getOption('einsatzvw_gmap_api');
return $option;
}

/**
* @return string
*/
public function getGMapDefaultPos()
{
$option = $this->getOption('einsatzvw_gmap_default_pos');
return $option;
}

/**
* @return bool
*/
public function isGMapActivate()
{
$option = $this->getOption('einsatzvw_gmap');
return $this->toBoolean($option);
}

/**
* Gibt die Basis für die URL zu Einsatzberichten zurück
*
Expand Down
Loading