在操作页面引入以下js
<script
type="text/javascript" src=
"http://api.map.baidu.com/api?v=2.0&ak=你的密钥"></script>
<script
type="text/javascript" src=
"http://***.com/assets/js/shop.js"></script>
<input placeholder=
"请输入商铺地址" id=
"address" required=
"true" type="text" />
<input placeholder=
"点击自动获取坐标" readonly=
"true" id=
"shopcoord" type="text" />
<input id=
"getPoint" type="button" value=
"获取坐标" />
shop.js 代码:
$(
function(){
$(
'#getPoint').click(
function(){
if($(
'#address').val() ==
''){
alert(
'请填写一个地址');
}
var adds = $(
'#address').val();
getPoint(adds);
})
})
function getPoint(adds){
var myGeo =
new BMap.Geocoder();
myGeo.getPoint(adds,
function(point){
$(
'#shopcoord').val(
JSON.stringify(point));
},
"北京市");
}