百度提供的LBS服务
并不是所有 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.BaiduMapCloudSearch
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.BaiduMapWebServiceTest
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 云服务)
<!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 云服务)
<!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 云服务)
<!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>
百度提供的LBS服务的更多相关文章
- Android 系统api实现定位及使用百度提供的api来实现定位
目前在国内使用定位的方法主要是 1. Android系统提供的 LocationManager locationManager = (LocationManager) getSystemService ...
- 云 MongoDB 优化让 LBS 服务性能提升十倍
欢迎大家前往腾讯云技术社区,获取更多腾讯海量技术实践干货哦~ 随着国内服务共享化的热潮普及,共享单车,共享雨伞,共享充电宝等各种服务如雨后春笋,随之而来的LBS服务定位问题成为了后端服务的一个挑战.M ...
- 必看!如何让你的LBS服务性能提升十倍!
本文由云+社区发表 作者:腾讯云数据库团队 随着国内服务共享化的热潮普及,共享单车,共享雨伞,共享充电宝等各种服务如雨后春笋,随之而来的LBS服务定位问题成为了后端服务的一个挑战.MongoDB对LB ...
- Python flask 基于 Flask 提供 RESTful Web 服务
转载自 http://python.jobbole.com/87118/ 什么是 REST REST 全称是 Representational State Transfer,翻译成中文是『表现层状态转 ...
- 站内全文检索服务来了,Xungle提供免费全文检索服务
免费站内全文检索服务来了,是的,你没听错.全文检索相信大家已经不太陌生,主流检索服务有sphinx.xunsearch等,但这些都受服务器限制,对于中小站长尤其是没有服务器实现就困难了,随着数据量的增 ...
- WebService学习总结(四)——调用第三方提供的webService服务
http://www.cnblogs.com/xdp-gacl/p/4260627.html 互联网上面有很多的免费webService服务,我们可以调用这些免费的WebService服务,将一些其他 ...
- WebService-调用第三方提供的webService服务
互联网上面有很多的免费webService服务,我们可以调用这些免费的WebService服务,将一些其他网站的内容信息集成到我们的Web应用中显示,下面就以获取天气预报数据和查询国内手机号码归属地为 ...
- heartbeat单独提供高可用服务
本文目录:1.简介2.安装heartbeat 2.1 编译安装Heartbeat3.heartbeat相关配置文件 3.1 配置文件ha.cf 3.2 配置文件authkeys 3.3 配置文件har ...
- WebService学习--(四)调用第三方提供的webService服务
互联网上面有很多的免费webService服务,我们可以调用这些免费的WebService服务,将一些其他网站的内容信息集成到我们的Web应用中显示,下面就以获取天气预报数据和查询国内手机号码归属地为 ...
随机推荐
- 原生态hadoop2.6平台搭建
hadoop2.6平台搭建 一.条件准备 软件条件: Ubuntu14.04 64位操作系统,jdk1.7 64位,Hadoop 2.6.0 硬件条件: 1台主节点机器,配置:cpu 8个,内存32 ...
- (转)Linux SSH批量分发管理
Linux SSH批量分发管理 原文:http://blog.51cto.com/chenfage/1831166 第1章 SSH服务基础介绍 1.1 SSH服务 1.1.1SSH介绍 SSH是Sec ...
- 关于GitHub在VS中出现“已经存在master版本,无法……”的错误解决方案
引用:http://www.cnblogs.com/SmallZL/p/3637613.html(这篇已经很详细说明如何使用Vs+GitHub),我这里做补充: VS2013已经集成了Git一部分控件 ...
- 如何让JS变量和字符串拼接后,是变量而不是字符串
今天有个非常有趣的事,因为我需要用JS去实现多语言,就是我在JS文件里定义了不同的变量,尝试用变量拼接字符串组成之前定义好的变量名称,结果拼接之后,显示的却是字符串,而不是变量,所以无法解析 zh_t ...
- SVM 之 MATLAB 实现代码
MATLAB 中 SVM 实现 直接上代码 main.m %% Initialize data clear, clc, close all; load('data.mat'); y(y == 0) = ...
- nyoj 568——RMQ with Shifts——————【线段树单点更新、区间求最值】
RMQ with Shifts 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 In the traditional RMQ (Range Minimum Q ...
- Linux VFS机制简析(二)
Linux VFS机制简析(二) 接上一篇Linux VFS机制简析(一),本篇继续介绍有关Address space和address operations.file和file operations. ...
- 04.Path类的学习
path 是路径的意思. path类是一个静态类,所以path是一个工具类. Path类是专门用来操作路径的. Path的常用方法: namespace _15.Path类的学习 { class Pr ...
- XML深入了解(XML JavaSprint)
XMLHttpRequest 对象 XMLHttpRequest 对象用于在后台与服务器交换数据. XMLHttpRequest 对象是开发者的梦想,因为您能够: 在不重新加载页面的情况下更新网页 在 ...
- hibernate的各种保存方式的区别 (save,persist,update,saveOrUpdte,merge,flush,lock)
hibernate的保存hibernate对于对象的保存提供了太多的方法,他们之间有很多不同,这里细说一下,以便区别:一.预备知识:在所有之前,说明一下,对于hibernate,它的对象有三种状态,t ...