country_coordinates 1.0.0
country_coordinates: ^1.0.0 copied to clipboard
A simple package to get coordinates and geometry type of countries.
example/country_coordinates_example.dart
import 'package:country_coordinates/country_coordinates.dart';
void main() async {
print('π Country Coordinates Package Example\n');
try {
// Step 1: Load countries data from assets
print('π₯ Loading countries data...');
await loadCountries();
print('β
Countries data loaded successfully!\n');
// Step 2: Get all countries and show some basic info
print('π Getting all countries...');
List<Country> allCountries = getAllCountries();
print('π Total countries available: ${allCountries.length}\n');
// Step 3: Display first 5 countries as examples
print('π First 5 countries:');
for (int i = 0; i < 5 && i < allCountries.length; i++) {
Country country = allCountries[i];
print(' ${i + 1}. ${country.title}');
print(
' Coordinates: [${country.coordinates[0].toStringAsFixed(4)}, ${country.coordinates[1].toStringAsFixed(4)}]');
print(' Geometry Type: ${country.geometryType}');
print('');
}
// Step 4: Search for specific countries by name
print('π Searching for specific countries...\n');
// Search for France
Country? france = getCountryByName('France');
if (france != null) {
print('π«π· Found France:');
print(
' Coordinates: [${france.coordinates[0].toStringAsFixed(4)}, ${france.coordinates[1].toStringAsFixed(4)}]');
print(' Geometry Type: ${france.geometryType}');
print('');
} else {
print('β France not found');
}
// Search for Japan
Country? japan = getCountryByName('Japan');
if (japan != null) {
print('π―π΅ Found Japan:');
print(
' Coordinates: [${japan.coordinates[0].toStringAsFixed(4)}, ${japan.coordinates[1].toStringAsFixed(4)}]');
print(' Geometry Type: ${japan.geometryType}');
print('');
} else {
print('β Japan not found');
}
// Search for a non-existent country
Country? nonExistent = getCountryByName('Atlantis');
if (nonExistent != null) {
print('ποΈ Found Atlantis: ${nonExistent.title}');
} else {
print('β Atlantis not found (as expected)');
}
print('');
// Step 5: Demonstrate working with country data
print('π οΈ Working with country data...\n');
Country? usa = getCountryByName('United States');
if (usa != null) {
print('πΊπΈ United States Details:');
print(' Name: ${usa.title}');
print(' Longitude: ${usa.coordinates[0].toStringAsFixed(4)}Β°');
print(' Latitude: ${usa.coordinates[1].toStringAsFixed(4)}Β°');
print(' Geometry Type: ${usa.geometryType}');
// Calculate distance from equator (simple example)
double distanceFromEquator =
(usa.coordinates[1] * 111).abs(); // Rough km calculation
print(
' Approximate distance from equator: ${distanceFromEquator.toStringAsFixed(0)} km');
print('');
}
// Step 6: Show some statistics
print('π Package Statistics:');
print(' β’ Total countries: ${allCountries.length}');
// Count different geometry types
Map<String, int> geometryCounts = {};
for (Country country in allCountries) {
geometryCounts[country.geometryType] =
(geometryCounts[country.geometryType] ?? 0) + 1;
}
print(' β’ Geometry types:');
geometryCounts.forEach((type, count) {
print(' - $type: $count countries');
});
// Find countries with extreme coordinates
Country? northernmost = allCountries
.reduce((a, b) => a.coordinates[1] > b.coordinates[1] ? a : b);
Country? southernmost = allCountries
.reduce((a, b) => a.coordinates[1] < b.coordinates[1] ? a : b);
Country? easternmost = allCountries
.reduce((a, b) => a.coordinates[0] > b.coordinates[0] ? a : b);
Country? westernmost = allCountries
.reduce((a, b) => a.coordinates[0] < b.coordinates[0] ? a : b);
print(' β’ Extreme coordinates:');
print(
' - Northernmost: ${northernmost.title} (${northernmost.coordinates[1].toStringAsFixed(2)}Β°)');
print(
' - Southernmost: ${southernmost.title} (${southernmost.coordinates[1].toStringAsFixed(2)}Β°)');
print(
' - Easternmost: ${easternmost.title} (${easternmost.coordinates[0].toStringAsFixed(2)}Β°)');
print(
' - Westernmost: ${westernmost.title} (${westernmost.coordinates[0].toStringAsFixed(2)}Β°)');
print('\nπ Example completed successfully!');
print('π‘ You can now use these functions in your own projects.');
} catch (e) {
print('β Error occurred: $e');
print(
'π‘ Make sure the countries.json file is properly included in your assets.');
}
}