如何生成包含Geofire密钥的模拟数据?
Hey there! I’ve run into a similar snag with Mockaroo before—turns out it doesn’t have a built-in feature to generate GeoFire keys directly. But don’t stress, there are two solid workarounds to get the mock data your feature depends on:
1. Generate Lat/Lon First, Then Convert to GeoFire Keys
This is the most straightforward approach if you don’t mind a quick post-processing step:
- In Mockaroo, create two number fields:
latitude: Set the range to-90to90(valid global latitude values)longitude: Set the range to-180to180(valid global longitude values)
- Export your mock dataset (CSV, JSON, etc.) once it’s generated
- Use GeoFire’s official SDK to convert each pair of coordinates into a GeoFire key. Here’s a quick JavaScript example:
const GeoFire = require('geofire'); // Example entry from your Mockaroo data const mockLocation = { latitude: 37.7749, longitude: -122.4194 }; const geofireKey = GeoFire.encodeGeohash(mockLocation.latitude, mockLocation.longitude); console.log(geofireKey); // Outputs the corresponding GeoHash key
2. Use Mockaroo’s Custom Script Field to Generate Keys Directly
If you want to skip post-processing entirely, you can embed a GeoHash encoding script directly into Mockaroo:
- Add a new field to your Mockaroo schema, set its type to Script
- Paste a complete GeoHash encoding function into the script editor (you can find open-source GeoHash implementations online that work in vanilla JS)
- Modify the script to generate random valid coordinates, then encode them into a GeoFire key. Here’s a simplified template:
// Full GeoHash encoding function (replace with a complete public implementation) function encodeGeohash(latitude, longitude) { const base32Chars = '0123456789bcdefghjkmnpqrstuvwxyz'; // Insert full GeoHash encoding logic here (plenty of free implementations exist online) } // Generate random valid coordinates const randomLat = Math.random() * 180 - 90; const randomLon = Math.random() * 360 - 180; // Return the encoded GeoFire key return encodeGeohash(randomLat, randomLon);
Once you save this script, Mockaroo will generate a valid GeoFire key for each entry in your dataset automatically.
Either method should get you the mock data you need to test your feature that relies on GeoFire keys.
内容的提问来源于stack exchange,提问作者about14sheep

