Use the Baidu map api to query the longitude and latitude Based on the address and the api address

Source: Internet
Author: User

Use the Baidu map api to query the longitude and latitude Based on the address and the api address

Copy codeThe Code is as follows:
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> query longitude and latitude by 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 style = "width: 730px; margin: auto;">
URL to be queried: <input id = "text _" type = "text" value = "Xuzhou gupeng square" style = "margin-right: 100px;"/>
Query result (longitude and latitude): <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 (); // enables scroll wheel zoom in and out, which is disabled by default.
Map. enableContinuousZoom (); // enable map inertial drag and drop. disabled by default.
Map. addControl (new BMap. NavigationControl (); // Add the default zoom/PAN Control
Map. addControl (new BMap. OverviewMapControl (); // Add the default thumbnail map control
Map. addControl (new BMap. OverviewMapControl ({isOpen: true, anchor: BMAP_ANCHOR_BOTTOM_RIGHT}); // in the lower right corner, open
Var localSearch = new BMap. LocalSearch (map );
LocalSearch. enableAutoViewport (); // you can adjust the form size automatically.
Function searchByStationName (){
Map. clearOverlays (); // clear the original Annotation
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 a dimension, which is the longitude and latitude of the area to be queried.
Map. addOverlay (marker );
Var content = document. getElementById ("text _"). value + "<br/> longitude:" + poi. point. lng + "<br/> latitude:" + 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); // specifies the beating animation.
});
LocalSearch. search (keyword );
}
</Script>
</Html>

The above is all the content of the code. You can use it directly in the project. You don't have to say thank you to me. Please call me Lei Feng ~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.