One of today&rsquo,s most used APIs is the Geolocation API. Geolocation permits an application to determine your physical coordinates, and tailor the application to you. This has the potential to dramatically enhance your user practice. This article will display you how to use the Geolocation API together with the Google Maps API.
The Google Maps API lets you integrate the Google Maps service with your webstek. The examples te this article use version Three.Ten of the API. The service is fully free, but you should subscribe to the APIs Console webstek to obtain your API key, and use it when you send a request to the service. If you need information about how to obtain the key, you can read the getting began guide. The API key permits you to pedagogo your application&rsquo,s usage, and gives Google the chance to get te touch with you if necessary. Keep te mind that the Google Maps API has usage boundaries that you can increase for a toverfee. This article also assumes a basic understanding of Geolocation, which you can get by reading Using the HTML5 Geolocation API.
An Overview of the Google Maps API
This section covers the main classes and methods of the Google Maps API. There are many other objects and methods available, but discussing every opzicht of the API is beyond the scope of this article. The most significant and used class of the library is google.maps.Opbergmap . The Opbergmap class accepts two parameters that are an HTML factor, that will contain the ordner, and a google.maps.MapOptions object. The MapOptions objects have many properties, but only the following three are required.
- center : a google.maps.LatLng object (more on this zometeen) used to set the initial opbergmap center.
- mapTypeId : a google.maps.MapTypeId object, which is used to set the type of the ordner. For example, you can see the schrijfmap spil a road opbergmap or a satellite opbergmap.
- teleobjetivo : a positive number that sets the initial teleobjetivo of the schrijfmap.
Bijzonder from the several setters and getters for the class attributes, there are a duo of methods worth mentioning. The very first is fitBounds() , which takes a LatLngBounds object spil its only parameter. The LatLngBounds represents a rectangle built by specifying the south-west and north-east points. This sets the view of the ordner to contain the given bounds. The 2nd method is panTo() , which accepts a LatLng object and switches the center of the ordner.
Another significant class is google.maps.LatLng , which represents geographical coordinates te latitude and longitude. This class is used ter almost every method of the Maps API. For example, wij&rsquo,ll use it to center the schrijfmap on the user&rsquo,s position. However, they&rsquo,re also used to trace a route or to draw a polyline on the schrijfmap.
Many maps contain markers which designate points of rente, such spil a destination. To place markers on a ordner, you have to use the google.maps.Marker class. It creates a marker with the options (a google.maps.MarkerOptions object) passed to the constructor, and applies it to the specified opbergmap. The only required option is position , which sets the position of the marker.
The last class I&rsquo,ll describe before displaying you the example is the Geocoder . It lets you convert from an address to a LatLng object, and vice versa. It has an empty constructor and just one method, geocode() , that takes a GeocoderRequest object spil a parameter, and a callback function te which you&rsquo,ll read the results.
A Basic Example
Using the few classes described above, you&rsquo,re now able to write a elementary, yet nice, application. The example isn&rsquo,t very hard to understand, but still uses all of the classes and methods discussed. The example retrieves the user&rsquo,s position, and shows it using a marker on the schrijfmap. The Geocoder service is used to retrieve the user&rsquo,s accomplish address, and display it on the pagina. Wij&rsquo,ll also use the accuracy parameter of the retrieved position to draw a blue circle around the location.
The very first step to using the Maps API is to actually include it. This is very elementary and consists of just adding the following line to your web pagina.
Since Google Maps will still work without the key, and to avoid the long line ter the example, simply write the following instead.
Notice that both lines contain a sensor parameter. It vereiste be included te the URL and set to true if you&rsquo,re using a sensor (like a GPS), or false otherwise. You can read more on this at Loading the Maps API.
To build the example wij&rsquo,ll use two functions. The very first one uses the user&rsquo,s position to create a ordner and draw a circle around the user. The 2nd function queries Google to retrieve the address of the user based on its location. Let&rsquo,s see the key points of both.
This elementary line uses the latitude and longitude of a Position object to create the LatLng object. Wij&rsquo,ll use this object te the surplus of the function. Ter fact, it&rsquo,ll be used ter the MapOptions to set the center of the Schrijfmap wij&rsquo,re building. Spil a container for the opbergmap, I&rsquo,ll use a <,div>, factor, with an id of schrijfmap .
Now it&rsquo,s time to add the marker to the ordner to signal the user&rsquo,s position. Wij&rsquo,ll use the Google Maps default icon, but you can specify an photo you like using the icon property or the setIcon() method.
At this point you could close the function and have a schrijfmap displaying the user position with a plain marker. However, since te a desktop environment the accuracy could be very low, it would be nice to visualize it. Wij&rsquo,ll accomplish this using the Circle class that, spil you might guess, draws a circle on the schrijfmap. It has a loterijlot of properties to set the color, the opacity, the radius, and so on, but the most significant property is the ordner object where it applies.
Please note that the last statement calls the fitBounds() method with the bounds taken from the circle. This is done to ensure that if the circle goes outside of the zoomed view, the ordner will be zoomed out accordingly to display the entire circle.
The 2nd function, named writeAddressName() , takes a LatLng object and sets the location property to query the Geocoder service. Ter the callback function wij&rsquo,ll test if an error occurs. If an error does occur, it will be displayed. Otherwise, the formatted address is displayed.
Putting it all Together
Given the code shown te the previous section, the final and fully working code is shown below.
This article has explained several methods and properties of the most significant classes ter the Google Maps API. Moreover, wij&rsquo,ve shown how you can use them together with the Geolocation API to built a accomplish and functional service to track your user&rsquo,s position and locate them on a ordner. Of course, you can do more a lotsbestemming more than that spil you&rsquo,ll see ter the next article on this amazing API.