Google Maps
Add a marker to Google Maps
Google Maps - Overlays
An overlay is an object bound to longitude/latitude coordinates on the map, and moves as you drag or zoom the map.
Google Maps API has the following overlays:
- Points on the map are displayed using markers, usually displaying custom icons. A marker is an object of type GMarker and can be customized using an object of type GIcon.
- Lines on the map are displayed using polylines (representing a collection of points). A line is an object of type GPolyline.
- Areas on the map are displayed as polygons (if an arbitrary-shaped area) or ground overlays (if a rectangular area). Polygons are similar to closed polylines, so they can be any shape. Ground overlays are usually used for areas on the map that are directly or indirectly associated with tiles.
- The map itself is displayed using tile overlays. If you have your own series of tiles, you can use the GTileLayerOverlay class to change the existing tiles on the map, or even use GMapType to create your own map type.
- An info window is also a special overlay. However, please note that the info window is automatically added to the map, and the map can only add one object of type GInfoWindow.
Google Maps - Add Marker
Marks identify points on the map. By default, they use G_DEFAULT_ICON (you can also specify a custom icon). The GMarker constructor takes GLatLng and GMarkerOptions (optional) objects as parameters.
Markers are designed to be interactive. For example, by default they receive the "click" event, often used to open an info window in an event listener.
Add a marker to the map via the setMap() method:
Example
var
marker
=
new
.
maps
.
Marker
(
{
position
:
myCenter
,
}
)
;
marker
.
setMap
(
map
)
;
Try it »
Google Maps - Draggable Marker
The following example introduces how to use the animation property to drag a marker:
Example
marker
=
new
.
maps
.
Marker
(
{
position
:
myCenter
,
animation
:
.
maps
.
Animation
.
BOUNCE
}
)
;
marker
.
setMap
(
map
)
;
Try it »
Google Maps - Icon
Markers can be displayed with a custom new icon instead of the default icon:
Example
var
marker
=
new
.
maps
.
Marker
(
{
position
:
myCenter
,
icon
:
'
pinkball.png
'
}
)
;
marker
.
setMap
(
map
)
;
Try it »
Google Maps - Polyline
The GPolyline object can create linear overlays on the map. GPolyline includes a series of points and creates a series of line segments that connect these points in order.
Polyline supports the following properties:
- path - specifies the latitude/longitude coordinates of multiple straight lines
- strokeColor - specifies the hexadecimal color value of the straight line (format: "#FFFFFF")
- strokeOpacity - specifies the transparency of the straight line (the value is 0.0 to 1.0)
- strokeWeight - defines the width of the line, in pixels.
- editable - defines whether the user can edit the straight line (true/false)
Example
var
myTrip
=
[
stavanger
,
amsterdam
,
london
]
;
var
flightPath
=
new
.
maps
.
Polyline
(
{
path
:
myTrip
,
strokeColor
:
"
#0000FF
"
,
strokeOpacity
:
0.8
,
strokeWeight
:
2
}
)
;
Try it »
Google Maps - Polygon
The GPolygon object is similar to the GPolyline object because they both include a series of ordered points. However, unlike a polyline which has two endpoints, a polygon is designed to define a closed region.
Like polylines, you can customize the color, thickness, and transparency of the polygon edges (lines), as well as the color and transparency of the enclosed fill area. Colors should be hexadecimal numbers in HTML style.
Polygon supports the following properties:
- path - specifies the coordinates of multiple straight line latitudes (the first and last coordinates are equal)
- strokeColor - specifies the hexadecimal color value of the straight line (format: "#FFFFFF")
- strokeOpacity - specifies the transparency of the straight line (the value is 0.0 to 1.0)
- strokeWeight - defines the width of the line, in pixels.
- fillColor - specifies the hexadecimal color value of the closed area (format: "#FFFFFF")
- fillOpacity - specifies the transparency of the fill color (the value is 0.0 to 1.0)
- editable - defines whether the user can edit the straight line (true/false)
Example
var
myTrip
=
[
stavanger
,
amsterdam
,
london
,
stavanger
]
;
var
flightPath
=
new
.
maps
.
Polygon
(
{
path
:
myTrip
,
strokeColor
:
"
#0000FF
"
,
strokeOpacity
:
0.8
,
strokeWeight
:
2
,
fillColor
:
"
#0000FF
"
,
fillOpacity
:
0.4
}
)
;
Try it »
Google Maps - Circle
Circle supports the following properties:
- center - specifies the center point parameter of the circle google.maps.LatLng
- radius - specifies the radius of the circle, in meters
- strokeColor - specifies the hexadecimal color value of the arc (format: "#FFFFFF")
- strokeOpacity - specifies the transparency of the arc (the value is 0.0 to 1.0)
- strokeWeight - defines the width of the line, in pixels.
- fillColor - specifies the hexadecimal color value of the circle fill value (format: "#FFFFFF")
- fillOpacity - specifies the transparency of the fill color (the value is 0.0 to 1.0)
- defines whether the user can edit the straight line (true/false)
Example
var
myCity
=
new
.
maps
.
Circle
(
{
center
:
amsterdam
,
radius
:
20000
,
strokeColor
:
"
#0000FF
"
,
strokeOpacity
:
0.8
,
strokeWeight
:
2
,
fillColor
:
"
#0000FF
"
,
fillOpacity
:
0.4
}
)
;
Try it »
Google Maps - Info Window
Display a text info window on a marker:
Example
var
infowindow
=
new
.
maps
.
InfoWindow
(
{
content
:
"
Hello World!
"
}
)
;
infowindow
.
open
(
map
,
marker
)
;
Try it »