1.详细资料请参照百度地图开放平台
http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding
2.需要在平台申请一个ak
3.controller代码
@RequestMapping("/getnewgps")
public @ResponseBody GpsModel GetNewGps(Message msg, GpsModel gpsModel, String pos){
try {
//PostUtil.PostForJson是把url转换成json格式
String gpsstring=PostUtil.PostForJson("http://api.map.baidu.com/geocoder/v2/?callback=renderOption&output=json&address="+pos+"&city=上海市&ak=f7VO0mgzPaztvtZqFVurHFwRpVrBCtdP", null);
gpsstring = gpsstring.substring(gpsstring.indexOf("(") + 1, gpsstring.indexOf(")"));
JSONObject json = JSONObject.fromObject(gpsstring);
int status = Integer.valueOf(json.get("status").toString());
if(status == 1){
return new GpsModel();
}
Gps g = (Gps) JSONObject.toBean(json,Gps.class);
gpsModel.setLat(g.getResult().getLocation().getLat());
gpsModel.setLng(g.getResult().getLocation().getLng());
return gpsModel;
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
return new GpsModel();
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
return new GpsModel();
}
}
4.JS代码
$('#modeposition').on('click',function(){
var locan = $('#spadr1').val();
var spids = $('.spid').val();
$.post('getgps.action',{pos:locan,spid:spids},function(data){
if(data.genre == 0) alert("地址异常");
else{
alert("定位成功");
$('#isfindm').text("已定位");
}
});
});
5.需要注意的地方
<a href="http://api.map.baidu.com/geocoder/v2/?callback=renderOption&output=json&address=\" +pos+\"&city="上海市&ak=f7VO0mgzPaztvtZqFVurHFwRpVrBCtdP"">http://api.map.baidu.com/geocoder/v2/?callback=renderOption&output=json&address="+pos+"&city=上海市&ak=f7VO0mgzPaztvtZqFVurHFwRpVrBCtdP
5.1 http://api.map.baidu.com/geocoder/v2/?为API借口
5.2 output为json
5.3 address为你需要定位的地址
5.4 city为不必要填写的项
5.5 ak为API借口的密钥,需要申请
http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding
2.需要在平台申请一个ak
3.controller代码
@RequestMapping("/getnewgps")
public @ResponseBody GpsModel GetNewGps(Message msg, GpsModel gpsModel, String pos){
try {
//PostUtil.PostForJson是把url转换成json格式
String gpsstring=PostUtil.PostForJson("http://api.map.baidu.com/geocoder/v2/?callback=renderOption&output=json&address="+pos+"&city=上海市&ak=f7VO0mgzPaztvtZqFVurHFwRpVrBCtdP", null);
gpsstring = gpsstring.substring(gpsstring.indexOf("(") + 1, gpsstring.indexOf(")"));
JSONObject json = JSONObject.fromObject(gpsstring);
int status = Integer.valueOf(json.get("status").toString());
if(status == 1){
return new GpsModel();
}
Gps g = (Gps) JSONObject.toBean(json,Gps.class);
gpsModel.setLat(g.getResult().getLocation().getLat());
gpsModel.setLng(g.getResult().getLocation().getLng());
return gpsModel;
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
return new GpsModel();
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
return new GpsModel();
}
}
4.JS代码
$('#modeposition').on('click',function(){
var locan = $('#spadr1').val();
var spids = $('.spid').val();
$.post('getgps.action',{pos:locan,spid:spids},function(data){
if(data.genre == 0) alert("地址异常");
else{
alert("定位成功");
$('#isfindm').text("已定位");
}
});
});
5.需要注意的地方
<a href="http://api.map.baidu.com/geocoder/v2/?callback=renderOption&output=json&address=\" +pos+\"&city="上海市&ak=f7VO0mgzPaztvtZqFVurHFwRpVrBCtdP"">http://api.map.baidu.com/geocoder/v2/?callback=renderOption&output=json&address="+pos+"&city=上海市&ak=f7VO0mgzPaztvtZqFVurHFwRpVrBCtdP
5.1 http://api.map.baidu.com/geocoder/v2/?为API借口
5.2 output为json
5.3 address为你需要定位的地址
5.4 city为不必要填写的项
5.5 ak为API借口的密钥,需要申请