並非全部 LBS 雲服務 均可以使用 js Ajax 訪問,涉及跨域問題 (Jsonp 方式解決)
Jsonp 解決跨域問題原理,在頁面生成<script> 加載遠程 js 代碼片斷.
在LBS的服務文檔:http://lbsyun.baidu.com/index.php?title=lbscloud/api/geosearch,除了雲存儲都有callback回調函數.
在jQuery的API文檔中 $.getJSON(url,[data],function(data))加入callback=?執行回調函數.
package cn.itcast.map; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.http.Consts; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import org.junit.Test; // 雲存儲
public class BaiduMapCloudStorageTest { // 建立表
@Test public void demo1() throws IOException { HttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost("http://api.map.baidu.com/geodata/v3/geotable/create"); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("name", "mytable1")); nameValuePairs.add(new BasicNameValuePair("geotype", "1")); nameValuePairs.add(new BasicNameValuePair("is_published", "1")); nameValuePairs.add(new BasicNameValuePair("ak", "zbLsuDDL4CS2U0M4KezOZZbGUY9iWtVf")); httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); System.out.println(EntityUtils.toString(httpEntity)); } @Test // 查詢表
public void demo2() throws IOException { HttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet( "http://api.map.baidu.com/geodata/v3/geotable/list?ak=zbLsuDDL4CS2U0M4KezOZZbGUY9iWtVf"); HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); System.out.println(EntityUtils.toString(httpEntity)); } @Test // 查詢表結構
public void demo3() throws IOException { HttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet( "http://api.map.baidu.com/geodata/v3/geotable/detail?id=153944&ak=zbLsuDDL4CS2U0M4KezOZZbGUY9iWtVf"); HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); System.out.println(EntityUtils.toString(httpEntity)); } @Test // 建立列
public void demo4() throws IOException { HttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost("http://api.map.baidu.com/geodata/v3/column/create"); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("geotable_id", "153944")); nameValuePairs.add(new BasicNameValuePair("name", "名稱")); nameValuePairs.add(new BasicNameValuePair("key", "name")); nameValuePairs.add(new BasicNameValuePair("type", "3")); nameValuePairs.add(new BasicNameValuePair("max_length", "512")); nameValuePairs.add(new BasicNameValuePair("is_sortfilter_field", "0")); nameValuePairs.add(new BasicNameValuePair("is_search_field", "1")); nameValuePairs.add(new BasicNameValuePair("is_index_field", "1")); nameValuePairs.add(new BasicNameValuePair("is_unique_field", "1")); nameValuePairs.add(new BasicNameValuePair("ak", "zbLsuDDL4CS2U0M4KezOZZbGUY9iWtVf")); httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); System.out.println(EntityUtils.toString(httpEntity)); } @Test // 查詢列
public void demo5() throws IOException { HttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet( "http://api.map.baidu.com/geodata/v3/column/list?geotable_id=153944&ak=zbLsuDDL4CS2U0M4KezOZZbGUY9iWtVf"); HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); System.out.println(EntityUtils.toString(httpEntity)); } @Test // 查詢指定id列
public void demo6() throws IOException { HttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet( "http://api.map.baidu.com/geodata/v3/column/detail?id=265695&geotable_id=153944&ak=zbLsuDDL4CS2U0M4KezOZZbGUY9iWtVf"); HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); System.out.println(EntityUtils.toString(httpEntity)); } @Test // 修改列
public void demo7() throws IOException { HttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost("http://api.map.baidu.com/geodata/v3/column/update"); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("id", "265695")); nameValuePairs.add(new BasicNameValuePair("geotable_id", "153944")); nameValuePairs.add(new BasicNameValuePair("name", "描述")); nameValuePairs.add(new BasicNameValuePair("key", "name")); nameValuePairs.add(new BasicNameValuePair("type", "3")); nameValuePairs.add(new BasicNameValuePair("max_length", "512")); nameValuePairs.add(new BasicNameValuePair("is_sortfilter_field", "0")); nameValuePairs.add(new BasicNameValuePair("is_search_field", "1")); nameValuePairs.add(new BasicNameValuePair("is_index_field", "1")); nameValuePairs.add(new BasicNameValuePair("is_unique_field", "1")); nameValuePairs.add(new BasicNameValuePair("ak", "zbLsuDDL4CS2U0M4KezOZZbGUY9iWtVf")); httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, Consts.UTF_8)); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); System.out.println(EntityUtils.toString(httpEntity)); } @Test // 建立數據 (create poi)
public void demo8() throws IOException { HttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost("http://api.map.baidu.com/geodata/v3/poi/create"); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("geotable_id", "153944")); nameValuePairs.add(new BasicNameValuePair("title", "好嫂子")); nameValuePairs.add(new BasicNameValuePair("address", "海淀區建材城西路xx號")); nameValuePairs.add(new BasicNameValuePair("latitude", "39.899552")); nameValuePairs.add(new BasicNameValuePair("longitude", "116.647367")); nameValuePairs.add(new BasicNameValuePair("coord_type", "3")); nameValuePairs.add(new BasicNameValuePair("ak", "zbLsuDDL4CS2U0M4KezOZZbGUY9iWtVf")); httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); System.out.println(EntityUtils.toString(httpEntity)); } @Test // 查詢指定條件數據(poi)
public void demo9() throws IOException { HttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet( "http://api.map.baidu.com/geodata/v3/poi/list?geotable_id=153944&ak=zbLsuDDL4CS2U0M4KezOZZbGUY9iWtVf"); HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); System.out.println(EntityUtils.toString(httpEntity)); } @Test // 查詢指定id的數據(poi)
public void demo10() throws IOException { HttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet( "http://api.map.baidu.com/geodata/v3/poi/detail?id=1822675338&geotable_id=153944&ak=zbLsuDDL4CS2U0M4KezOZZbGUY9iWtVf"); HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); System.out.println(EntityUtils.toString(httpEntity)); } @Test // 修改數據 (update poi)
public void demo11() throws IOException { HttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost("http://api.map.baidu.com/geodata/v3/poi/update"); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("id", "1822675338")); nameValuePairs.add(new BasicNameValuePair("geotable_id", "153944")); nameValuePairs.add(new BasicNameValuePair("title", "好嫂子")); nameValuePairs.add(new BasicNameValuePair("address", "海淀區建材城西路xx號")); nameValuePairs.add(new BasicNameValuePair("latitude", "40.066269")); nameValuePairs.add(new BasicNameValuePair("longitude", "116.353776")); nameValuePairs.add(new BasicNameValuePair("coord_type", "3")); nameValuePairs.add(new BasicNameValuePair("ak", "zbLsuDDL4CS2U0M4KezOZZbGUY9iWtVf")); httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, Consts.UTF_8)); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); System.out.println(EntityUtils.toString(httpEntity)); } @Test public void demoX() { System.out.println("\u53c2\u6570\u5fc5\u9700"); } }
2.BaiduMapCloudSearchjavascript
package cn.itcast.map; import java.io.IOException; import java.net.URLEncoder; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.junit.Test; // 雲檢索
public class BaiduMapCloudSearchTest { @Test // 周邊檢索
public void demo1() throws IOException { HttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet( "http://api.map.baidu.com/geosearch/v3/nearby?ak=zbLsuDDL4CS2U0M4KezOZZbGUY9iWtVf&geotable_id=153944&location=116.647367,39.899552&radius=1000"); HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); System.out.println(EntityUtils.toString(httpEntity)); } @Test // 本地檢索
public void demo2() throws IOException { HttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet( "http://api.map.baidu.com/geosearch/v3/local?region=北京&ak=zbLsuDDL4CS2U0M4KezOZZbGUY9iWtVf&geotable_id=153944&location=116.647367,39.899552&radius=1000"); HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); System.out.println(EntityUtils.toString(httpEntity)); } @Test // 雲地理編碼
public void demo3() throws IOException { String address = URLEncoder.encode("好嫂子", "utf-8"); String url = "http://api.map.baidu.com/cloudgc/v1?geotable_id=153944&ak=zbLsuDDL4CS2U0M4KezOZZbGUY9iWtVf&address="
+ address; System.out.println(url); HttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(url); HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); System.out.println(EntityUtils.toString(httpEntity)); } }
3.BaiduMapWebServiceTestphp
package cn.itcast.map; import java.io.IOException; import java.net.URLEncoder; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.junit.Test; // web開放服務
public class BaiduMapWebServiceTest { @Test // Place Suggestion API
public void demo1() throws IOException { HttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet( "http://api.map.baidu.com/place/v2/suggestion?ak=zbLsuDDL4CS2U0M4KezOZZbGUY9iWtVf®ion=全國&q=和平&output=json"); HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); System.out.println(EntityUtils.toString(httpEntity)); } @Test // 地理編碼 api
public void demo2() throws IOException { HttpClient httpClient = HttpClients.createDefault(); String address = URLEncoder.encode("北京市海淀區上地十街10號", "utf-8"); HttpGet httpGet = new HttpGet( "http://api.map.baidu.com/geocoder/v2/?ak=zbLsuDDL4CS2U0M4KezOZZbGUY9iWtVf&output=json&address="
+ address); HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); System.out.println(EntityUtils.toString(httpEntity)); } @Test // 路線規劃距離和行駛時間
public void demo3() throws IOException { HttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet( "http://api.map.baidu.com/routematrix/v2/riding?ak=zbLsuDDL4CS2U0M4KezOZZbGUY9iWtVf&output=json&origins=40.056878,116.30815&destinations=40.063597,116.364973"); HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); System.out.println(EntityUtils.toString(httpEntity)); } @Test // 路線規劃
public void demo4() throws IOException { HttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet( "http://api.map.baidu.com/direction/v2/transit?ak=zbLsuDDL4CS2U0M4KezOZZbGUY9iWtVf&output=json&origin=40.056878,116.30815&destination=40.063597,116.364973"); HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); System.out.println(EntityUtils.toString(httpEntity)); } @Test // ip高精度定位
public void demo5() throws IOException { HttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet( "http://api.map.baidu.com/highacciploc/v1?ak=zbLsuDDL4CS2U0M4KezOZZbGUY9iWtVf&qterm=pc&callback_type=json&coord=bd09ll"); HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); System.out.println(EntityUtils.toString(httpEntity)); } @Test // 轉換座標
public void demo6() throws IOException { HttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet( "http://api.map.baidu.com/geoconv/v1/?ak=zbLsuDDL4CS2U0M4KezOZZbGUY9iWtVf&output=json&coords=114.21892734521,29.575429778924"); HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); System.out.println(EntityUtils.toString(httpEntity)); } }
4.周邊檢索( 基於 JavaScript 訪問 LBS 雲服務)html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>周邊檢索 </title> <script type="text/javascript" src="js/jquery.js" ></script> <script type="text/javascript"> $.getJSON("http://api.map.baidu.com/geosearch/v3/nearby?ak=zbLsuDDL4CS2U0M4KezOZZbGUY9iWtVf&geotable_id=153944&q=好嫂子&location=116.395884,39.932154&radius=5000&callback=?",function(data){ console.log(data); }); </script> </head> <body> </body> </html>
5.本地檢索( 基於 JavaScript 訪問 LBS 雲服務)java
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>本地檢索 </title> <script type="text/javascript" src="js/jquery.js" ></script> <script type="text/javascript"> $.getJSON("http://api.map.baidu.com/geosearch/v3/local?ak=zbLsuDDL4CS2U0M4KezOZZbGUY9iWtVf&geotable_id=153944&q=好嫂子®ion=北京市&callback=?",function(data){ console.log(data); }); </script> </head> <body> </body> </html>
6.雲地理檢索 ( 基於 JavaScript 訪問 LBS 雲服務)jquery
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>雲地理編碼</title> <script type="text/javascript" src="js/jquery.js" ></script> <script type="text/javascript"> var address = encodeURIComponent("金燕龍辦公樓"); $.getJSON("http://api.map.baidu.com/cloudgc/v1?ak=zbLsuDDL4CS2U0M4KezOZZbGUY9iWtVf&address="+address+"&callback=?",function(data){ console.log(data); }); </script> </head> <body> </body> </html>