Google Maps API entry: Google Maps common events and Application Ideas 2

Source: Internet
Author: User
Tags polyline
From: http://hi.baidu.com/xfm_zhr/blog/item/7bb88c19139de94242a9ad2b.html

Gmarker click, double-click, and drag effect

Note: The created gmarker cannot be dragged by default. You must activate this function in gmarkoptions when creating gmarker. Set draggable of gmarkeroptions to true, generally, the bouncy and bouncegravity attributes are set at the same time to change the drag-and-drop effect.

Double-click events. By default, double-clicking is performed by setting the double-clicking position to the map center. You can use gmap2.doubleclickzoomenabled () or other methods to block default operations.

ExampleCode:

Gmarker. Prototype. setindex = function (INDEX)

{

This. Index = index;

}

VaR markers = [];

//Generated by copying

VaR myicon = new gicon (g_default_icon, "http://www.google.com/mapfiles/marker.png ");

//Create a landmark

Function createmarker (point)

{

VaR marker = new gmarker (point, {icon: myicon, draggable: True, bouncy: true });

VaR Index = markers. length;

//Double-click a landmark to delete it.

Gevent. addlistener (Marker, 'dblclick', function ()

{

Deletemarker (Marker );

});

// DragstartEvent, new icon

Gevent. addlistener (Marker, 'dragstart', function ()

{

Marker. setimage ("http://www.google.com/mapfiles/dd-start.png ");

});

// DragendEvent, switch back to the original icon

Gevent. addlistener (Marker, 'dragend', function ()

{

Marker. setimage ("http://www.google.com/mapfiles/marker.png ");

});

//Click Show to form an arrangement

Gevent. addlistener (Marker, 'click', function ()

{

Marker. openinfowindowhtml ("What are you doing? ");

});

 

// DragEvent

//NOTE: If polyline is added, you need to re-draw the route when dragging the GEO chart.

Gevent. addlistener (Marker, 'drag', function ()

{

Redrawpolyline ();

});

Marker. setindex (INDEX );

Markers [Index] = marker;

Map. addoverlay (Marker );

//Each time a new landmark is created, it is re-painted.

Redrawpolyline ();

}

//Double-click a landmark to delete it.

Function deletemarker (Marker)

{

//Are you sure you want to delete it?

If (! Confirm ("Are you sure you want to delete this landmark? ") Return;

VaR Index = marker. index;

//Remove this layer

Map. removeoverlay (Marker );

//Delete the specified gmarker from the array and update the index.

For (VAR I = index; I <markers. Length-1; I ++)

{

Markers [I + 1]. setindex (I );

Markers [I] = markers [I + 1];

}

Markers. Length = markers. Length-1;

}

Function load ()

{

If (gbrowseriscompatible ())

{

Map = new gmap2 (document. getelementbyid ("map "));

Map. addcontrol (New gsmallmapcontrol ());

Map. addcontrol (New gmaptypecontrol ());

VaR centerpoint = new glatlng (39.92, 116.46 );

Map. setcenter (centerpoint, 4 );

Gevent. addlistener (MAP, 'click', function (overlay, point)

{

If (point) createmarker (point );

});

}

}

//Add route

Function addpolyline ()

{

//Create

VaR points = [];

For (VAR I = 0; I <markers. length; I ++)

{

Points [I] = markers [I]. getpoint ();

}

Polyline = new gpolyline (

Points, // glatlng ()Array

"# Ff0000 ",//Line color

10 ,//Line width

0.5 //Transparency

);

Map. addoverlay (polyline );

}

//Delete A route

Function removepolyline ()

{

Polyline. Remove ();

Polyline = NULL;

}

//Re-draw the route

Function redrawpolyline ()

{

If (polyline)

{

Removepolyline ();

}

Addpolyline ();

}

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.