### 數據清洗(將沒有位置信息的數據刪除)
~~~
DELETE FROM we_user WHERE province = '' OR city = ''
DELETE FROM we_user_count WHERE lng = '' OR lat = ''
~~~
> 由于部分好友未設置地理位置信息,部分好友將地理位置信息設置成國外的,而百度地圖地理編碼服務只對國內地理位置進行地理編碼。因此將這兩種數據清除。
* * * * *
### 地理位置解析(thinkphp5調用[百度地理編碼服務](http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding))
~~~
public function index()
{
$url = 'http://api.map.baidu.com/geocoder/v2/'; //請求api
$ak = '2qkjnM1Bpkac4825hyth9mGzU9xAGtDt'; //appkey
$location = Db::name('user')->field(['id','province','city'])->select();
foreach ($location as $item) {
$province = $item['province'];
$city = $item['city'];
$api_url = "$url?address=$province.$city&output=json&ak=$ak&callback=showLocation";
urlencode($api_url);
$res = $this->httpGet2($api_url);
if ($res && !empty($res['result'])) {
$res2 = Db::name('user')->where('id',$item['id'])->update([
'lng' => $res['result']['location']['lng'],
'lat' => $res['result']['location']['lat'],
]);
if ($res2) {
echo '更新成功!';
}
}
}
~~~
* * * * *
### 部分結果如下:

* * * * *
### 統計不同地區出現的人次(基于[thinkphp5](http://www.thinkphp.cn/))
~~~
//統計同一地區好友出現的次數
public function count()
{
$sql = "SELECT id,lng,lat,COUNT('lng') as 'count' FROM we_user GROUP BY lng";
$res = Db::query($sql);
foreach ($res as $item) {
$data = [
'user_id' => $item['id'],
'lng' => $item['lng'],
'lat' => $item['lat'],
'count' => $item['count']
];
$res = Db::name('user_count')->insert($data);
if ($res) {
echo "插入成功!";
}
}
}
~~~
* * * * *
> 一點小改動:由于我的好友大多分布在隴南市和蘭州市,與其它市的數量相差較大,成圖效果不理想,因此,調整了部分市的好友數量。
[TOC]