Skip to content

Commit

Permalink
fix(reverse): reverse not working when no address found
Browse files Browse the repository at this point in the history
  • Loading branch information
azarz committed Dec 8, 2023
1 parent ca28e06 commit 4f37c5d
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 18 deletions.
6 changes: 5 additions & 1 deletion src/js/elevation-line-control.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ class ElevationLineControl {
if (lastLngLat != null) {
currentDistance += currentLngLat.distanceTo(lastLngLat);
}
let currentDataPoint = {x: currentDistance, y: elevation.z}
let elevationValue = elevation.z;
if (elevationValue == -99999) {
elevationValue = 0;
}
let currentDataPoint = {x: currentDistance, y: elevationValue}
this.elevationData.push(currentDataPoint);
lastLngLat = currentLngLat;
});
Expand Down
32 changes: 28 additions & 4 deletions src/js/my-position.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,25 @@ class Position {
var longitude = this.coordinates.lon;
var altitude = this.elevation;

// adresse disponible
var templateAddress = `
<span class="lblPositionAddress">${address.number} ${address.street}</span><br />
<span class="lblPositionCity">${address.postcode} ${address.city}</span>
`
// pas d'adresse
if (!address.street) {
templateAddress = `
<span class="lblPositionAddress">${address.city} ${address.postcode}</span>
`
}
// Ni adress ni poi
if (!address.city) {
templateAddress = `
<span class="lblPositionAddress">${latitude}, ${longitude}</span>
`
}


// template litteral
this.contentPopup = `
<div id="${id.popup}">
Expand All @@ -88,8 +107,7 @@ class Position {
<div class="divPositionAddress">
<label class="lblPositionImgAddress"></label>
<div class="divPositionSectionAddress fontLight">
<span class="lblPositionAddress">${address.number} ${address.street}</span><br />
<span class="lblPositionCity">${address.postcode} ${address.city}</span>
${templateAddress}
</div>
</div>
<div class="divPositionCoord fontLight">
Expand All @@ -112,6 +130,13 @@ class Position {
// message
var message = `${self.address.number} ${self.address.street}, ${self.address.postcode} ${self.address.city}
(latitiude: ${self.coordinates.lat} / longitude: ${self.coordinates.lon} / altitude: ${self.elevation} m)`;
if (!address.street) {
message = `${self.address.city} ${self.address.postcode}
(latitiude: ${self.coordinates.lat} / longitude: ${self.coordinates.lon} / altitude: ${self.elevation} m)`;
}
if (!address.city) {
message = `latitiude: ${self.coordinates.lat} / longitude: ${self.coordinates.lon} / altitude: ${self.elevation} m`;
}
// redirection vers...
if (self.isDesktop()) {
window.open(`https://api.whatsapp.com:/send?text= ${message}`);
Expand All @@ -130,8 +155,7 @@ class Position {
<div class="divPositionAddress">
<label class="lblPositionImgAddress"></label>
<div class="divPositionSectionAddress fontLight">
<span class="lblPositionAddress">${address.number} ${address.street}</span><br />
<span class="lblPositionCity">${address.postcode} ${address.city}</span>
${templateAddress}
</div>
</div>
<div class="divPositionButtons">
Expand Down
11 changes: 7 additions & 4 deletions src/js/services/elevation.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ const target = new EventTarget();

/**
* service
* @param {*} coordinates
* @returns
* @param {*} coordinates
* @returns
* @fire elevation
*/
const compute = async (coordinates) => {
Expand Down Expand Up @@ -62,12 +62,15 @@ const compute = async (coordinates) => {
return results;
};

/**
/**
* obtenir la valeur Z
* @example
* { lon lat }
*/
const getElevation = () => {
if (results.elevations[0] == -99999) {
return 0;
}
return results.elevations[0];
};

Expand All @@ -80,4 +83,4 @@ export default {
target,
compute,
getElevation
};
};
43 changes: 34 additions & 9 deletions src/js/services/reverse.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ const compute = async (coordinates) => {

let url = new URL("https://data.geopf.fr/geocodage/reverse");
let params = {
index: "address",
index: "address,poi",
searchgeom: `{"type":"Circle","coordinates":[${coordinates.lon},${coordinates.lat}],"radius":100}`,
lon: coordinates.lon,
lat: coordinates.lat
lat: coordinates.lat,
limit: 1,
};

Object.keys(params).forEach(key => url.searchParams.append(key, params[key]));
Expand All @@ -39,18 +40,42 @@ const compute = async (coordinates) => {
if (response.status !== 200) {
throw new Error(response.message);
}
let number = "";
let street = "";
let postcode = "";
let city = "";
let lon = coordinates.lon;
let lat = coordinates.lat;

if (geojson.features[0]) {
if (geojson.features[0].properties._type === "address") {
if (geojson.features[0].properties.housenumber) {
number = geojson.features[0].properties.housenumber;
}
street = geojson.features[0].properties.street;
postcode = geojson.features[0].properties.postcode;
city = geojson.features[0].properties.city;
} else if (geojson.features[0] && geojson.features[0].properties._type === "poi") {
city = geojson.features[0].properties.toponym;
if (geojson.features[0].properties.postcode) {
postcode = geojson.features[0].properties.postcode[0];
}
}
lon = geojson.features[0].geometry.coordinates[0];
lat = geojson.features[0].geometry.coordinates[1];
}

var address = {
number : geojson.features[0].properties.housenumber,
street : geojson.features[0].properties.street,
postcode : geojson.features[0].properties.postcode,
city : geojson.features[0].properties.city
number : number,
street : street,
postcode : postcode,
city : city,
};

results = {
coordinates : {
lon : geojson.features[0].geometry.coordinates[0],
lat : geojson.features[0].geometry.coordinates[1]
lon : lon,
lat : lat,
},
address : address,
};
Expand Down Expand Up @@ -93,4 +118,4 @@ export default {
compute,
getCoordinates,
getAddress
};
};

0 comments on commit 4f37c5d

Please sign in to comment.