The code copy is as follows:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Query latitude and longitude based on address</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="http://api.map.baidu.com/api?v=1.3"></script>
</head>
<body style="background:#CBE1FF">
<div>
The address to be queryed: <input id="text_" type="text" value="Xuzhou Gupeng Square" style="margin-right:100px;"/>
Query result (latitude and longitude): <input id="result_" type="text" />
<input type="button" value="query" onclick="searchByStationName();"/>
<div id="container"
style="position: absolute;
margin-top:30px;
width: 730px;
height: 590px;
top: 50;
border: 1px solid gray;
overflow:hidden;">
</div>
</div>
</body>
<script type="text/javascript">
var map = new BMap.Map("container");
map.centerAndZoom("Xuzhou", 12);
map.enableScrollWheelZoom(); //Enable scroll wheel zoom in and out, disabled by default
map.enableContinuousZoom(); //Enable map inertia drag and drop, disabled by default
map.addControl(new BMap.NavigationControl()); //Add default zoom pan control
map.addControl(new BMap.OverviewMapControl()); //Add default thumbnail map control
map.addControl(new BMap.OverviewMapControl({ isOpen: true, anchor: BMAP_ANCHOR_BOTTOM_RIGHT })); //The lower right corner, open
var localSearch = new BMap.LocalSearch(map);
localSearch.enableAutoViewport(); //Allows automatic adjustment of form size
function searchByStationName() {
map.clearOverlays();//Clear the original tag
var keyword = document.getElementById("text_").value;
localSearch.setSearchCompleteCallback(function (searchResult) {
var poi = searchResult.getPoi(0);
document.getElementById("result_").value = poi.point.lng + "," + poi.point.lat;
map.centerAndZoom(poi.point, 13);
var marker = new BMap.Marker(new BMap.Point(poi.point.lng, poi.point.lat)); // Create annotation to be the corresponding latitude and longitude of the place to be queryed
map.addOverlay(marker);
var content = document.getElementById("text_").value + "<br/><br/>Long: " + poi.point.lng + "<br/>Lat: " + poi.point.lat;
var infoWindow = new BMap.InfoWindow("<p style='font-size:14px;'>" + content + "</p>");
marker.addEventListener("click", function () { this.openInfoWindow(infoWindow); });
// marker.setAnimation(BMAP_ANIMATION_BOUNCE); // Beat animation
});
localSearch.search(keyword);
}
</script>
</html>
The above is all the code. Friends can use it directly in the project. Don’t say thank you to me, please call me Lei Feng~