Skip to content

发送定位 ​

给好友或群发送一条位置消息,对方点开就是地图。

接口地址: POST /sendLocation

前置条件 ​

  • 微信实例已登录,并已获得可用 wId。
  • 发送目标是好友、文件传输助手或当前微信所在群聊。

请求参数 ​

参数名必选类型说明
wId是string登录实例标识
wcId是string接收方微信 ID、filehelper 或群 ID,如 24187765053@chatroom
content是string位置消息的 XML,写法见下方「content 怎么填」

content 怎么填 ​

转发收到的定位:把消息回调里的 content 原样传进来即可,见回调字典的 地理位置 60020 和 群聊地理位置 80020。

自己发一个地点:复制下面的模板,只改这 4 个属性,其他属性照模板保留。

xml
<?xml version="1.0"?> <msg> <location x="31.239580" y="121.499763" label="上海市浦东新区世纪大道1号" poiid="" poiname="东方明珠广播电视塔" poiPhone="" isFromPoiList="null" scale="15" maptype="" buildingId="" floorName="" poiCategoryTips="" poiBusinessHour="" poiPriceTips="" adcode="" cityname="" /> </msg>
属性说明
x纬度,如 31.239580
y经度,如 121.499763
poiname地点名称,对方看到的第一行
label详细地址,对方看到的第二行

注意 x 和 y

x 是纬度、y 是经度,和常见的「经度在前」相反。填反了地图会落到别处。

放进 JSON 时,XML 里的双引号要转义成 \",见下方请求示例。

响应参数 ​

参数名类型说明
codestring1000 成功;其他值都是失败,通常是 1001
messagestring反馈信息
data.typeint消息类型
data.msgIdlong消息 ID
data.newMsgIdlong新消息 ID
data.createTimelong发送时间戳(秒)
data.wcIdstring接收方 ID
data.respRetint发送失败时才有:微信侧错误码,含义见发送错误码
data.respMsgstring发送失败时可能有:微信侧的错误说明

请求示例 ​

http
POST {BASE_URL}/sendLocation
Authorization: Bearer <你的调用凭证>
Content-Type: application/json

{
  "wId": "0000016e-63eb-f319-0001-ed01076abf1f",
  "wcId": "filehelper",
  "content": "<?xml version=\"1.0\"?> <msg> <location x=\"31.239580\" y=\"121.499763\" label=\"上海市浦东新区世纪大道1号\" poiid=\"\" poiname=\"东方明珠广播电视塔\" poiPhone=\"\" isFromPoiList=\"null\" scale=\"15\" maptype=\"\" buildingId=\"\" floorName=\"\" poiCategoryTips=\"\" poiBusinessHour=\"\" poiPriceTips=\"\" adcode=\"\" cityname=\"\" /> </msg>"
}
bash
curl -X POST '{BASE_URL}/sendLocation' \
  -H 'Authorization: Bearer <你的调用凭证>' \
  -H 'Content-Type: application/json' \
  --data-raw '{
  "wId": "0000016e-63eb-f319-0001-ed01076abf1f",
  "wcId": "filehelper",
  "content": "<?xml version=\"1.0\"?> <msg> <location x=\"31.239580\" y=\"121.499763\" label=\"上海市浦东新区世纪大道1号\" poiid=\"\" poiname=\"东方明珠广播电视塔\" poiPhone=\"\" isFromPoiList=\"null\" scale=\"15\" maptype=\"\" buildingId=\"\" floorName=\"\" poiCategoryTips=\"\" poiBusinessHour=\"\" poiPriceTips=\"\" adcode=\"\" cityname=\"\" /> </msg>"
}'
python
import requests

url = "{BASE_URL}/sendLocation"
headers = {"Authorization": "Bearer <你的调用凭证>", "Content-Type": "application/json"}
body = r"""{
  "wId": "0000016e-63eb-f319-0001-ed01076abf1f",
  "wcId": "filehelper",
  "content": "<?xml version=\"1.0\"?> <msg> <location x=\"31.239580\" y=\"121.499763\" label=\"上海市浦东新区世纪大道1号\" poiid=\"\" poiname=\"东方明珠广播电视塔\" poiPhone=\"\" isFromPoiList=\"null\" scale=\"15\" maptype=\"\" buildingId=\"\" floorName=\"\" poiCategoryTips=\"\" poiBusinessHour=\"\" poiPriceTips=\"\" adcode=\"\" cityname=\"\" /> </msg>"
}"""

resp = requests.post(url, headers=headers, data=body.encode("utf-8"))
print(resp.text)
java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

String url = "{BASE_URL}/sendLocation";
String body = "{\n" +
    "  \"wId\": \"0000016e-63eb-f319-0001-ed01076abf1f\",\n" +
    "  \"wcId\": \"filehelper\",\n" +
    "  \"content\": \"<?xml version=\\\"1.0\\\"?> <msg> <location x=\\\"31.239580\\\" y=\\\"121.499763\\\" label=\\\"上海市浦东新区世纪大道1号\\\" poiid=\\\"\\\" poiname=\\\"东方明珠广播电视塔\\\" poiPhone=\\\"\\\" isFromPoiList=\\\"null\\\" scale=\\\"15\\\" maptype=\\\"\\\" buildingId=\\\"\\\" floorName=\\\"\\\" poiCategoryTips=\\\"\\\" poiBusinessHour=\\\"\\\" poiPriceTips=\\\"\\\" adcode=\\\"\\\" cityname=\\\"\\\" /> </msg>\"\n" +
    "}";

HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
    .header("Authorization", "Bearer <你的调用凭证>")
    .header("Content-Type", "application/json")
    .POST(HttpRequest.BodyPublishers.ofString(body))
    .build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
php
<?php

$url = '{BASE_URL}/sendLocation';
$body = <<<'JSON'
{
  "wId": "0000016e-63eb-f319-0001-ed01076abf1f",
  "wcId": "filehelper",
  "content": "<?xml version=\"1.0\"?> <msg> <location x=\"31.239580\" y=\"121.499763\" label=\"上海市浦东新区世纪大道1号\" poiid=\"\" poiname=\"东方明珠广播电视塔\" poiPhone=\"\" isFromPoiList=\"null\" scale=\"15\" maptype=\"\" buildingId=\"\" floorName=\"\" poiCategoryTips=\"\" poiBusinessHour=\"\" poiPriceTips=\"\" adcode=\"\" cityname=\"\" /> </msg>"
}
JSON;

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Authorization: Bearer <你的调用凭证>',
    'Content-Type: application/json',
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;

成功响应 ​

json
{
  "code": "1000",
  "message": "处理成功",
  "data": {
    "type": 48,
    "msgId": 2562652206,
    "newMsgId": 4482117376572170922,
    "createTime": 1641457769,
    "wcId": "filehelper"
  }
}

错误响应 ​

发送失败时 message 和发送文本一样,都是「发送文本消息失败」,具体原因看 data.respRet。

json
{
  "code": "1001",
  "message": "发送文本消息失败",
  "data": {
    "respRet": -24
  }
}

验证与排查 ​

场景处理方式
首次联调发到 filehelper,手机微信能看到位置卡片即链路正常
地图位置不对检查 x、y 是否填反:x 是纬度,y 是经度
message 是「wId已注销或二维码失效,请重新登录」wId 填错了,或这个账号已掉线,重新登录后用新的 wId
返回失败看 data.respRet,按 发送错误码 处理