NewsMarker.fromGeoJson constructor

NewsMarker.fromGeoJson(
  1. Map<String, dynamic> feature
)

Parse a NewsMarker from GDELT GeoJSON feature.

Implementation

factory NewsMarker.fromGeoJson(Map<String, dynamic> feature) {
  final geometry = feature['geometry'] as Map<String, dynamic>;
  final properties = feature['properties'] as Map<String, dynamic>?;

  final coordinates = geometry['coordinates'] as List<dynamic>;
  final lng = (coordinates[0] as num).toDouble();
  final lat = (coordinates[1] as num).toDouble();

  // Extract title and URL from HTML field.

  String title = properties?['name']?.toString() ?? 'No title';
  String? url;

  final html = properties?['html']?.toString();
  if (html != null && html.isNotEmpty) {
    // Parse HTML to extract title and URL.
    final hrefMatch = RegExp(r'href="([^"]+)"').firstMatch(html);
    final titleMatch = RegExp(r'>([^<]+)</a>').firstMatch(html);

    if (hrefMatch != null) {
      url = hrefMatch.group(1);
    }
    if (titleMatch != null) {
      title = titleMatch.group(1) ?? title;
    }
  }

  return NewsMarker(
    id: feature['id']?.toString() ?? DateTime.now().toIso8601String(),
    location: LatLng(lat, lng),
    title: title,
    source: properties?['name']?.toString(), // Country/region name
    url: url,
    publishedAt: null, // GDELT geo API doesn't provide dates
    imageUrl: properties?['shareimage']?.toString(),
    tone: null, // Not available in geo API
  );
}