首頁 >php教程 >php手册 >微信公眾號開發之LBS

微信公眾號開發之LBS

WBOY
WBOY原創
2016-08-10 08:49:352199瀏覽

百度地圖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

<span style="color: #008080;"> 1</span> <span style="color: #0000ff;">case</span> "location":<span style="color: #008000;">//</span><span style="color: #008000;">地理位置信息</span>
<span style="color: #008080;"> 2</span>                     <span style="color: #800080;">$location_X</span> = <span style="color: #800080;">$postObj</span>-><span style="color: #000000;">Location_X;
</span><span style="color: #008080;"> 3</span>                     <span style="color: #800080;">$location_Y</span> = <span style="color: #800080;">$postObj</span>-><span style="color: #000000;">Location_Y;
</span><span style="color: #008080;"> 4</span>                     <span style="color: #800080;">$geourl</span>="http://api.map.baidu.com/routematrix/v2/walking?output=xml"
<span style="color: #008080;"> 5</span>                     ."&origins=30.281341,120.209929&destinations={<span style="color: #800080;">$location_X</span>},{<span style="color: #800080;">$location_Y</span>}"
<span style="color: #008080;"> 6</span>                     ."&ak=f6UkU0QqVAhABUeGKSxZBCeb"<span style="color: #000000;">;
</span><span style="color: #008080;"> 7</span>                     <span style="color: #800080;">$apistr</span> = <span style="color: #008080;">file_get_contents</span>(<span style="color: #800080;">$geourl</span><span style="color: #000000;">);
</span><span style="color: #008080;"> 8</span>                     <span style="color: #800080;">$apiobj</span> = <span style="color: #008080;">simplexml_load_string</span>(<span style="color: #800080;">$apistr</span><span style="color: #000000;">);
</span><span style="color: #008080;"> 9</span>                     <span style="color: #800080;">$distanceobj</span> = <span style="color: #800080;">$apiobj</span>->result->distance->value;<span style="color: #008000;">//</span><span style="color: #008000;">距离</span>
<span style="color: #008080;">10</span>                     <span style="color: #800080;">$durationobj</span> = <span style="color: #800080;">$apiobj</span>->result->duration->text;<span style="color: #008000;">//</span><span style="color: #008000;">时间</span>
<span style="color: #008080;">11</span>                     <span style="color: #800080;">$label</span> = <span style="color: #800080;">$postObj</span>-><span style="color: #000000;">Label;
</span><span style="color: #008080;">12</span>                     <span style="color: #800080;">$contentStr</span> = "你发送的地理位置是:"."\n".<span style="color: #800080;">$label</span>.",\n"."经度是:".<span style="color: #800080;">$location_Y</span>.",\n"."纬度是:".<span style="color: #800080;">$location_X</span>
<span style="color: #008080;">13</span>                     .",\n"."你到我们店铺还需要步行{<span style="color: #800080;">$distanceobj</span>}米,大约需要{<span style="color: #800080;">$durationobj</span>}."<span style="color: #000000;">;
</span><span style="color: #008080;">14</span>                     <span style="color: #0000ff;">break</span>;
測距

2.導航

UPI API:http://lbsyun.baidu.com/index.php?title=uri/api/web(介面說明-》web端-》公車、開車、步行導航)

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

3.全景圖

UPI API:http://lbsyun.baidu.com/index.php?title=uri/api/web(介面說明-》web端-》全景服務)

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

4.天氣

車聯網API:http://developer.baidu.com/map/wiki/index.php?title=car/api/weather(介面說明-》天氣查詢)

<span style="color: #008080;"> 1</span> <span style="color: #0000ff;">case</span> "天气":
<span style="color: #008080;"> 2</span>                             <span style="color: #800080;">$weatherurl</span>="http://api.map.baidu.com/telematics/v3/weather?location=杭州&ak=f6UkU0QqVAhABUeGKSxZBCeb"<span style="color: #000000;">;
</span><span style="color: #008080;"> 3</span>                             <span style="color: #800080;">$apistr</span> = <span style="color: #008080;">file_get_contents</span>(<span style="color: #800080;">$weatherurl</span><span style="color: #000000;">);
</span><span style="color: #008080;"> 4</span>                             <span style="color: #800080;">$apiobj</span> = <span style="color: #008080;">simplexml_load_string</span>(<span style="color: #800080;">$apistr</span><span style="color: #000000;">);
</span><span style="color: #008080;"> 5</span>                             <span style="color: #800080;">$placeobj</span> = <span style="color: #800080;">$apiobj</span>->results->currentCity;<span style="color: #008000;">//</span><span style="color: #008000;">读取城市名</span>
<span style="color: #008080;"> 6</span>                             <span style="color: #800080;">$todayobj</span> = <span style="color: #800080;">$apiobj</span>->results->weather_data-><span style="color: #008080;">date</span>;<span style="color: #008000;">//</span><span style="color: #008000;">读取当前时间</span>
<span style="color: #008080;"> 7</span>                             <span style="color: #800080;">$weatherobj</span> = <span style="color: #800080;">$apiobj</span>->results->weather_data->weather;<span style="color: #008000;">//</span><span style="color: #008000;">读取天气</span>
<span style="color: #008080;"> 8</span>                             <span style="color: #800080;">$windobj</span> = <span style="color: #800080;">$apiobj</span>->results->weather_data->wind;<span style="color: #008000;">//</span><span style="color: #008000;">读取风力</span>
<span style="color: #008080;"> 9</span>                             <span style="color: #800080;">$temobj</span> = <span style="color: #800080;">$apiobj</span>->results->weather_data->temperature;<span style="color: #008000;">//</span><span style="color: #008000;">读取温度</span>
<span style="color: #008080;">10</span>                             <span style="color: #800080;">$pm25obj</span> = <span style="color: #800080;">$apiobj</span>->results->pm25;<span style="color: #008000;">//</span><span style="color: #008000;">读取pm2.5</span>
<span style="color: #008080;">11</span>                             <span style="color: #800080;">$contentStr</span> = "{<span style="color: #800080;">$placeobj</span>}{<span style="color: #800080;">$todayobj</span>},天气:{<span style="color: #800080;">$weatherobj</span>},风力:{<span style="color: #800080;">$windobj</span>},温度:{<span style="color: #800080;">$temobj</span>},pm2.5:{<span style="color: #800080;">$pm25obj</span>}."<span style="color: #000000;">;
</span><span style="color: #008080;">12</span>                             <span style="color: #0000ff;">break</span>;
天氣

也可以透過地理位置返回天氣資訊。 (case "location":略)

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn