Jquery and google map APIs use controls and listeners in combination

Source: Internet
Author: User

Google Maps JavaScript. API allows you to use Google Maps on your webpage. before using the API, you must apply
API keys. To apply for an API key, go to http://code.google.com/apis/maps/signup.html. Assume that the obtained key is abqiga.
Jquery acquisition is no longer cumbersome here. There are many introductions on the Internet about jquery.
Then we will use JQuery and Google Maps JavaScript. API to demonstrate the interesting map effects of google map, so as to become familiar with Google Maps JavaScript. API.
HelloChina:
(1) Compile html code in an html file:
Map.html
Copy codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Strict // EN"
Http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta. http-equiv = "content-type" content = "text/html; charset = UTF-8"/>
<Title> use Google Maps with JQuery </title>
<Script. src = "http://maps.google.com/maps? File = api & v = 2 & key = abqiga "type =" text/javascript "> </script>
<Script. type = "text/javascript" src = "jquery. js"> </script>
<Script. type = "text/javascript" src = "map. js"> </script>
</Head>
<Body>
<Div id = "map" style = "top: 100px; left: 300px; width: 600px; height: 400px"> </div>
<Div id = "message"> </div>
</Body>
</Html>

(2) Compile js code in the js File
Map. js
Copy codeThe Code is as follows:
$ (Document). ready (function ()
{
// Check the browser compatibility
If (GBrowserIsCompatible ()){
Var map = new GMap2 (document. getElementById ("map "));
Map. setCenter (new GLatLng (36.94, 106.08), 4); // China's latitude and longitude and local magnification
Map. setMapType (G_SATELLITE_MAP );
// Triggered when document is uninstalled
$ (Window). unload (function (){
$ ('.'). Unbind ();
GUnload ();
});
} Else
{
Alert ('The browser you use does not support Google Map! ');
}
});

(3) enter the address corresponding to the page in the address bar (make sure that the key matches your entered address or domain name) and check that China is located in the center of the map.
Of HolloChina
Map movement and Transformation
(1) modify the javascript Code as follows:
Map. js
Copy codeThe Code is as follows:
$ (Document). ready (function ()
{
If (GBrowserIsCompatible ()){
Var map = new GMap2 (document. getElementById ("map "));
Map. setCenter (new GLatLng (36.94, 106.08), 4 );
// Move in 4 seconds
Window. setTimeout (function (){
Map. panTo (new GLatLng (35.746512259918504, 78.90625 ));
},4000 );
$ (Window). unload (function (){
$ ('.'). Unbind ();
GUnload ();
});
} Else
{
Alert ('The browser you use does not support Google Map! ');
}
});

(2) enter the corresponding address to view the map. After four seconds, you can see that the center of the map is moved to the western part of China (approximate location ):
Move the map center to the West of China
Add controls and modify map types
Modify the javascript Code as follows:
Map. js
Copy codeThe Code is as follows:
$ (Document). ready (function ()
{
If (GBrowserIsCompatible ()){
Var map = new GMap2 (document. getElementById ("map "));
// Small scaling Controller
Map. addControl (new GSmallMapControl ());
// Map Type Controller
Map. addControl (new GMapTypeControl ());
Map. setCenter (new GLatLng (36.94, 106.08), 4 );
// Set the map as a satellite map
Map. setMapType (G_SATELLITE_MAP); // modify the map type
$ (Window). unload (function (){
$ ('.'). Unbind ();
GUnload ();
});
} Else
{
Alert ('The browser you use does not support Google Map! ');
}
});

Refresh the page and you will see a small scaling control in the upper-left corner of the satellite map.
After the control is added
Add Event Listeners and enable scroll wheel Scaling
Modify javascript code:
Map. js
Copy codeThe Code is as follows:
$ (Document). ready (function ()
{
If (GBrowserIsCompatible ()){
Var map = new GMap2 (document. getElementById ("map "));
Map. addControl (new GSmallMapControl ());
Map. addControl (new GMapTypeControl ());
// Enable the scroll wheel scaling effect-scroll the mouse wheel forward to zoom in the map, and vice versa
Map. enableScrollWheelZoom ();
// Add a moveend event listener
GEvent. addListener (map, "moveend", function (){
Var center = map. getCenter ();
// Display the longitude and latitude of the map center in this DIV
$ ('# Message'). text (center. toString ());
});
Map. setCenter (new GLatLng (36.94, 106.08), 4 );
$ (Window). unload (function (){
$ ('.'). Unbind ();
GUnload ();
});
} Else
{
Alert ('The browser you use does not support Google Map! ');
}
});

At this time, the map will be scaled when the scroll wheel is rolled, and after the map is dragged, the coordinate information on the left side of the map will change.

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.