Skip to content

Commit

Permalink
Adjusted Feature Details
Browse files Browse the repository at this point in the history
  • Loading branch information
esride-ale committed Nov 18, 2024
1 parent d8d6222 commit f6fbcb1
Showing 1 changed file with 170 additions and 59 deletions.
229 changes: 170 additions & 59 deletions native-apps/urban-heat-notifier/urban_heat_notifier/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,29 @@ class MapScreen extends StatefulWidget {

class _MapScreenState extends State<MapScreen> {
bool _switchValue = false;
bool _showHriDetails = false;
Feature? _currentLocationFeature;
String _hriDetails = '';
Map<String, String> _fieldAliases = {};
final _mapViewController = ArcGISMapView.createController();
late ArcGISVectorTiledLayer _hriVectorTiledLayer;
late FeatureLayer _hriFeatureLayer;


@override
void initState() {
super.initState();
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(Env.title)
),
body: Stack(
children: [
appBar: AppBar(title: Text(Env.title)),
body: Stack(children: [
ArcGISMapView(
controllerProvider: () => _mapViewController,
onMapViewReady: onMapViewReady
),
Positioned(
bottom: 25,
controllerProvider: () => _mapViewController,
onMapViewReady: onMapViewReady),
Positioned(
bottom: 30,
right: 16,
child: Row(
children: [
Expand All @@ -64,19 +63,71 @@ class _MapScreenState extends State<MapScreen> {
],
),
),
])
);
Positioned(
bottom: 65,
right: 16,
child: Row(
children: [
Text('Show HRI Details'),
Switch(
value: _showHriDetails,
onChanged: (value) {
setState(() {
_showHriDetails = value;
});
},
),
],
),
),
if (_showHriDetails)
Positioned(
bottom: 30,
left: 10,
child: Container(
width: 200,
padding: EdgeInsets.all(10),
margin: EdgeInsets.all(10),
decoration: BoxDecoration(
color: Colors.grey.withOpacity(0.5),
borderRadius: BorderRadius.circular(10),
boxShadow: [
BoxShadow(
color: Colors.black26,
blurRadius: 5,
offset: Offset(0, 2),
),
],
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'HRI Details',
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.bold,
),
),
SizedBox(height: 0),
Text(
_hriDetails,
),
],
),
),
),
]));
}

void onMapViewReady() {

BasemapStyle basemapStyle = BasemapStyle.values.firstWhere((e) => e.toString() == Env.basemap);
var map = ArcGISMap.withBasemapStyle(basemapStyle);
void onMapViewReady() async {
BasemapStyle basemapStyle =
BasemapStyle.values.firstWhere((e) => e.toString() == Env.basemap);
var map = ArcGISMap.withBasemapStyle(basemapStyle);

final parsedInitialViewPoint = jsonDecode(Env.initialViewPont);

final parsedInitialViewPoint = jsonDecode(Env.initialViewPont);

map.initialViewpoint = Viewpoint.fromCenter(
map.initialViewpoint = Viewpoint.fromCenter(
ArcGISPoint(
x: double.parse(parsedInitialViewPoint["lon"].toString()),
y: double.parse(parsedInitialViewPoint["lat"].toString()),
Expand All @@ -85,47 +136,107 @@ class _MapScreenState extends State<MapScreen> {
scale: double.parse(parsedInitialViewPoint["scale"].toString()),
);

var portalItem = PortalItem.withPortalAndItemId(
portal: Portal.arcGISOnline(),
itemId: Env.hriVectorTileServicePortalItemID);
_hriVectorTiledLayer = ArcGISVectorTiledLayer.withItem(portalItem);
map.operationalLayers.add(_hriVectorTiledLayer);

portalItem = PortalItem.withPortalAndItemId(
portal: Portal.arcGISOnline(),
itemId: Env.hriFeatureServicePortalItemID);

_hriFeatureLayer =
FeatureLayer.withItem(featureServiceItem: portalItem, layerId: 0);
_hriFeatureLayer.definitionExpression = "hri >= 9";
_hriFeatureLayer.opacity = .5;
map.operationalLayers.add(_hriFeatureLayer);

await _hriFeatureLayer.load();
setState(() {
_fieldAliases = {
for (var field in _hriFeatureLayer.featureTable!.fields)
field.name: field.alias
};
});

_mapViewController.arcGISMap = map;

// Set the initial system location data source and auto-pan mode.
_mapViewController.locationDisplay.dataSource = SystemLocationDataSource();
_mapViewController.locationDisplay.autoPanMode =
LocationDisplayAutoPanMode.compassNavigation;
_mapViewController.locationDisplay.initialZoomScale = 2000;

_mapViewController.locationDisplay.onLocationChanged
.listen((location) async {

_hriFeatureLayer.clearSelection();
final locationGeometryProjected = GeometryEngine.project(
location.position,
outputSpatialReference: SpatialReference.webMercator);

final queryParameters = QueryParameters();
queryParameters.geometry = locationGeometryProjected;
queryParameters.maxFeatures = 1;
queryParameters.returnGeometry = false;

ServiceFeatureTable sfTable =
_hriFeatureLayer.featureTable as ServiceFeatureTable;

final queryResult = await sfTable.queryFeaturesWithFieldOptions(
parameters: queryParameters,
queryFeatureFields: QueryFeatureFields.loadAll);

if (!queryResult.features().isEmpty) {
final locationFeature = queryResult.features().first;

_currentLocationFeature = locationFeature;
_hriFeatureLayer.selectFeature(feature: locationFeature);

setState(() {
_hriDetails = _currentLocationFeature!.attributes.entries
.where((entry) => ['HRI', 'PCT_built_up_area', 'PCT_Tree_Cover']
.contains(entry.key))
.map((entry) {
final value = entry.value;
final displayValue =
value is double ? value.toStringAsFixed(2) : value.toString();
return '${_fieldAliases[entry.key] ?? entry.key}: $displayValue';
}).join('\n');
});
} else {
setState(() {
_hriDetails = '';
});
}
});

var portalItem = PortalItem.withPortalAndItemId(portal: Portal.arcGISOnline(), itemId: Env.hriVectorTileServicePortalItemID);
_hriVectorTiledLayer = ArcGISVectorTiledLayer.withItem(portalItem);
map.operationalLayers.add(_hriVectorTiledLayer);

portalItem = PortalItem.withPortalAndItemId(portal: Portal.arcGISOnline(), itemId: Env.hriFeatureServicePortalItemID);
_hriFeatureLayer = FeatureLayer.withFeatureLayerItem(portalItem);
_hriFeatureLayer.definitionExpression = "hri >= 9";
_hriFeatureLayer.opacity = .5;
map.operationalLayers.add(_hriFeatureLayer);

_mapViewController.arcGISMap = map;

// Set the initial system location data source and auto-pan mode.
_mapViewController.locationDisplay.dataSource = SystemLocationDataSource();
_mapViewController.locationDisplay.autoPanMode = LocationDisplayAutoPanMode.compassNavigation;
_mapViewController.locationDisplay.initialZoomScale = 2000;

_mapViewController.onScaleChanged.listen((scale){
_toggleUserLocation();
_toggleLayerVisibility(scale);
});
_mapViewController.onScaleChanged.listen((scale) {
_toggleUserLocation();
_toggleLayerVisibility(scale);
});
}

void _toggleUserLocation() async {
if (_switchValue) {
_mapViewController.locationDisplay.autoPanMode = LocationDisplayAutoPanMode.compassNavigation;
_mapViewController.locationDisplay.start();
} else {
_mapViewController.locationDisplay.autoPanMode = LocationDisplayAutoPanMode.off;
_mapViewController.locationDisplay.stop();
}
void _toggleUserLocation() async {
if (_switchValue) {
_mapViewController.locationDisplay.autoPanMode =
LocationDisplayAutoPanMode.compassNavigation;
_mapViewController.locationDisplay.start();
} else {
_mapViewController.locationDisplay.autoPanMode =
LocationDisplayAutoPanMode.off;
_mapViewController.locationDisplay.stop();
}
}

void _toggleLayerVisibility(double scale) async {
if (scale <= 10000) {
_hriVectorTiledLayer.isVisible = false;
_hriFeatureLayer.isVisible = true;
} else {
_hriVectorTiledLayer.isVisible = true;
_hriFeatureLayer.isVisible = false;
}
void _toggleLayerVisibility(double scale) async {
if (scale <= 10000) {
_hriVectorTiledLayer.isVisible = false;
_hriFeatureLayer.isVisible = true;
} else {
_hriVectorTiledLayer.isVisible = true;
_hriFeatureLayer.isVisible = false;
}
}
}
}

0 comments on commit f6fbcb1

Please sign in to comment.