Google Maps V3 rectangular electronic barrier implementation

Source: Internet
Author: User

Many map positioning and so on require the electronic barrier function, which is also required by a positioning project recently. Google hasn't found any usefulCodeI had to write one for myself.

 

It's very simple.

 

<! Doctype html >  
< Html >  
< Head >  
< Meta Name = "Viewport" Content = "Initial-scale = 1.0, user-scalable = No"   />  
< Meta HTTP-equiv = "Content-Type" Content = "Text/html; charsets = UTF-8" />  
< Title > Google Maps javascript API V3 example: marker simple </ Title >  
< Link Href = "Http://code.google.com/apis/maps/documentation/javascript/examples/default.css" REL = "Stylesheet" Type = "Text/CSS"   />  
< Script Type = "Text/JavaScript" SRC = "Http://maps.googleapis.com/maps/api/js? Sensor = false" > </ Script >  
< Script Type = "Text/JavaScript" >  
Function Initialize (){
VaR Mylatlng =   New Google. Maps. latlng ( 29.678815 , 121.426489 );
VaR Myoptions = {
Zoom: 12 ,
Center: mylatlng,
Maptypeid: Google. Maps. maptypeid. Roadmap
}
VaR Map =   New Google. Maps. Map (document. getelementbyid ( " Map_canvas " ), Myoptions );

VaR Marker =   New Google. Maps. Marker ({
Position: mylatlng,
Map: map,
Title: " Hello world! "
});

VaR Rectangle =   New Google. Maps. rectangle ();
VaR Rectoptions = {
Strokecolor: " # Ff0000 " ,
Strokeopacity: 0.8 ,
Strokeweight: 2 ,
Fillcolor: " # Ff0000 " ,
Fillopacity: 0.35 ,
Map: map,
Clickable: False ,
Bounds: map. getbounds ()
};

VaR Beginlatlng =   "" ; // Record start point coordinates
VaR Endlatlng =   "" ; // End Point Coordinate of record
VaR Rectbounds =   "" ;
VaR Sfclick =   "" ;
VaR Sfmove =   "" ;
VaR Clickcount =   0 ; // Clicks
Sfclick = Google. Maps. event. addlistener (map, ' Click ' , Function (E ){
VaR Begin = E. latlng;
Clickcount ++ ;
If (Clickcount =   1 ){
Sfmove = Google. Maps. event. addlistener (map, " Mousemove " , Function (E ){
Beginlatlng = Begin;
Endlatlng = E. latlng;
Rectoptions. Bounds =   New Google. Maps. latlngbounds (beginlatlng, endlatlng );
Rectoptions. Map = Map;
Rectangle. setoptions (rectoptions );
});
} Else {
Google. Maps. event. removelistener (sfmove );
If (Window. Confirm ( " Are you sure you want to set the electronic barrier range? " )){
Clickcount =   0 ;
Alert ( ' Starting coordinate point '   + Beginlatlng +   ' \ N ending coordinate point '   + Endlatlng)
} Else {
Clickcount =   0 ;
Rectoptions. Map =   Null ;
Rectangle. setoptions (rectoptions );
}
}
});
}
</ Script >  
</ Head >  
< Body Onload = "Initialize ()" >  
< Div ID = "Map_canvas" > </ Div >  
</ Body >

</Html>  

 

Http://www.cnblogs.com/relax/archive/2011/08/24/2151838.html

Note that the clickable of rectangle must be set to falsh, otherwise the rectangle will not end. It is best to include the source when you reprint it.

 

 

 

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.