Skip to content

Commit

Permalink
Merge pull request #407 from open-pv/feature/search_boundary_zoom
Browse files Browse the repository at this point in the history
Search popups for buildings only
  • Loading branch information
khdlr authored Jan 17, 2025
2 parents b5968a4 + 9e1d061 commit a3e7cf8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 29 deletions.
22 changes: 8 additions & 14 deletions src/pages/Map.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,26 +62,20 @@ function Index() {
})
} else {
// Use only the first one
locations = [locations[0]]
const lons = locations.map((loc) => loc.lon)
const lats = locations.map((loc) => loc.lat)
const location = locations[0]
let bounds = location.boundingBox

const bounds = [
Math.min(...lons),
Math.min(...lats),
Math.max(...lons),
Math.max(...lats),
]
if (location.addressType === 'building') {
// Only add popup when search result is a building!
setMapMarkers([<MapPopup key={location.key} {...location} />])
}
console.log('bounds')
console.log(bounds)
mapRef.current.fitBounds(bounds, {
maxZoom: 17,
speed: 2,
})
}
setMapMarkers(
locations.map((location) => (
<MapPopup key={location.key} {...location} />
)),
)
}

const mapRef = useRef()
Expand Down
35 changes: 20 additions & 15 deletions src/simulation/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,29 @@ async function processAddress(searchString) {
)
response = await fetchCoordinates(url)
}
return response.map((obj) => ({
lat: obj.lat,
lon: obj.lon,
key: obj.place_id,
display_name: format_address(obj.address),
}))
return response.map((obj) => {
console.log(obj.boundingbox)
let [lat0, lat1, lon0, lon1] = obj.boundingbox.map(parseFloat)
return {
lat: obj.lat,
lon: obj.lon,
key: obj.place_id,
addressType: obj.addresstype,
boundingBox: [lon0, lat0, lon1, lat1],
display_name: format_address(obj.address),
}
})
}

function format_address(address) {
let addr =
(address.road || '') +
' ' +
(address.house_number || '') +
', ' +
(address.postcode || '') +
' ' +
(address.city || '')
return addr
const part1 = (address.road || '') + ' ' + (address.house_number || '')
const part2 = (address.postcode || '') + ' ' + (address.city || '')

if (part1 != ' ' && part2 != ' ') {
return part1 + ', ' + part2
} else {
return part1 + part2
}
}

async function fetchCoordinates(url) {
Expand Down

0 comments on commit a3e7cf8

Please sign in to comment.