文章标题:Let's Meet in the Middle
我喜欢做一些只专注做好一件事的小工具,也喜欢琢磨怎么把它们巧妙地组合起来。这正是我在 Less Software 上那些小型软件项目背后的一贯理念。
我最早做的工具之一叫 whereami。半开玩笑地说,我想把它包装成 whoami 的天然搭档。做这个工具时的想法是:几乎每个现代操作系统都提供了某种原生的地理位置 API。让我觉得别扭的是,终端里我可以为那么多事情跑一条快捷命令,却没法跑一条能调用这项能力的命令。
在让这个工具跨平台的过程中,我发现自己需要应对的其中一件事,就是它们之间的差异。macOS 往往一贯内置了很多额外的贴心功能,尤其是在图像识别、自然语言处理和辅助功能工具方面。
在地理定位方面,macOS 提供而其他操作系统原生方案没有的一个好东西,是内置的地理编码器(以及反向地理编码器)。所以你可以输入这样的东西:
$ whereami
Location: 40.6753, -73.9431
Accuracy: 35m
Address: 865 St Marks Ave, New York, NY, 11213, US
$ whereami --json
{
"latitude": 40.675265033592396,
"longitude": -73.94310370569808,
"accuracy": 35,
"address": {
"street": "865 St Marks Ave",
"city": "New York",
"state": "NY",
"postal_code": "11213",
"country": "US"
}
}
然后你就得到了那个顺手的小地址块!它也让下面这种事变得很简单:
$ whereami "397 Bridge Street, Brooklyn NY"
Location: 40.6913, -73.9850
Address: 397 Bridge St, New York, NY, 11201, US
$ whereami "397 Bridge Street, Brooklyn NY" --json
{
"latitude": 40.6913424,
"longitude": -73.9850328,
"accuracy": 0,
"address": {
"street": "397 Bridge St",
"city": "New York",
"state": "NY",
"postal_code": "11201",
"country": "US"
}
}
现在你可以拿到一个地址的经纬度坐标了。而这一切你在 macOS 系统上「免费」就能获得。还不错!
macOS 还向你的系统提供了另一个顺手的框架:MapKit。它类似于 Google Maps 及其 API 产品,但原生属于操作系统。
事实证明,围绕这个框架构建一个 CLI 封装层也相当直接,所以我做了 nearme(仅限 macOS)。
它们被设计成能很好地配合使用:
$ whereami --json | nearme "coffeeshop" --count=5
1. Cafe Imael (cafe)
1090 St Johns Pl, New York, NY 11213
+1 (718) 758-5857 · https://www.ima el.co · 485 m
2. Zaim Coffee Shop (cafe)
1102 St Johns Pl, New York, NY 11213
+1 (347) 529-3444 · https://www.yelp.com/biz/zaim-coffee-shop-crown-heights · 500 m
3. Cheeni (restaurant)
485 Tompkins Ave, New York, NY 11216
+1 (347) 365-4015 · https://www.yelp.com/biz/cheeni-brooklyn · 589 m
4. Cotton Bean (cafe)
1077 Bergen St, Brooklyn, NY 11216
+1 (347) 276-8749 · https://www.cafecottonbean.com · 623 m
5. MoCo 575 (restaurant)
531 Nostrand Ave, New York, NY 11216
+1 (347) 365-3680 · https://moco575.com · 673 m
$ whereami --json | nearme "coffeeshop" --count=5 --json
文章标题:Let's Meet in the Middle
正文:
[
{
"name": "Cafe Imael",
"address": "1090 St Johns Pl, New York, NY 11213",
"latitude": 40.67105,
"longitude": -73.942339,
"phone": "+1 (718) 758-5857",
"url": "https://www.imael.co",
"category": "cafe"
},
{
"name": "Zaim Coffee Shop",
"address": "1102 St Johns Pl, New York, NY 11213",
"latitude": 40.6710142,
"longitude": -73.9416723,
"phone": "+1 (347) 529-3444",
"url": "https://www.yelp.com/biz/zaim-coffee-shop-crown-heights",
"category": "cafe"
},
{
"name": "Cheeni",
"address": "485 Tompkins Ave, New York, NY 11216",
"latitude": 40.6806663,
"longitude": -73.9431295,
"phone": "+1 (347) 365-4015",
"url": "https://www.yelp.com/biz/cheeni-brooklyn",
"category": "restaurant"
},
{
"name": "Cotton Bean",
"address": "1077 Bergen St, Brooklyn, NY 11216",
"latitude": 40.6762179,
"longitude": -73.950436,
"phone": "+1 (347) 276-8749",
"url": "https://www.cafecottonbean.com",
"category": "cafe"
},
{
"name": "MoCo 575",
"address": "531 Nostrand Ave, New York, NY 11216",
"latitude": 40.6791358,
"longitude": -73.9493723,
"phone": "+1 (347) 365-3680",
"url": "https://moco575.com",
"category": "restaurant"
}
]
这些咖啡馆里至少有 3 家我可以打包票!
去年春天,我在 Recurse Center 度过了一段非正式的休假时光,花了大量时间打磨这些小工具,并寻找将它们组合起来的方式。有人告诉我,我正在做并分享的工作让他们想到了一个理念:你只需要控制三个焦点就能完成一个魔术。
这条评论启发我制作了这份“配方”清单,在其中尝试展示各种以出人意料、实用和/或有趣的方式组合这些工具的方法:
- https://less.software/recipes/
但今天我想专门写的,是这个“meet in the middle”的想法。
我刚才已经展示了如何在 Mac 上使用 whereami 和 nearme 来查找咖啡馆(或者任何你能通过 Apple Maps 找到的地方)。只需一点管道操作和一个顺手的别名,你就能帮你和另一个地点之间找到一个“在中间碰面”的地方:
{ whereami "350 5th Ave, New York" --json; \
whereami "200 Eastern Pkwy, Brooklyn" --json; } | \
jq -s '{latitude: (map(.latitude) | add / 2),
longitude: (map(.longitude) | add / 2)}' | \
nearme "bar" --count=3
1. Deux Chats (nightlife)
27 Broadway, New York, NY 11249
+1 (917) 909-0145 · https://www.deuxchatsbk.com · 575 m
2. Bar Milagro (nightlife)
29 Dunham Pl, New York, NY 11249
+1 (929) 298-0032 · https://www.xolobk.com/barmilagro · 617 m
3. Cafe Susanne (nightlife)
8 River St, Brooklyn, NY 10044
https://www.barsusanne.com · 651 m
就是这样!不需要 API 密钥,不需要浏览器,也不需要账号:只需输入几个地址,另一头就会冒出几家看起来靠谱的酒吧。
工作原理
它是三个小工具,每个只做一件事。
whereami 最初是一个 CLI,用来询问你的操作系统你在哪里。从 0.4 版本开始,它还可以在 macOS 上充当反向地理编码器:
$ whereami "350 5th Ave, New York" --json
{"latitude":40.7482378,"longitude":-73.9850579,"accuracy":0,"address":{"street":"350 5th Ave","city":"New York","state":"NY","postal_code":"10118","country":"US"}}
查找地址不需要位置权限,因为没有任何东西在询问你在哪里。jq 负责做算术。-s 标志把两个 JSON 对象吸入一个数组,然后过滤器对坐标取平均值:
{"latitude":40.7097896,"longitude":-73.9743696}
nearme 使用 MapKit(Apple Maps 背后的搜索引擎)在一组坐标附近搜索地点。它从 stdin 读取 JSON 中的坐标,只关心纬度和经度。它完全不知道这些数字是来自 jq,而不是来自 GPS。
文章标题:让我们在中间碰面
把两个坐标取平均并不是找到“真正”中点的办法,但如果在几英里的范围内找地方碰头喝咖啡,这已经足够接近了。真正要紧的是,中点本身并不知道那里实际上有什么。
把它做成一条命令
把那条管道反复敲上几遍,很快就让人厌烦。把它做成一个函数会更方便:
middle() {
local second=()
# Two addresses only if a third argument is there to be the search term
if [[ $# -ge 3 && $3 != -* ]]; then
second=("$2")
set -- "$1" "${@:3}"
fi
{ whereami "$1" --json; whereami "${second[@]}" --json; } |
jq -s '{latitude: (map(.latitude) | add / 2), longitude: (map(.longitude) | add / 2)}' |
nearme "${2:-coffeeshop}" "${@:3}"
}
或者,如果你像我一样用 fish:
function middle --description 'Search for places halfway between two addresses, or between you and one'
set -l second
# Two addresses only if a third argument is there to be the search term
if set -q argv[3]; and not string match -q -- '-*' $argv[3]
set second $argv[2]
set -e argv[2]
end
begin
whereami $argv[1] --json
whereami $second --json
end | jq -s '{latitude: (map(.latitude) | add / 2), longitude: (map(.longitude) | add / 2)}' | nearme (set -q argv[2]; and echo $argv[2]; or echo coffeeshop) $argv[3..]
end
给它两个地址,再加一个要找的东西。那之后的所有内容都会原样传给 nearme。省掉第二个地址,它就用你当前所在的位置,所以 middle "123 Main Street" coffee 会找到你和 123 Main Street 之间的咖啡店。只给一个地址时,它会找一家咖啡店。
$ middle "350 5th Ave, New York" "200 Eastern Pkwy, Brooklyn" coffee --count=2
1. Cafe Susanne (nightlife)
8 River St, Brooklyn, NY 10044
https://www.barsusanne.com · 651 m
2. Social House Cafe (cafe)
60 Broadway, Brooklyn, NY 11249
+1 (929) 295-0397 · https://www.socialhousecafellc.com · 697 m
注意事项
- 它只支持 macOS。nearme 基于 MapKit 构建,而 whereami 的地址查询也只在 macOS 上存在。
- 它要求街道地址或城市名,不过地标有时也能用。"Golden Gate Bridge" 无法地理编码,但 "350 5th Ave, New York" 和 "Central Park" 以及 "Brooklyn Briddge" 可以(买家自慎)。
- 这套算法是一个非常字面、朴素的地理中点,没有考虑通勤时间中点或其他任何因素。
试试看
brew install georgemandis/tap/whereami georgemandis/tap/nearme jq
这两个工具都归在 Less Software 名下:
如果你觉得它有用,或者碰巧发现其他值得分享的“recipe”好点子,请告诉我!
如果你喜欢读这篇文章,可以考虑在 GitHub 上赞助我的工作,或者订阅我的 newsletter。
分享这篇文章:Hacker News • Reddit • LinkedIn • Mastodon。
发布于 2026 年 11 月 17 日,星期二。以纯文本阅读这篇文章。