Getting started with Google Maps JavaScript |
Written by Ian Elliot | ||||||
Tuesday, 04 February 2014 | ||||||
Page 2 of 2
Objects, methods and propertiesThe map object has a range of methods and properties that you can use to work with it and there are a range of auxiliary objects which are designed to make it all easier. For example, the map's SetCenter method will move the map location to the specified latitude and longitude. In turn the latitude and longitude are specified as a suitably initialised LatLng object which we have already used in the first map. The easiest way to make sure you understand this is via a simple example: If you add a button to the web page:
and the following click event handler:
then clicking the button re-centres the map on 55N, 1.5W.. You can adjust all of the other viewing parameters using similar methods and objects – just check the online documentation. For example there is a setOptions and a setZoom method and so on. To try this out change the button's click event handler to read:
As well as get methods there are also get methods that can be used to retrieve current settings. Pushpins (Markers)Moving on from simply panning, zooming and generally controlling the map view it is also possible to add pushpin markers. This is just a matter of creating a Marker object and customising it using its options object. For example, to place a pushpin at the location used in the previous example:
The options object has a number of possible fields but you have to specify position. The map field specifies the map object that the Marker is added to.
If you want to you can do this as a two step procedure. First create the basic marker and then customise it:
This approach allows you to keep a reference to a marker and hence change them after they have been created and add them to the map when you are ready. On the subject of changing Markers - it is worth noting that Markers are fixed by default which means that a user cannot modify your placement by dragging. To make a Marker draggable use:
There are lots of other properties you can set and there are corresponding get methods. You can use a custom graphic for the push pin simply by setting the setIcon method to a URL that designates the file to be loaded. For example:
MarkerImages are more versatile than simple Markers and you can use them to create markers that are have custom hotspots for clicking and location. What Else?You can also draw on the map using Polylines and Polygons and you can create shapes which are photos and full 3D models. You can also use the AddShape method to add an array of objects and the DeleteShape method removes shapes. Shapes can also be organised into layers. Working with the map control is just a matter of figuring out which methods and properties affect the feature you are trying to control. You can do most of the things that a Google Maps user can, including planning a route and finding locations. My advice is to read through the methods and properties of the Map object, which is the central object in the system, and investigate any of the methods and properties that interest you. Often this will involve finding out how some auxiliary object works that is used as a parameter in the method. In most cases extending Google Maps is a matter of adding shapes, objects and other graphics at various locations. Sometimes you might want to add a more advanced facility and in this case it helps to copy the same approach used by Google i.e. create JavaScript objects which manipulate the DOM. You can use this method to control even the parts of the DOM that the map object uses and so change the look of things like markers in code or in CSS. There are other mapping services that you can use in the same general way as the Map control. You can use the
The all work in more or less the same way with the tiny added complication of usually returning data as a JSON object - but this is very easy to work with. More Informationhttps://developers.google.com/maps/ Related ArticlesGetting started with Google Earth An Interactive Google Earth KML Editor Getting started wth Bing Maps (Virtual Earth) Where are you from? IP Geolocation
To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, Facebook, Google+ or Linkedin, or sign up for our weekly newsletter.
Comments
or email your comment to: comments@i-programmer.info
<ASIN:1846288266> <ASIN:1590597079> <ASIN:0321525590> <ASIN:1934356069> |
||||||
Last Updated ( Friday, 20 March 2015 ) |