微信公众号开发之LBS

时间:2022-11-01 06:57:04

百度地图Web服务api:http://lbsyun.baidu.com/index.php?title=webapi

1.测距

Route Matrix API v2.0:http://lbsyun.baidu.com/index.php?title=webapi/route-matrix-api-v2

 case "location"://地理位置信息
$location_X = $postObj->Location_X;
$location_Y = $postObj->Location_Y;
$geourl="http://api.map.baidu.com/routematrix/v2/walking?output=xml"
."&origins=30.281341,120.209929&destinations={$location_X},{$location_Y}"
."&ak=f6UkU0QqVAhABUeGKSxZBCeb";
$apistr = file_get_contents($geourl);
$apiobj = simplexml_load_string($apistr);
$distanceobj = $apiobj->result->distance->value;//距离
$durationobj = $apiobj->result->duration->text;//时间
$label = $postObj->Label;
$contentStr = "你发送的地理位置是:"."\n".$label.",\n"."经度是:".$location_Y.",\n"."纬度是:".$location_X
.",\n"."你到我们店铺还需要步行{$distanceobj}米,大约需要{$durationobj}.";
break;

测距

2.导航

UPI API:http://lbsyun.baidu.com/index.php?title=uri/api/web(接口说明-》web端-》公交、驾车、步行导航)

 case "location"://地理位置信息
$location_X = $postObj->Location_X;//纬度
$location_Y = $postObj->Location_Y;//经度
$geourl="http://api.map.baidu.com/routematrix/v2/walking?output=xml"
."&origins=30.281341,120.209929&destinations={$location_X},{$location_Y}"
."&ak=f6UkU0QqVAhABUeGKSxZBCeb";
$apistr = file_get_contents($geourl);
$apiobj = simplexml_load_string($apistr);
$distanceobj = $apiobj->result->distance->value;//距离
$durationobj = $apiobj->result->duration->text;//时间
$label = $postObj->Label;
$contentStr = "你发送的地理位置是:"."\n".$label.",\n"."经度是:".$location_Y.",\n"."纬度是:".$location_X
.",\n"."你到我们店铺还需要步行{$distanceobj}米,大约需要{$durationobj}."."\n"
."<a href=\"http://api.map.baidu.com/direction?origin=latlng:{$location_X},{$location_Y}"
."|name:{$label}&destination=latlng:30.281341,120.209929|name:艮山西路233号新东升市场"
."&mode=walking&output=html&region=杭州&src=yourCompanyName|yourAppName\">步行导航</a>";
break;

步行导航

3.全景图

UPI API:http://lbsyun.baidu.com/index.php?title=uri/api/web(接口说明-》web端-》全景服务)

 case "location"://地理位置信息
$location_X = $postObj->Location_X;//纬度
$location_Y = $postObj->Location_Y;//经度
$geourl="http://api.map.baidu.com/routematrix/v2/walking?output=xml"
."&origins=30.281341,120.209929&destinations={$location_X},{$location_Y}"
."&ak=f6UkU0QqVAhABUeGKSxZBCeb";
$apistr = file_get_contents($geourl);
$apiobj = simplexml_load_string($apistr);
$distanceobj = $apiobj->result->distance->value;//距离
$durationobj = $apiobj->result->duration->text;//时间
$label = $postObj->Label;
$contentStr = "你发送的地理位置是:"."\n".$label.",\n"."经度是:".$location_Y.",\n"."纬度是:".$location_X
.",\n"."你到我们店铺还需要步行{$distanceobj}米,大约需要{$durationobj}."."\n"
."<a href=\"http://api.map.baidu.com/direction?origin=latlng:{$location_X},{$location_Y}"
."|name:{$label}&destination=latlng:30.281341,120.209929|name:艮山西路233号新东升市场"
."&mode=walking&output=html&region=杭州&src=yourCompanyName|yourAppName\">查看步行导航</a>"."\n"
."<a href=\"http://api.map.baidu.com/pano/?x={$location_Y}&y={$location_X}&lc=0&ak=f6UkU0QqVAhABUeGKSxZBCeb\">"
."查看你所在位置的全景图</a>";
break;

全景图

4.天气

车联网API:http://developer.baidu.com/map/wiki/index.php?title=car/api/weather(接口说明-》天气查询)

 case "天气":
$weatherurl="http://api.map.baidu.com/telematics/v3/weather?location=杭州&ak=f6UkU0QqVAhABUeGKSxZBCeb";
$apistr = file_get_contents($weatherurl);
$apiobj = simplexml_load_string($apistr);
$placeobj = $apiobj->results->currentCity;//读取城市名
$todayobj = $apiobj->results->weather_data->date;//读取当前时间
$weatherobj = $apiobj->results->weather_data->weather;//读取天气
$windobj = $apiobj->results->weather_data->wind;//读取风力
$temobj = $apiobj->results->weather_data->temperature;//读取温度
$pm25obj = $apiobj->results->pm25;//读取pm2.5
$contentStr = "{$placeobj}{$todayobj},天气:{$weatherobj},风力:{$windobj},温度:{$temobj},pm2.5:{$pm25obj}.";
break;

天气

还可以通过地理位置返回天气信息。(case "location":略)