Context
This article explains how users developing HarmonyOS Wearable applications can generate random coordinates and utilize them within their applications.
Description
In location- and map-based HarmonyOS Wearable applications, functionalities such as displaying markers on a map, creating navigation routes, or visualizing target locations play a crucial role in enhancing the user experience. Developers often need to simulate real-world scenarios during development and testing, which requires the use of location-based mock data. In these cases, generating random coordinates allows developers to test various features—such as dynamic route calculation, proximity-based notifications, and map visualization—without relying on fixed or predefined locations. This approach ensures more robust testing, helps identify potential edge cases, and improves the overall reliability and performance of location-aware applications.
Solution / Approach
The following example solution demonstrates how to generate random coordinates within the boundaries of a specific country and how to persist (store) the generated location.
First, as an example, let’s create a type named Friend and add latitude and longitude fields to this type.
export interface Friend {
id: number,
name: string,
latitude: number,
longitude: number
}
Secondly, we can store this object in the database by generating and assigning random coordinates to it.
function generateTimestampCoordinates(): Location {
const timestamp = new Date().getTime();
const latSeed = (timestamp * 9301 + 49297) % 233280;
const lonSeed = (timestamp * 49297 + 9301) % 233280;
const minLat = 36.5;
const maxLat = 41.5;
const minLon = 26.0;
const maxLon = 44.5;
let latitude = minLat + (latSeed / 233280) * (maxLat - minLat);
let longitude = minLon + (lonSeed / 233280) * (maxLon - minLon);
const easternLimit = 27.5 + (latitude - minLat) * 3.5;
if (longitude > easternLimit) {
longitude = easternLimit;
}
return {
latitude: parseFloat(latitude.toFixed(6)),
longitude: parseFloat(longitude.toFixed(6))
};
}
export async function addFriend(fullName: string): Promise<void> {
try {
const location: Location = generateTimestampCoordinates();
let valueBucket: relationalStore.ValuesBucket = {
'name': fullName,
'latitude': location.latitude,
'longitude': location.longitude
};
await rdbStore.insert('friends', valueBucket);
promptAction.showToast({
message: 'Friend added successfully',
duration: 3000
});
} catch (error) {
console.error('Unexpected error in addFriend:', error);
promptAction.showToast({
message: 'Unexpected error occurred while adding friend',
duration: 3000
});
}
}
As can be seen in the corresponding code, a function is created to generate random coordinates within the borders of Turkey for each incoming record, and the object is then stored using these coordinates.When the corresponding record is visualized on the map in various ways, the resulting output appears as shown below;
As can be observed in the example, two different locations have been generated for two separate records.
Key Takeaways
- Random Coordinate Generation: Developers can create random location coordinates within specific geographic boundaries to simulate real-world scenarios for testing and development purposes.
- Enhanced Testing Flexibility: Using random coordinates allows testing of location-based features, such as map markers, route calculation, and proximity-based notifications, without relying on fixed or predefined locations.
-
Database Integration:
Generated coordinates can be associated with objects (e.g., a
Friendtype) and stored in a database for further usage or simulation. - Dynamic Map Visualization: Randomly generated locations can be visualized on maps, helping developers to verify the correct placement of markers and the behavior of map-based features in different scenarios.
Additional Resources
https://developer.huawei.com/consumer/en/doc/harmonyos-guides/location-kit-intro
https://developer.huawei.com/consumer/en/doc/harmonyos-guides/map-kit-guide
https://developer.huawei.com/consumer/en/doc/harmonyos-guides/map-marker
https://developer.huawei.com/consumer/en/doc/harmonyos-guides/data-persistence-by-rdb-store


Top comments (0)