Skip to content

Commit

Permalink
Merge pull request #2592 from phili67/phili67-maps-update
Browse files Browse the repository at this point in the history
Phili67 maps update
  • Loading branch information
phili67 committed Apr 7, 2024
2 parents b54430d + c511fda commit 1b25241
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 43 deletions.
20 changes: 12 additions & 8 deletions src/EcclesiaCRM/utils/GeoUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,21 @@ public static function getLatLong($address)
$logger->debug("Using: Geo Provider - ". $provider->getName());
$geoCoder = new StatefulGeocoder($provider, Bootstrapper::GetCurrentLocale()->getShortLocale());
$result = $geoCoder->geocodeQuery(GeocodeQuery::create($address));
$logger->debug("We have " . $result->count() . " results");
if (!empty($result)) {
$firstResult = $result->get(0);
$coordinates = $firstResult->getCoordinates();
$lat = $coordinates->getLatitude();
$long = $coordinates->getLongitude();
}
} catch (\Exception $exception) {
$logger->warn("issue creating geoCoder " . $exception->getMessage());
$logger->debug("issue creating geoCoder " . $exception->getMessage());
$provider = new Nominatim($adapter, SystemConfig::getValue("sNominatimLink"), SystemConfig::getValue("sChurchEmail") );
$geoCoder = new StatefulGeocoder($provider, Bootstrapper::GetCurrentLocale()->getShortLocale());
$result = $geoCoder->geocodeQuery(GeocodeQuery::create($address));
}

$logger->debug("We have " . $result->count() . " results");
if (!empty($result)) {
$firstResult = $result->get(0);
$coordinates = $firstResult->getCoordinates();
$lat = $coordinates->getLatitude();
$long = $coordinates->getLongitude();
}

return array(
'Latitude' => $lat,
'Longitude' => $long
Expand Down
38 changes: 21 additions & 17 deletions src/skin/js/calendar/GoogleMapEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,32 @@
if (val.which == 13) {
deleteMarker(marker);

var address = $('form #EventLocation').val();

$.ajax({
url:"https://maps.googleapis.com/maps/api/geocode/json?address="+address+"&sensor=false&key="+window.CRM.iGoogleMapKey,
type: "POST",
success:function(res){
var address = $('form #EventLocation').val();

fetch("https://maps.googleapis.com/maps/api/geocode/json?address="+address+"&sensor=false&key="+window.CRM.iGoogleMapKey, {
method: "POST"
})
.then(resj => resj.json())
.then(res => {
if (res.status == "ZERO_RESULTS") {
alert(i18next.t('Wrong address format.'));
$('form #EventLocation').val('');
return;
}
var latitude = res.results[0].geometry.location.lat;
var longitude = res.results[0].geometry.location.lng;
var EventTitle = $('form #EventTitle').val();
var EventDesc = $('form #EventDesc').val();
let latitude = res.results[0].geometry.location.lat;
let longitude = res.results[0].geometry.location.lng;
let EventTitle = $('form #EventTitle').val();
let EventDesc = $('form #EventDesc').val();

if ( latitude > 0 && longitude > 0 ) {
var Salutation = EventTitle + " ("+EventDesc+")";
var Name = EventTitle;
var latlng = new google.maps.LatLng(latitude, longitude);
let Salutation = EventTitle + " ("+EventDesc+")";
let Name = EventTitle;
let latlng = new google.maps.LatLng(latitude, longitude);

var imghref = window.CRM.root+"/v2/calendar";
let imghref = window.CRM.root+"/v2/calendar";
var iconurl = window.CRM.root+"/skin/icons/event.png";

var image = {
let image = {
url: iconurl,
// This marker is 37 pixels wide by 34 pixels high.
size: new google.maps.Size(37, 34),
Expand All @@ -63,8 +64,11 @@

window.CRM.map.setCenter(latlng);
}
}
});
})
.catch(error => {
// enter your logic for when there is an error (ex. error toast)
console.log(error)
});
}
});

Expand Down
38 changes: 20 additions & 18 deletions src/skin/js/calendar/OpenStreetMapEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,33 @@
var address = $('form #EventLocation').val();

var address_nomatim = address.replace(/ /g,'+');

$.ajax({
url:window.CRM.sNominatimLink+"/search?q="+address_nomatim+"&format=json",
type: "GET",
dataType: "json",
success:function(res){

fetch(window.CRM.sNominatimLink+"/search?q="+address_nomatim+"&format=json", {
method: "GET",
headers: {
"Content-Type": "application/json",
}
})
.then(resj => resj.json())
.then(res => {
if (res === undefined || res.length == 0) {
alert(i18next.t('Wrong address format.'));
return;
}

var latitude = res[0].lat;
var longitude = res[0].lon;
var EventTitle = $('form #EventTitle').val();
var EventDesc = $('form #EventDesc').val();
let latitude = res[0].lat;
let longitude = res[0].lon;
let EventTitle = $('form #EventTitle').val();
let EventDesc = $('form #EventDesc').val();

if ( latitude > 0 && longitude > 0 ) {
var Salutation = EventTitle + " ("+EventDesc+")";
var Name = EventTitle;
let Salutation = EventTitle + " ("+EventDesc+")";
let Name = EventTitle;

var imghref = window.CRM.root+"/v2/calendar";
var iconurl = window.CRM.root+"/skin/icons/event.png";
let imghref = window.CRM.root+"/v2/calendar";
let iconurl = window.CRM.root+"/skin/icons/event.png";

var icon = L.icon({
let icon = L.icon({
iconUrl: iconurl,
iconSize: [32, 32], // size of the icon
iconAnchor: [16, 32], // point of the icon which will correspond to marker's location
Expand All @@ -54,16 +57,15 @@
contentString = "<b><a href='" + imghref + "'>" + Salutation + "</a></b>";
contentString += "<p>" + address + "</p>";

var centerCard = {
let centerCard = {
lat: Number(latitude),
lng: Number(longitude)};

//Add marker and infowindow
marker = addMarkerWithInfowindow(window.CRM.map, centerCard, icon, Name, contentString);

window.CRM.map.setView([centerCard.lat, centerCard.lng], window.CRM.mapZoom);
}
}
}
});
}
});
Expand Down

0 comments on commit 1b25241

Please sign in to comment.