kopia lustrzana https://github.com/FacilMap/facilmap
Only get elevation for search results when necessary
rodzic
65a52131d1
commit
f146f6b4d2
|
@ -322,7 +322,8 @@ Search for places.
|
|||
|
||||
* `data` (object): An object with the following properties:
|
||||
* `query` (string): The query string
|
||||
* `loadUrls` (string): Whether to return the file if `query` is a URL
|
||||
* `loadUrls` (boolean): Whether to return the file if `query` is a URL
|
||||
* `elevation` (boolean): Whether to find out the elevation of the result(s). Will make the search significantly slower.
|
||||
* _returns_ (Promise<string|Array<[searchResult](#searchresult)>>): If `data.query` is a URL to a GPX/KML/OSM/GeoJSON
|
||||
file, that file as a string, otherwise an array of search results.
|
||||
|
||||
|
|
|
@ -44,7 +44,7 @@ fm.app.factory("fmSearchQuery", function($rootScope, $compile, fmUtils, $timeout
|
|||
var q = scope.submittedSearchString = scope.searchString;
|
||||
map.mapEvents.$broadcast("searchchange");
|
||||
|
||||
map.client.find({ query: scope.searchString, loadUrls: true }).then(function(results) {
|
||||
map.client.find({ query: scope.searchString, loadUrls: true, elevation: true }).then(function(results) {
|
||||
if(q != scope.submittedSearchString)
|
||||
return; // Another search has been started in the meantime
|
||||
|
||||
|
@ -164,7 +164,7 @@ fm.app.factory("fmSearchQuery", function($rootScope, $compile, fmUtils, $timeout
|
|||
map.mapEvents.$on("longmousedown", function(e, latlng) {
|
||||
clickMarker.clearLayers();
|
||||
|
||||
map.client.find({ query: "geo:" + fmUtils.round(latlng.lat, 5) + "," + fmUtils.round(latlng.lng, 5) + "?z=" + map.map.getZoom(), loadUrls: false }).then(function(results) {
|
||||
map.client.find({ query: "geo:" + fmUtils.round(latlng.lat, 5) + "," + fmUtils.round(latlng.lng, 5) + "?z=" + map.map.getZoom(), loadUrls: false, elevation: true }).then(function(results) {
|
||||
clickMarker.clearLayers();
|
||||
|
||||
if(results.length > 0) {
|
||||
|
|
|
@ -55,7 +55,7 @@ const stateAbbr = {
|
|||
|
||||
const search = module.exports = {
|
||||
|
||||
find(query, loadUrls) {
|
||||
find(query, loadUrls, loadElevation) {
|
||||
return Promise.resolve().then(function() {
|
||||
query = query.replace(/^\s+/, "").replace(/\s+$/, "");
|
||||
|
||||
|
@ -78,18 +78,18 @@ const search = module.exports = {
|
|||
lat: 1*lonlat_match[2].replace(",", ".").replace(/\s+/, ""),
|
||||
lon : 1*lonlat_match[4].replace(",", ".").replace(/\s+/, ""),
|
||||
zoom : lonlat_match[7] != null ? 1*lonlat_match[7] : null
|
||||
}).then((res) => (res.map((res) => (Object.assign(res, {id: query})))));
|
||||
}, loadElevation).then((res) => (res.map((res) => (Object.assign(res, {id: query})))));
|
||||
}
|
||||
|
||||
let osm_match = query.match(/^([nwr])(\d+)$/i);
|
||||
if(osm_match)
|
||||
return search._findOsmObject(osm_match[1], osm_match[2]);
|
||||
return search._findOsmObject(osm_match[1], osm_match[2], loadElevation);
|
||||
|
||||
return search._findQuery(query);
|
||||
return search._findQuery(query, loadElevation);
|
||||
});
|
||||
},
|
||||
|
||||
_findQuery(query) {
|
||||
_findQuery(query, loadElevation) {
|
||||
return request({
|
||||
url: nameFinderUrl + "/search?format=jsonv2&polygon_geojson=1&addressdetails=1&namedetails=1&limit=" + encodeURIComponent(limit) + "&extratags=1&q=" + encodeURIComponent(query),
|
||||
json: true
|
||||
|
@ -101,7 +101,7 @@ const search = module.exports = {
|
|||
throw body.error;
|
||||
|
||||
let points = body.filter((res) => (res.lon && res.lat));
|
||||
if(points.length > 0) {
|
||||
if(loadElevation && points.length > 0) {
|
||||
return elevation.getElevationForPoints(points).then((elevations) => {
|
||||
elevations.forEach((elevation, i) => {
|
||||
points[i].elevation = elevation;
|
||||
|
@ -114,7 +114,7 @@ const search = module.exports = {
|
|||
}).then((body) => (body.map(search._prepareSearchResult)));
|
||||
},
|
||||
|
||||
_findOsmObject(type, id) {
|
||||
_findOsmObject(type, id, loadElevation) {
|
||||
return request({
|
||||
url: `${nameFinderUrl}/reverse?format=json&addressdetails=1&polygon_geojson=1&extratags=1&namedetails=1&osm_type=${encodeURI(type.toUpperCase())}&osm_id=${encodeURI(id)}`,
|
||||
json: true
|
||||
|
@ -123,20 +123,20 @@ const search = module.exports = {
|
|||
throw body ? body.error : "Invalid response from name finder";
|
||||
}
|
||||
|
||||
if(body.lat && body.lon)
|
||||
if(loadElevation && body.lat && body.lon)
|
||||
return elevation.getElevationForPoint(body).then((elevation) => (Object.assign(body, { elevation })));
|
||||
else
|
||||
return body;
|
||||
}).then((body) => ([ search._prepareSearchResult(body) ]));
|
||||
},
|
||||
|
||||
_findLonLat(lonlatWithZoom) {
|
||||
_findLonLat(lonlatWithZoom, loadElevation) {
|
||||
return Promise.all([
|
||||
request({
|
||||
url: `${nameFinderUrl}/reverse?format=json&addressdetails=1&polygon_geojson=1&extratags=1&namedetails=1&lat=${encodeURI(lonlatWithZoom.lat)}&lon=${encodeURI(lonlatWithZoom.lon)}&zoom=${encodeURI(lonlatWithZoom.zoom != null ? (lonlatWithZoom.zoom >= 12 ? lonlatWithZoom.zoom+2 : lonlatWithZoom.zoom) : 17)}`,
|
||||
json: true
|
||||
}),
|
||||
elevation.getElevationForPoint(lonlatWithZoom)
|
||||
...(loadElevation ? [elevation.getElevationForPoint(lonlatWithZoom)] : [])
|
||||
]).then(function([body, elevation]) {
|
||||
if(!body || body.error) {
|
||||
let name = utils.round(lonlatWithZoom.lat, 5) + ", " + utils.round(lonlatWithZoom.lon, 5);
|
||||
|
|
|
@ -455,10 +455,10 @@ utils.extend(SocketConnection.prototype, {
|
|||
|
||||
find: function(data) {
|
||||
return Promise.resolve().then(() => {
|
||||
if(!utils.stripObject(data, { query: "string", loadUrls: "boolean" }))
|
||||
if(!utils.stripObject(data, { query: "string", loadUrls: "boolean", elevation: "boolean" }))
|
||||
throw "Invalid parameters.";
|
||||
|
||||
return search.find(data.query, data.loadUrls);
|
||||
return search.find(data.query, data.loadUrls, data.elevation);
|
||||
});
|
||||
},
|
||||
|
||||
|
|
Ładowanie…
Reference in New Issue