Rumah  >  Artikel  >  php教程  >  微信公众号开发之LBS

微信公众号开发之LBS

WBOY
WBOYasal
2016-08-10 08:49:352160semak imbas

百度地图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="%5C%22http://api.map.baidu.com/direction?origin=latlng:%7B<span" style="color: #800080;">$location_X},{<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="%5C%22http://api.map.baidu.com/direction?origin=latlng:%7B<span" style="color: #800080;">$location_X},{<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="%5C%22http://api.map.baidu.com/pano/?x=%7B<span" style="color: #800080;">$location_Y}&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":略)

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn