Geocode/address to geo

跳转到: 导航, 搜索
(使用示例)
(使用示例)
第78行: 第78行:
 
;JSON:  
 
;JSON:  
 
:curl -u "username:password" "http://api.t.sina.com.cn/location/geocode/address_to_geo.json?address=%E4%B8%AD%E5%85%B3%E6%9D%91&source=appkey"
 
:curl -u "username:password" "http://api.t.sina.com.cn/location/geocode/address_to_geo.json?address=%E4%B8%AD%E5%85%B3%E6%9D%91&source=appkey"
 +
 +
== '''其它''' ==
 +
 +
===  '''PHP示例''' ===
 +
需要开启PHP的cURL扩展
 +
<pre>
 +
<?php
 +
//改为你的微博账号,用户名和密码之间用:隔开
 +
$userPwd = 'user@sina.com:pwd';
 +
//改为你微博应用的appkey
 +
$appkey = '0123456789';
 +
//查询关键词
 +
$q = urlencode("海淀");
 +
 +
$url = "http://api.t.sina.com.cn/location/geocode/address_to_geo.xml?address=$q&source=$appkey";
 +
 +
$rst = curlSample($url,$userPwd);
 +
 +
echo $rst;
 +
 +
function curlSample($url,$userPwd,$postFields = '',$header = ''){
 +
$ch = curl_init() or die (curl_error()) ;
 +
curl_setopt($ch,CURLOPT_URL,$url);
 +
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
 +
curl_setopt($ch,CURLOPT_TIMEOUT,30);
 +
if(!empty($userPwd)){
 +
curl_setopt($ch,CURLOPT_USERPWD,$userPwd);
 +
}
 +
if(!empty($postFields)){
 +
curl_setopt($ch,CURLOPT_POST,true);
 +
curl_setopt($ch,CURLOPT_POSTFIELDS,$postFields);
 +
}
 +
if(!empty($header)){
 +
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
 +
}
 +
$result = curl_exec($ch) or die (curl_error($ch));
 +
curl_close($ch);
 +
return $result;
 +
}
 +
?>
 +
</pre>

2011年7月5日 (二) 15:03的版本

目录

geocode/address_to_geo

根据地址返回坐标的接口

URL

http://api.t.sina.com.cn/location/geocode/address_to_geo.(json%7Cxml)

支持格式

XML/JSON

HTTP请求方式

GET

是否需要登录

true

请求数限制

true

请求参数

参数 必选 类型及范围 说明
source true string 申请应用时分配的AppKey,调用接口时候代表应用的唯一身份
address true string 需要获取坐标的地址

返回结果

XML示例

  <?xml version="1.0" encoding="UTF-8" ?> 
  <geoResult>
   <status>1</status> 
   <geo>
    <exactitude>1.000000</exactitude> 
    <name>中国北京市海淀区中关村</name> 
    <longitude>116.324416</longitude> 
    <latitude>39.984213</latitude> 
   </geo>
  </geoResult>

=== '''JSON示例''' ===
<pre>
{
  "status":1,
  "geo":   
  {
   "exactitude":"1.000000",
    "name":"\u4e2d\u56fd\u5317\u4eac\u5e02\u6d77\u6dc0\u533a\u4e2d\u5173\u6751",
    "longitude":"116.324416",
    "latitude":"39.984213"
  }
}

字段说明

status:状态
geo:geo信息
exactitude:置信度(可能为空)
name:匹配到的地址名称
longitude:匹配到的坐标经度
latitude:匹配到的坐标纬度

使用示例

注:中文需进行urlencode编码
XML
curl -u "username:password" "http://api.t.sina.com.cn/location/geocode/address_to_geo.xml?address=%E4%B8%AD%E5%85%B3%E6%9D%91&source=appkey"
JSON
curl -u "username:password" "http://api.t.sina.com.cn/location/geocode/address_to_geo.json?address=%E4%B8%AD%E5%85%B3%E6%9D%91&source=appkey"

其它

PHP示例

需要开启PHP的cURL扩展

<?php
//改为你的微博账号,用户名和密码之间用:隔开
$userPwd = 'user@sina.com:pwd';
//改为你微博应用的appkey
$appkey = '0123456789';
//查询关键词
$q = urlencode("海淀");

$url = "http://api.t.sina.com.cn/location/geocode/address_to_geo.xml?address=$q&source=$appkey";

$rst = curlSample($url,$userPwd);

echo $rst;

function curlSample($url,$userPwd,$postFields = '',$header = ''){
	$ch = curl_init() or die (curl_error()) ;
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch,CURLOPT_TIMEOUT,30);
	if(!empty($userPwd)){
		curl_setopt($ch,CURLOPT_USERPWD,$userPwd);
	}
	if(!empty($postFields)){
		curl_setopt($ch,CURLOPT_POST,true);
		curl_setopt($ch,CURLOPT_POSTFIELDS,$postFields);
	}
	if(!empty($header)){
		curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
	}
	$result = curl_exec($ch) or die (curl_error($ch));
	curl_close($ch);
	return $result;
}
?>