Baidu Map JS Summary

Source: Internet
Author: User

1, obtain the JavaScript API service method, first request the key (AK), can be successfully loaded into the Apijs file.

Usage such as the following:

<script type= "Text/javascript" src= " http://api.map.baidu.com/api?v=2.0&ak= your key "></script >

If you need to restrict the area, then you need to introduce the following JS

<!--area Limit JS---
<script type= "Text/javascript" src="http://api.map.baidu.com/library/AreaRestriction/1.2/src/ Arearestriction_min.js "></script>

2, set style, flush full screen, pop-up form style

Body, HTML, #l-map{width:100%;height:100%;overflow:hidden;margin:0;}

3, call Baidu Map

var map = new Bmap.map ("L-map"); Create a Map instance

var point = new Bmap.point (111.818239, 41.386087); Create point coordinates

Map.centerandzoom (point, 5); initialize map, set center point coordinates and map level

Map.enablescrollwheelzoom ();

Map.addcontrol (New Bmap.navigationcontrol ()); join? Default Zoom translation control

4. Join? Zoom the translation control

Map.addcontrol (New Bmap.navigationcontrol ()); join? Default Zoom translation control

Map.addcontrol (New Bmap.navigationcontrol ({anchor:bmap_anchor_top_right, type:bmap_navigation_control_small}  )); Upper-right corner, including pan and zoom button only

Map.addcontrol (New Bmap.navigationcontrol ({anchor:bmap_anchor_bottom_left, type:bmap_navigation_control_pan}  )); lower-left corner, including only the panning button

Map.addcontrol (New Bmap.navigationcontrol ({anchor:bmap_anchor_bottom_right, Type:bmap_navigation_control_  ZOOM})); Lower-right corner, including zoom button only

Map.enablescrollwheelzoom (); Enable wheel Zoom out, disabled by default

Map.enablecontinuouszoom (); Enable map inertia drag, disabled by default

5, regional restrictions must be introduced to the above area limit JS

The example of a regional limitation is to show only the Beijing

var B = new Bmap.bounds (new Bmap.point (116.027143, 39.772348),

New Bmap.point (116.832025, 40.126349));

try {

BMapLib.AreaRestriction.setBounds (map, b);

} catch (e) {

Alert (e);

}

6. Add overlay, Timer call overlay

// join? cover

function Getboundary () {

var bdary = new Bmap.boundary ();

bdary.get (" Inner Mongolia ", function (RS) {// get Administrative Region

var count = rs.boundaries.length;// How many points in an administrative region

for (var i = 0; i < count; i++) {

var ply = new Bmap.polygon (Rs.boundaries[i], {strokeweight:2, Strokecolor: "#0000ff", FillColor: ""}); Create polygon Overlay fillcolor font display

Map.addoverlay (ply); Add cover

Map.setviewport (Ply.getpath ()); Adjust your horizons

}

});

}

// timer call to join? cover

SetTimeout (function () {

// Clear Map Overlay

Map.clearoverlays ();

Map.centerandzoom (111.818239, 41.386087, 6); Set map coordinates, level

// join? Administrative coverage

Getboundary ();

}, 1000);

7. Add to Label

Point latitude and longitude, txtinfo hint information, type of picture

function Addmarker (point, Txtinfo, type) {

var Infowindow = "";

var marker = new Bmap.marker (point, {Icon:mapicon (type)});

Marker.addeventlistener ("click", Function () {

Infowindow = new Bmap.infowindow (txtinfo);

This.openinfowindow (Infowindow);

});

Map.addoverlay (marker); Join? Callout

marker.setanimation (bmap_animation_bounce);//Animation effect

}

// Create ICON

function Mapicon (type) {

var mappng;

Switch (parseint (type)) {

Case 1:

Mappng = "${pagecontext.request.contextpath}/images/triangle.png";

Break

Case 2:

Mappng = "${pagecontext.request.contextpath}/images/ban.png";

Break

        }

var Mapicon = new Bmap.icon (Mappng,

New Bmap.size (24, 24), {

Offset:new bmap.size (0,-5),

Imageoffset:new bmap.size (0, 0)

});

return Mapicon;

}

8, remove the label

Map.removeoverlay (marker); Remove a callout point

function Removemarker () {
var mkrs = Map.getoverlays (); Get annotations on the map, starting with 0 loops
for (var i=0; i < mkrs.length;i++) {
Object Polygon is an administrative area stroke overlay, not removed
if (mkrs[i]! = "[Object Polygon]") {
Map.removeoverlay (Mkrs[i]); Remove a callout point
}
}
}

9. Add text
function Addtxt (point,txtinfo) {
var opts = {
Position:point,//Specify the geographic location where the text callout is located
Offset:new Bmap.size (-20, 5)//Set text offset

}
var label=new bmap.label (txtinfo,opts);
Label.setstyle ({
Color: "#000000",
FontSize: "12px",
Height: "20px",
Lineheight: "20px",
fontFamily: "Microsoft Ya Black",
border: "#000000"
});
Map.addoverlay (label);
}

10, enter the address to obtain latitude and longitude

public static final String key_1 = "Your KEY (AK)";

/**
* Returns the latitude and longitude coordinates of the input address
* Key LNG (longitude), lat (latitude)
*/
public static map<string,string> Getgeocoderlatitude (String address) {
BufferedReader in = null;
try {
Convert address to Utf-8 16 binary
Address = Urlencoder.encode (address, "UTF-8");
Suppose there is an agent, to set the agent, no agent can stare
System.setproperty ("Http.proxyhost", "192.168.1.188");
System.setproperty ("Http.proxyport", "3128");
URL tirc = new URL ("http://api.map.baidu.com/geocoder?address= "+ address +" &output=json&key= "+ key_1);

in = new BufferedReader (New InputStreamReader (Tirc.openstream (), "UTF-8"));
String Res;
StringBuilder sb = new StringBuilder ("");
while (res = In.readline ())!=null) {
Sb.append (Res.trim ());
}
String str = sb.tostring ();
Map<string,string> map = null;
if (Stringutils.isnotempty (str)) {
int lngstart = Str.indexof ("lng\": ");
int lngend = Str.indexof (", \" lat ");
int latend = Str.indexof ("},\" precise ");
if (Lngstart > 0 && lngend > 0 && latend > 0) {
String LNG = str.substring (lngstart+5, lngend);
String lat = str.substring (lngend+7, latend);
Map = new hashmap<string,string> ();
Map.put ("LNG", LNG);
Map.put ("lat", lat);
return map;
}
}
}catch (Exception e) {
E.printstacktrace ();
}finally{
try {
In.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
return null;
}
public static void Main (String args[]) {
map<string, string> json = mapaction.getgeocoderlatitude ("Beijing");
System.out.println ("LNG:" +json.get ("LNG"));
System.out.println ("Lat:" +json.get ("lat"));
}

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.