http://bbs.lbsyun.baidu.com/foru ... id=90606&extra=
这是我之前【咨询求助】时发布的帖子,当时也没有解决。今天有时间处理一下这个遗留问题,终于让我弄明白了。---------》进入正题。
前奏:
我是通过百度地图获取百度坐标,能够定位显示,但是获取的百度坐标是BD09II的,百度导航又是不能直接用BD09ii,必须转换格式才行,百度地图导航这边,支持三种坐标系bd09mc(百度摩卡托坐标)、gcj02(国测局加密坐标)、wgs84(gps设备获取的坐标),根据现在情况,肯定不能使用gcj02(国测局加密坐标)、wgs84(gps设备获取的坐标)这两种了,如果非要用,那就南辕北辙了(对于我来说,嘿嘿)。所以千方百计的要实现bd09ii->bdmc的实现方式。
正题:
试了N种自己转换的方式,发现总是不对,后来通过在api首页发现有个[坐标转换APIWeb服务API] ,这个里面有关于坐标格式转换的。连接:http://developer.baidu.com/map/i ... 8.E5.9C.BA.E6.99.AF
至于内容我就不多少了,请自行查看api说明。里面有个 from=5&to=6 5代表bd09坐标,6代表bdmc坐标。啊哈,如果至宝。但是这个是通过web调用的,我现在使用的是android开发的。不怕,不怕,封装调用即可。。
1.客户端:首先先声明一个实体,便于数据封装
public class GeoConvPoint implements Serializable {
private int code;
private double lat1;
private double lng1;
private double lat2;
private double lng2;
public int getCode() {
return code;
}
public double getLat1() {
return lat1;
}
public double getLng1() {
return lng1;
}
public double getLat2() {
return lat2;
}
public double getLng2() {
return lng2;
}
public void setCode(int code) {
this.code = code;
}
public void setLat1(double lat1) {
this.lat1 = lat1;
}
public void setLng1(double lng1) {
this.lng1 = lng1;
}
public void setLat2(double lat2) {
this.lat2 = lat2;
}
public void setLng2(double lng2) {
this.lng2 = lng2;
}
}
2.客户端:定义一个方法,方法干嘛用的呢,就是为了请求使用的,走http请求的
....前面省类声明了,直接方法了
//传入经纬度
public AbstractHttpRequest<GeoConvPoint> geoConvPoint(String lat1,String lng1,String lat2,String lng2,ApiCallback<GeoConvPoint> callback) {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("lat1", lat1));
nameValuePairs.add(new BasicNameValuePair("lng1", lng1));
nameValuePairs.add(new BasicNameValuePair("lat2", lat2));
nameValuePairs.add(new BasicNameValuePair("lng2", lng2));
ApiReqeust<GeoConvPoint> apiReqeust = new DefaultApiReqeust<GeoConvPoint>(ApiReqeust.POST_METHOD, URI.create(HOST + HTTP_GEO_CONV), nameValuePairs, callback);
AbstractHttpRequest<GeoConvPoint> httpRequest = apiReqeust.obtainRequest(new GsonParser<GeoConvPoint>(GeoConvPoint.class), null, null);
NetworkManager.getInstance().requestAsync(httpRequest);
return httpRequest;
}
3.服务端:接收并请求百度web api
function convertPoint(lat1,lng1,lat2,lng2,db,res,req){
// Build the post string from an object
var coord=lng1+","+lat1+";"+lng2+","+lat2;
var post_data = qs.stringify({
'coords' : coord,
'from': '5',
'to': '6',
'ak':'你申请的ak'//需要重新申请一个web的ak,android的不行哦
});
// An object of options to indicate where to post to
var post_options = {
host: 'api.map.baidu.com',
path: '/geoconv/v1/?',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded' ,
'Content-Length': Buffer.byteLength(post_data,'utf8')
}
};
// Set up the request
var post_req = http.request(post_options, function(response) {
req.finish = true;
response.setEncoding('utf8');
var responseString = '';
response.on('data',function(chunk){
responseString += chunk;
})
response.on('end', function () {
var obj = JSON.parse(responseString);
//获取status
var sta=obj.status;
if(sta==null){
res.writeHead(404,{'Content-Type': 'text/plain','Content-Length': "xxx.".length});
res.end("User is not registered.");
}else if(sta==0){
var rest=obj.result;
var objs = {};
objs.code = 200;
objs.lat1 = rest[0].y;
objs.lng1 = rest[0].x;
objs.lat2 = rest[1].y;
objs.lng2 = rest[1].x;
var respString = JSON.stringify(objs);
res.writeHead(response.statusCode, {'Content-Type': 'text/plain','Content-Length': Buffer.byteLength(respString,'utf8')});
res.end(respString);
}
});
});
post_req.on('error', function(e) {
req.finish = true;
console.log('problem with request: ' + e.message);
res.writeHead(500,{'Content-Type': 'text/plain','Content-Length': e.message.length});
res.end(e.message);
});
// post the data
post_req.write(post_data);
post_req.end();
}
4.客户端:接收返回来的数据并封装到导航中,进入导航索引
if (obj instanceof GeoConvPoint) {
GeoConvPoint gcp=(GeoConvPoint)obj;
if(gcp.getCode()==200)
{
BNRoutePlanNode sNode = null;
BNRoutePlanNode eNode = null;
double slat=gcp.getLat1();
double slng=gcp.getLng1();
sNode = new BNRoutePlanNode(slng,slat,"", null, BNRoutePlanNode.CoordinateType.BD09_MC);
double elat=gcp.getLat2();
double elng=gcp.getLng2();
eNode = new BNRoutePlanNode(elng,elat,"", null, BNRoutePlanNode.CoordinateType.BD09_MC);
if (sNode != null && eNode != null) {
List<BNRoutePlanNode> list = new ArrayList<BNRoutePlanNode>();
list.add(sNode);
list.add(eNode);
BaiduNaviManager.getInstance().launchNavigator(this, list, 1, true, new NavRoutePlanListener(sNode));
}
}
}
------------------------->
这是我之前【咨询求助】时发布的帖子,当时也没有解决。今天有时间处理一下这个遗留问题,终于让我弄明白了。---------》进入正题。
前奏:
我是通过百度地图获取百度坐标,能够定位显示,但是获取的百度坐标是BD09II的,百度导航又是不能直接用BD09ii,必须转换格式才行,百度地图导航这边,支持三种坐标系bd09mc(百度摩卡托坐标)、gcj02(国测局加密坐标)、wgs84(gps设备获取的坐标),根据现在情况,肯定不能使用gcj02(国测局加密坐标)、wgs84(gps设备获取的坐标)这两种了,如果非要用,那就南辕北辙了(对于我来说,嘿嘿)。所以千方百计的要实现bd09ii->bdmc的实现方式。
正题:
试了N种自己转换的方式,发现总是不对,后来通过在api首页发现有个[坐标转换APIWeb服务API] ,这个里面有关于坐标格式转换的。连接:http://developer.baidu.com/map/i ... 8.E5.9C.BA.E6.99.AF
至于内容我就不多少了,请自行查看api说明。里面有个 from=5&to=6 5代表bd09坐标,6代表bdmc坐标。啊哈,如果至宝。但是这个是通过web调用的,我现在使用的是android开发的。不怕,不怕,封装调用即可。。
1.客户端:首先先声明一个实体,便于数据封装
public class GeoConvPoint implements Serializable {
private int code;
private double lat1;
private double lng1;
private double lat2;
private double lng2;
public int getCode() {
return code;
}
public double getLat1() {
return lat1;
}
public double getLng1() {
return lng1;
}
public double getLat2() {
return lat2;
}
public double getLng2() {
return lng2;
}
public void setCode(int code) {
this.code = code;
}
public void setLat1(double lat1) {
this.lat1 = lat1;
}
public void setLng1(double lng1) {
this.lng1 = lng1;
}
public void setLat2(double lat2) {
this.lat2 = lat2;
}
public void setLng2(double lng2) {
this.lng2 = lng2;
}
}
2.客户端:定义一个方法,方法干嘛用的呢,就是为了请求使用的,走http请求的
....前面省类声明了,直接方法了
//传入经纬度
public AbstractHttpRequest<GeoConvPoint> geoConvPoint(String lat1,String lng1,String lat2,String lng2,ApiCallback<GeoConvPoint> callback) {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("lat1", lat1));
nameValuePairs.add(new BasicNameValuePair("lng1", lng1));
nameValuePairs.add(new BasicNameValuePair("lat2", lat2));
nameValuePairs.add(new BasicNameValuePair("lng2", lng2));
ApiReqeust<GeoConvPoint> apiReqeust = new DefaultApiReqeust<GeoConvPoint>(ApiReqeust.POST_METHOD, URI.create(HOST + HTTP_GEO_CONV), nameValuePairs, callback);
AbstractHttpRequest<GeoConvPoint> httpRequest = apiReqeust.obtainRequest(new GsonParser<GeoConvPoint>(GeoConvPoint.class), null, null);
NetworkManager.getInstance().requestAsync(httpRequest);
return httpRequest;
}
3.服务端:接收并请求百度web api
function convertPoint(lat1,lng1,lat2,lng2,db,res,req){
// Build the post string from an object
var coord=lng1+","+lat1+";"+lng2+","+lat2;
var post_data = qs.stringify({
'coords' : coord,
'from': '5',
'to': '6',
'ak':'你申请的ak'//需要重新申请一个web的ak,android的不行哦
});
// An object of options to indicate where to post to
var post_options = {
host: 'api.map.baidu.com',
path: '/geoconv/v1/?',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded' ,
'Content-Length': Buffer.byteLength(post_data,'utf8')
}
};
// Set up the request
var post_req = http.request(post_options, function(response) {
req.finish = true;
response.setEncoding('utf8');
var responseString = '';
response.on('data',function(chunk){
responseString += chunk;
})
response.on('end', function () {
var obj = JSON.parse(responseString);
//获取status
var sta=obj.status;
if(sta==null){
res.writeHead(404,{'Content-Type': 'text/plain','Content-Length': "xxx.".length});
res.end("User is not registered.");
}else if(sta==0){
var rest=obj.result;
var objs = {};
objs.code = 200;
objs.lat1 = rest[0].y;
objs.lng1 = rest[0].x;
objs.lat2 = rest[1].y;
objs.lng2 = rest[1].x;
var respString = JSON.stringify(objs);
res.writeHead(response.statusCode, {'Content-Type': 'text/plain','Content-Length': Buffer.byteLength(respString,'utf8')});
res.end(respString);
}
});
});
post_req.on('error', function(e) {
req.finish = true;
console.log('problem with request: ' + e.message);
res.writeHead(500,{'Content-Type': 'text/plain','Content-Length': e.message.length});
res.end(e.message);
});
// post the data
post_req.write(post_data);
post_req.end();
}
4.客户端:接收返回来的数据并封装到导航中,进入导航索引
if (obj instanceof GeoConvPoint) {
GeoConvPoint gcp=(GeoConvPoint)obj;
if(gcp.getCode()==200)
{
BNRoutePlanNode sNode = null;
BNRoutePlanNode eNode = null;
double slat=gcp.getLat1();
double slng=gcp.getLng1();
sNode = new BNRoutePlanNode(slng,slat,"", null, BNRoutePlanNode.CoordinateType.BD09_MC);
double elat=gcp.getLat2();
double elng=gcp.getLng2();
eNode = new BNRoutePlanNode(elng,elat,"", null, BNRoutePlanNode.CoordinateType.BD09_MC);
if (sNode != null && eNode != null) {
List<BNRoutePlanNode> list = new ArrayList<BNRoutePlanNode>();
list.add(sNode);
list.add(eNode);
BaiduNaviManager.getInstance().launchNavigator(this, list, 1, true, new NavRoutePlanListener(sNode));
}
}
}
------------------------->
至此全部完结,相信大家都明白了吧。是不是easy~~~
ps:本人于2015-09-18 发表于百度地图API链接: http://bbs.lbsyun.baidu.com/forum.php?mod=viewthread&tid=99440&extra=