Recently, there is a project related to Baidu Map API, require automatic positioning and search function. Painstaking research for half a day, finally can be two functions into one, now put the code to share to everyone, I hope your bricks move fast and good. Note Not much, please refer to: http://lbsyun.baidu.com/index.php?title=jspopular
Note: This code is best uploaded to the Sina cloud and other free space, use the phone to open the connection can see the effect, otherwise you can not see the see OH.
<! DOCTYPE html>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<meta name= "viewport" content= "initial-scale=1.0, User-scalable=no"/>
<style type= "Text/css" >
Body, html{width:100%;height:100%;margin:0;font-family: "Microsoft Jas Black"; font-size:14px;}
Body{position:relative;}
#outMap {height:500px;width:100%;p osition:absolute;top:2.8rem;}
#r-result{width:100%;}
. Top{width:95%;height:auto;margin:. 5rem auto;position:relative;z-index:3;font-family:arial;font-size:13.3px;}
/*.box{width:100%;height:1.5rem;position:absolute;top:0;left:0;} */
. Top Input{width:80%;height:1.5rem;}
. Top. Btn{background:orangered;border:none;width:15%;height:1.5rem;
-webkit-appearance:none;
-moz-appearance:none;
Appearance:none;}
. moren{width:80.7%;height:1.3rem;border-bottom:1px solid #a9a9a9; border-left:1px solid #a9a9a9; border-right:1px Solid #a9a9a9;p Osition:absolute; }
. Img{background:url (Search.png) no-repeat;background-size:80%;}
. close{width:80.7%;height:1.1rem;line-height:1.1rem;border-bottom:1px solid #a9a9a9; border-left:1px solid #a9a9a9; border-right:1px Solid #a9a9a9;p osition:absolute;top:3.3rem;display:box;display:-webkit-box;}
. Left{width:80.7%;height:1.2rem;}
. right{-webkit-box-flex:1;box-flex:1;border-left:1px solid #a9a9a9; text-align:center;line-height:1.2rem;}
</style>
<script type= "Text/javascript" src= "Http://api.map.baidu.com/api?v=2.0&ak=6VArBpbbbMjBVP22TflHkpSq" > </script>
<script type= "Text/javascript" src= "Http://developer.baidu.com/map/jsdemo/demo/convertor.js" ></script >
<title> Baidu Map </title>
<body>
<!--steps: 1. Write a div to hold the Baidu map;-->
<!--2. After entering the company's address, get the value;--> of this input
<!--3. Click Search to make the map jump to the company's location;-->
<div class= "Top" >
<input type= "text" id= "keyword"/>
<button class= "btn" id= "Searchresultpanel" onclick= "Search ()" > Search </button>
</div>
<div id= "Outmap" ></div>
<div id= "R-result" >
</div>
</body>
<script type= "Text/javascript" >
Baidu Map API Features
Window.onload=function () {
Navigator.geolocation.getCurrentPosition (Translatepoint);
}
function Translatepoint (position) {
var currentlat = position.coords.latitude;
var Currentlon = position.coords.longitude;
var gpspoint = new Bmap.point (Currentlon, Currentlat);
BMap.Convertor.translate (gpspoint, 0, Initmap); Convert coordinates
}
var map;
function Initmap (point) {
Initialize Map
Map = new Bmap.map ("Outmap");
Map.addcontrol (New Bmap.navigationcontrol ());
Map.addcontrol (New Bmap.scalecontrol ());
Map.addcontrol (New Bmap.overviewmapcontrol ());
Map.centerandzoom (Point, 15);
Map.addoverlay (New Bmap.marker (point))
}
function Search () {
var keyword = document.getElementById ("keyword"). value;
var local = new Bmap.localsearch (map, {
Renderoptions:{map:map}
});
Local.search (keyword);
}
Positioning
var map = new Bmap.map ("Outmap");
Map.centerandzoom ("Life Science Park", 15);
var mycity = new Bmap.localcity ();
Mycity.get (function () {
alert (rs.name);
Map.setcenter (40.0747430000,116.1580900000);
// });
</script>
Finally attached to my Baidu map demo link, sent to the mobile phone can be viewed, welcome criticism: http://1.jingcode.applinzi.com/map/geolocation.html
Baidu Map API Auto-location and search function (MOBILE)