您的位置:首页 > 其它

利用百度地图采集大量某一区域经纬度信息

2016-09-26 16:46 253 查看
先在一定范围内生成经纬度随机数,再丢给百度地图反解析地址,正则匹配返回内容,作出判断,如果是我们的目标区域,则存入数据库(我主要是采集朝阳区内的经纬度信息,以下代码中我把省份等其他信息也拿下来了,第一次写博文,好紧张

<?php
include_once "Connection.php";
include_once "autoload.php";

header("Content-type: text/html; charset=utf-8");

$db = NewADOConnection('mysql');
$db->Connect('localhost', 'root', '','chartdb');
$db->Execute("set names utf8;");

for ($i = 1; $i < 10000; $i++) {
$coordone=116.4+randomOne();
$coordtwo =39.8+randomTwo();
$url = "http://api.map.baidu.com/geocoder?location=".$coordtwo.",".$coordone."&output=xml&key=aaa";//aaa处粘贴自己申请的百度api的key即可
$output = httpcurl($url);
preg_match_all("/<lat.*?>(.*?)<\/lat>/", $output, $lat);
preg_match_all("/<lng.*?>(.*?)<\/lng>/", $output, $lng);
preg_match_all("/<formatted_address.*?>(.*?)<\/formatted_address>/", $output, $formatted_address);
preg_match_all("/<business.*?>(.*?)<\/business>/", $output, $business);
preg_match_all("/<street.*?>(.*?)<\/street>/", $output, $street);
preg_match_all("/<district.*?>(.*?)<\/district>/", $output, $district);
preg_match_all("/<city.*?>(.*?)<\/city>/", $output, $city);
preg_match_all("/<province.*?>(.*?)<\/province>/", $output, $province);
preg_match_all("/<cityCode.*?>(.*?)<\/cityCode>/", $output, $cityCode);
$lat=$lat[1][0];
$lng=$lng[1][0];
$formatted_address=$formatted_address[1][0];
$business=$business[1][0];
$street=$street[1][0];
$district=$district[1][0];
$city=$city[1][0];
$province=$province[1][0];
$cityCode=$cityCode[1][0];
if ($district == "朝阳区"){
$nums= rand(0,50);
insertToMySQL($db,$lat,$lng,$formatted_address,$business,$street,$district,$city,$province,$cityCode,$nums);
}
}

function insertToMySQL($db,$lat,$lng,$formatted_address,$business,$street,$district,$city,$province,$cityCode,$nums){

$sql="insert into heatmap (lat,lng,formatted_address,business,street,district,city,province,citycode,nums)
VALUES ('{$lat}','{$lng}','{$formatted_address}','{$business}','{$street}','{$district}','{$city}','{$province}','{$cityCode}','{$nums}')";
$db->Execute($sql);
}

function randomOne($min = 0, $max = 0.2) {
return $min + mt_rand() / mt_getrandmax() * ($max - $min);
}
function randomTwo($min = 0, $max = 0.3) {
return $min + mt_rand() / mt_getrandmax() * ($max - $min);
}

function httpcurl($url, $post_data = null)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);//x
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据 百度地图