具体步骤如下:
引入
<link rel="stylesheet" href="css/main1119.css" />
<script type="text/javascript" src="http://webapi.amap.com/maps?v=1.4.4&key=ad26780bfaabf0ca27ae6078e2e81682"></script>
<script type="text/javascript" src="js/addToolbar.js"></script>
main1119.css内容:
html, body {
margin: 0;
height: 100%;
width: 100%;
position: absolute;
}
#container {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
width: 100%;
height: 100%;
}
.button-group {
position: absolute;
bottom: 20px;
right: 20px;
font-size: 12px;
padding: 10px;
}
.button-group .button {
height: 28px;
line-height: 28px;
background-color: #0D9BF2;
color: #FFF;
border: 0;
outline: none;
padding-left: 5px;
padding-right: 5px;
border-radius: 3px;
margin-bottom: 4px;
cursor: pointer;
}
.button-group .inputtext {
height: 26px;
line-height: 26px;
border: 1px;
outline: none;
padding-left: 5px;
padding-right: 5px;
border-radius: 3px;
margin-bottom: 4px;
cursor: pointer;
}
/*
.tip {
position: absolute;
bottom: 30px;
right: 10px;
background-color: #FFF;
text-align: center;
border: 1px solid #ccc;
line-height: 30px;
border-radius: 3px;
padding: 0 5px;
font-size: 12px;
}
*/
#tip {
background-color: #fff;
padding-left: 10px;
padding-right: 10px;
position: absolute;
font-size: 12px;
right: 10px;
top: 20px;
border-radius: 3px;
border: 1px solid #ccc;
line-height: 30px;
}
/*
#tip input[type='button'] {
margin-top: 10px;
margin-bottom: 10px;
background-color: #0D9BF2;
height: 30px;
text-align: center;
line-height: 30px;
color: #fff;
font-size: 12px;
border-radius: 3px;
outline: none;
border: 0;
}
*/
.amap-info-content {
font-size: 12px;
}
#myPageTop {
position: absolute;
top: 5px;
right: 10px;
background: #fff none repeat scroll 0 0;
border: 1px solid #ccc;
margin: 10px auto;
padding:6px;
font-family: "Microsoft Yahei", "微软雅黑", "Pinghei";
font-size: 14px;
}
#myPageTop label {
margin: 0 20px 0 0;
color: #666666;
font-weight: normal;
}
#myPageTop input {
width: 170px;
}
#myPageTop .column2{
padding-left: 25px;
}
addToolbar.js 内容:
window.onload = function() {
map.plugin(["AMap.ToolBar"], function() {
map.addControl(new AMap.ToolBar());
});
if(location.href.indexOf('&guide=1')!==-1){
map.setStatus({scrollWheel:false})
}
}
html body 内容:
<div style="width:auto;height:340px;border:#ccc solid 1px;" id="dituContent"></div>
<div id="tip"></div>
js文件
<script type="text/javascript">
/***************************************
由于Chrome、IOS10等已不再支持非安全域的浏览器定位请求,为保证定位成功率和精度,请尽快升级您的站点到HTTPS。
***************************************/
var map, geolocation;
//加载地图,调用浏览器定位服务
map = new AMap.Map('dituContent', {
resizeEnable: true
});
map.plugin('AMap.Geolocation', function() {
geolocation = new AMap.Geolocation({
enableHighAccuracy: true, //是否使用高精度定位,默认:true
timeout: 10000, //超过10秒后停止定位,默认:无穷大
buttonOffset: new AMap.Pixel(10, 20), //定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)
zoomToAccuracy: true, //定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:false
buttonPosition: 'RB'
});
map.addControl(geolocation);
geolocation.getCurrentPosition();
AMap.event.addListener(geolocation, 'complete', onComplete); //返回定位信息
AMap.event.addListener(geolocation, 'error', onError); //返回定位出错信息
});
//解析定位结果
function onComplete(data) {
var str = [];
str.push(data.position.getLat());
str.push(data.position.getLng());
document.getElementById('tip').innerHTML = str;
}
//解析定位错误信息
function onError(data) {
document.getElementById('tip').innerHTML = '定位失败';
}
</script>