/* * Dynamically add reverse geocoded address to the tooltip. * Tooltip action must be set in advance (e.g. the wizard) * Usage: to use with reverse geocoding from HERE Maps * 2017-05-15 Piotr Szrajber <piotr.szrajber@hexagongeospatial.com> */varAPP_CODE="<Place your HERE Maps APP_CODE here>",APP_ID="<Place your HERE Maps APP_ID here>",ADDRESS_NOT_FOUND="Address not found";// fetch address from HERE Maps APIfunctionfetchAddress(lat,lng,options){varbaseUrl="https://reverse.geocoder.cit.api.here.com/6.2/reversegeocode.json",params={app_id:options.app_id,app_code:options.app_code,gen:options.gen||9,prox:[lat,lng,options.buffer||10].join(","),mode:"retrieveAddresses"},url=baseUrl+"?"+Object.keys(params).map(function(key){returnkey+"="+params[key];}).join("&");return$.ajax(url,{method:"GET",responseType:'application/json'});}// parse the address (just label here)functionparseAddress(json){varview=json.Response&&json.Response.View[0];if(!view)returnADDRESS_NOT_FOUND;varresult=view.Result&&view.Result[0];if(!result)returnADDRESS_NOT_FOUND;varlabel=result.Location.Address.Label;returnlabel;}// overwrite the default tooltip action so that it adds a row with reverse geocoded addressfunctiontooltipUpdateOverwrite(label,feature,rows,event){if(!feature||!feature.value)return;varttipContent=this.getTooltipContent(feature,rows),// add a new table row with a cell that is using feature IDnewRow="<tr><td>Address:</td><td id=\"Loading_tooltip_"+feature.id+"\">Loading...</td></tr>",newContent=ttipContent.replace(/<\/table>/,newRow+"</table>"),lat=event.lat,lng=event.lng;label.setContent(newContent);label.setLatLng({lat:lat,lng:lng});// asynchronously fetch the address using reverse geocoding from HERE MapsfetchAddress(lat,lng,{app_id:APP_ID,app_code:APP_CODE}).then(parseAddress).then(function(address){varcell=document.querySelector("#Loading_tooltip_"+feature.id);// if there is no such a cell, it means that the tooltip has been closed before so we can actuall cancel the asynchronous operationif(!cell){console.log("No cell for id="+feature.id);return;}cell.innerHTML=address;});}//gsp.ready("v1.0", function(gsp) { // uncomment this line when using in the javascript console// find first choroplethgsp.bi.stage.findWidgets({descriptors:[{chartM:{chart:"choropleth"}}]},function(widgets){if(!widgets||!widgets[0]){console.error("No choropleth found.");}vargeochart=widgets[0].chart;geochart.tooltipProvider().update=tooltipUpdateOverwrite;});//}); // uncomment this line when using in the javascript console
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.