<!doctype html>
<html> <head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
<title>地理编码</title>
<link rel="stylesheet" href="http://cache.amap.com/lbs/static/main1119.css" />
<script type="text/javascript" src="http://webapi.amap.com/maps?v=1.3&key=25570139c46c9bc652ded0d3be576696&plugin=AMap.Geocoder"></script>
<script type="text/javascript" src="http://cache.amap.com/lbs/static/addToolbar.js"></script>
</head> <body onload="geocoder()">
<div id="container"></div>
<div id="tip"> <span id="result"></span>
</div>
<script type="text/javascript">
var map = new AMap.Map("container", {
resizeEnable: true
}); function geocoder() {
var geocoder = new AMap.Geocoder({
city: "", //城市,默认:“全国”
radius: //范围,默认:500
});
//地理编码,返回地理编码结果
geocoder.getLocation("北京市海淀区苏州街", function (status, result) {
if (status === 'complete' && result.info === 'OK') {
var start = geocoder_CallBack(result);
window.sessionStorage.setItem('start', JSON.stringify(start)); }
});
geocoder.getLocation("深圳大学", function (status, result) {
if (status === 'complete' && result.info === 'OK') {
var end = geocoder_CallBack(result);
var start = JSON.parse(window.sessionStorage.getItem('start'));
var locMsg = {
startLng: start.lng,
startLat: start.lat,
endLng: end.lng,
endLat: end.lat,
startLoc: start.adr,
endLoc: end.adr
};
window.sessionStorage.setItem('locMsg', JSON.stringify(locMsg));
console.log(window.sessionStorage.getItem('locMsg'));
}
});
}
//地理编码返回结果展示
function geocoder_CallBack(data) {
//地理编码结果数组
var geocode = data.geocodes;
var obj = {
lng: geocode[].location.lng,
lat: geocode[].location.lat,
adr: geocode[].formattedAddress
}
return obj;
} </script>
</body> </html>