Skip to main content

Posts

Showing posts with the label google maps api v3 example

Simple Directions

    After taking an unintended long break after the 100 th post, I am back with more examples and several more GIS news. Today, we will take a look at a simple example showcasing the use of the Google’s Directions Services. Using the Google’s Direction service, we can calculate the distance between two points, show the path between these two points, calculate the average time taken to traverse this distance and we can also enforce several constraints on this path.     The Google Directions API is a service that calculates the directions between locations using an HTTP request. You may pass either an address (string) or a latitude/longitude coordinate as the origin and destination. If you pass an address as a string, the Directions service geocodes the string and converts it to a latitude-longitude coordinate to calculate directions. The origin and destination are two mandatory parameters for a directions request, whereas several other o...

Spherical Cap

    In some of the earlier posts, I have shown some examples related to creating Polygons and Polylines on the Google Map using the Google Maps API v3. In addition to this generic polygon class, the Javascript Google Maps API also includes a specific class for Circle to simplify its construction. In today’s example, I am going to show you how to generate a circle or a spherical cap on the map.     A Circle is similar to a Polygon in that you can define custom colors, weights, and opacities for the edge of the circle (the "stroke") and custom colors and opacities for the area within the enclosed region (the "fill"). Colors should be indicated in hexadecimal numeric HTML style.     Unlike a Polygon , you do not define paths for a Circle ; instead, a circle has two additional properties which define its shape:  Center: Specifies the center of the circle using google.maps.LatLng  Radius: Specifies the radius...

Pan to states using geocoding...

    In continuation with yesterday’s code which showcased the Geocoding with region biasing , today we will see a little complex but interesting geocoding example. Today we will pan to a state using the geocoding capability! Sounds interesting, right! So, let’s get on to the code straight away!     Now even if the length of the code is big, but the logic is not rocket science! It’s quite easy and pretty simple. There is a drop down list box which lists all the names of the states and union territories in India. These state names will act as addresses or the inputs to the geocoder. When you select a certain state name in the drop down list, that state name will be passed as a parameter to the geocoding function, which after geocoding the address will pan and zoom on to the state that is selected!     The output of the above code will be as seen in the results section above. If you have any queries or doubts regarding the above...

Geocoding - Region biasing

    Today we will have a look at a simple geocoding example wherein we will see the effects of region biasing on geocoding. First things first. What is region biasing in geocoding and why is it required; would be an obvious looking question here. The answer to this is pretty simple and straight. Region biasing means returning a geocoded address so that it falls in the region specified. Consider the example of the city named “Hyderabad” which is  present in India as well as Pakistan. Now, suppose that my target users are from India, then by using the region biasing property, the geocoder will always return the “Hyderabad” in India when a user enters a request for the same. I have taken the same example in the code, so that the region biasing geocoding concept would become much more clear.     The code goes here…     The code is pretty simple and does not need an explanation. The crux of the code lies in the following line of code. ...

Different map for a different browser!

            The title simply implies that detect the client's browser and then display the map accordingly! This is basically important when you are developing a map application for mobiles. Mobiles usually have a small screen and the maps that look brilliant on the desktops don't look the same on the mobile screens!             Well, in my example, I am considering only two of the most popular browsers in use - Mozilla and IE. The application detects whether the client's browser is one of the two and then renders the map accordingly! If you are viewing the example in Mozilla, then you will see a 600 X 800px map, whereas if you are viewing it in IE, you will see a smaller 500 X 500px map! Here is the code... <html> <title>Basic Google Map Example - Detect browser and render map accordingly </title> <head> <script type="text/javascript" src="htt...

Google Map Loading . . . . .

    The blank web page seems so boring and dull when the map is still loading. You will come across such a situation quite often where you will be having a slow internet connection! We cannot do anything about the slow internet connection, but we can surely avoid the dull blank web-page by using a simple trick. We can place a "loading" image at the center of the web page so that the page doesn't look blank! An image like this:     The following code will show you how this can be done and here it is!     As you all can see the code is very very simple and the output will look as seen in the result section above.     Do let me know what you think about the examples shared here! Do leave your comments here! Till the next example, happy mapping!

Google Map within an image

    Hope you'll enjoyed the last post describing how two maps can be placed side-by-side on the same webpage ! Most of you'll will enjoy today's code example too! All of you out there who work on Google Maps API, must have visited the API home page several times! You must have seen the map within the Google Nexus Phone frame there! It appears as though there is a map visible on the mobile screen! One of friend asked me the other day if I could implement that, and I did it and here is the code that I am sharing with you all!     I have used an I-Pad frame and am displaying a map centered at India within it! Here is the code!     The output of the above code is as seen in the result section above! The map that is appearing within the frame is not just an image but can be dragged like any other Google Map! Enjoy the code. It is very simple to understand!     In case you have any queries regarding the same, feel free to le...

Two maps on the same page - Side-by-side

    How good I am feeling to post a code example after such a long time! It's been all "news" over the past so many posts! Well now that I am finally doing a code example, I am posting a very highly requested code sample. Placing two Google Maps on the same page (Now that's simple you would say!), but side by side. Now this is the thing that most people struggle with. Well, implementing the second part is also very simple, as you will see in today's code.     Let's see the code. Here it is!     The output of the above code will be as seen in the result section above.     As most of you will realize, there are two maps, one centered at "Pune" and other at "Noida". Why I chose these two locations? Well, just like that!...The main issue of concern is how the maps appeared side-by-side and not one below the other as would be normal behavior of two "div" elements used in the same page. Now here is the trick! Check out the...

Reverse Geocoding

    The term geocoding generally refers to translating a human-readable address into a location on the map. The process of doing the converse, translating a location on the map into a human-readable address, is known as reverse geocoding. You can read more about geocoding here .     The Geocoder in Google Maps API v3, supports reverse geocoding directly. While geocoding, we supply a textual address and that gets mapped as a location on the map. However, in reverse geocoding, instead of supplying the textual address, we will supply a comma- separated latitude- longitude pair and get a textual address as the result. You can have a look at the geocoding example here , before proceeding to the reverse geocoding example.     The reverse geocoder often returns more than one result. Geocoding "addresses" are not just postal addresses, but any way to geographically name a location. For example, when geocoding a point in the city of Agra, India,...

Geocoding

    Geocoding is the process of converting addresses (like "Pune") into geographic coordinates (like latitude - 18.5193 and longitude - 73.8579), which you can use to place markers or position the map. The Google Maps API provides a geocoder class for geocoding addresses dynamically from user input. Accessing the Geocoding service is asynchronous, since the Google Maps API needs to make a call to an external server. For that reason, you need to pass a callback method to execute upon completion of the request. This callback method processes the result(s). To know more details about the geocoding requests, responses and results visit this link.     Understanding the geocoding requests, responses and results is very important for proper understanding and execution of any geocoding functionality in Google Maps API v3. Let us have a look at the following example...     The output of the above code will appear as seen in the results sectio...

Panoramia - Tag based search

    If you don't already know what Panoramia is, you can have a look at this link , to know more about Panoramia and a basic example. Today we will have a look at a more sophisticated example for Panoramia Layer in Google Maps API v3. In today's example we will search and display only those images on the map that have the tag that is being searched for.     The example is very simple and all you need to do is copy the following code in an html and open it in your browser. Search for whatever you want to and if there are images that have the same tag, then those images will appear on the map.     The output of the above code looks as it is seen in the result section above. Now you can enter your search parameter in the search box. I had entered "Fruits" and the output was as seen in the image below.     You can have your own search parameter. Enjoy the Panoramia ride!

Panoramio Layer...

    Let's start with the obvious question first...What is Panoramio? Panoramio is a geolocation-oriented photo sharing website. Panoramio website was officially launched on October 3, 2005 by Joaquín Cuenca Abela and Eduardo Manchón Aguilar, two Spanish entrepreneurs and was acquired by Google in July 2007.     Accepted photos uploaded to the site can be accessed as a layer in Google Maps, with new photos being added at the end of every month. The site's goal is to allow Google Maps and Google Earth users to learn more about a given area by viewing the photos that other users have taken at that place. The website is available in several languages and can be viewed here .     You may add photos from Panoramio as a layer to your maps using the PanoramioLayer object. The PanoramioLayer renders a layer of geotagged photo icons from Panoramio on the map as a series of large and small photo icons. Let us now have a look at the following code t...

The Bicycling Layer...

    Recreational cyclists and bike commuters alike can plot cycle-friendly routes, find trails, and avoid snarling traffic with Google Map's Bicycle layer. Map's bike-friendly, green-toned map layer is very eye-pleasing. The Google Maps API allows you to add bicycle information to your maps using the BicyclingLayer object.     The BicyclingLayer renders a layer of bike paths, suggested bike routes and other overlays specific to bicycling usage on top of the given map. Additionally, the layer alters the style of the base map itself to emphasize streets supporting bicycle routes and de-emphasize streets inappropriate for bicycles.     Let us have a look at the following example. The code has a map which is centered at Pune, India. There are very few cycle tracks in Pune and so you will see just a few dark green lines on the map. But if you would change the latitude-longitude values in the code and center the map at USA, then you will see a...

Google Map's Real-Time Traffic Layer...

    You can now check the traffic condition on the road before you leave for work, or a party or to your friend's place. Google's Traffic layer shows the traffic conditions in your area - LIVE!!! Live traffic data is available in major cities in the United States, France, Britain, Canada and Australia, with new cities and countries frequently added. The Google Maps API allows you to add real-time traffic information (where supported) to your maps using the TrafficLayer object. Traffic information is provided for the time at which the request is made. Consult this spreadsheet to determine traffic coverage support.     Let us now have a look at the Google Maps API - Trafiic Layer example. Copy the following code in a simple text file and save it as html. Click on this html file and it will open in your default browser. You will then see the traffic conditions in your area - LIVE...     The output of the above code looks as seen in the r...

Fusion Table Layer...

    Today we will have a look at the "Fusion Table" Layer example. But before we start with the actual code, we will have a look at what Fusion Table is. Google Fusion Table is a free service for sharing and visualizing data online. It allows you to share data, merge data from multiple tables into interesting derived tables, and see the most up-to-date data from all sources. There is a lot of documentation about Google Fusion Table available on the web. You can visit their home page or the Google Research Blog to gather more information about Fusion Tables.     Let us have a look at the following code. The code is very short and simple to understand!     The few things to look out for in the above code are: The manner in which mapTypeId has been specified! In other Google Maps API v3 examples, we have seen that we specify mapTypeID as ROADMAP, SATELLITE, etc. i.e., all the letters are in uppercase and not in quotes! However, in this...

KML Layer Example

         Until now, we have covered various examples involving markers, polylines, polygons, i.e. creating overlays. We have also seen other examples to remove overlays! We will now move on to some other concepts. Today we will have a look at what KML is and how to create a KML layer!          Let us first understand what KML is and why is it necessary. KML stands for Keyhole Markup Language which is an XML schema for expressing geographic annotation and visualisation within 2D maps and 3D Earth browsers. The KML file specifies a set of features (place marks, images, polygons, 3D models, textual descriptions, etc.) for display in Google Earth, Maps and Mobile, or any other 3D Earth browser (geobrowser) implementing the KML encoding. Each place always has a latitude and a longitude. Other data can make the view more specific, such as tilt, heading, altitude, which together define a "camera view". ...

Overlays - A complex example

    In the last post we have seen what an overlay is and we have also seen a small simple overlay example as well. Today's example is a bit complex as compared to the last one, but is not at all difficult. We will directly jump to the code instead of having any more descriptions and stuff. So, here's the code.     The output of the above code looks as seen in the result section above. You need to click on the map to place a marker on the map. You can place multiple markers on the map by clicking on the map multiple times. By clicking on the "Clear overlays" button all the markers on the map will disappear. By clicking on the "Show all overlays" button, all the cleared, but not deleted markers will reappear. Clicking on the "Delete overlays" buttons will remove all the markers permanently.     If you have any doubts or queries regarding the code please leave a comment or drop me a mail .

Removing overlays

    "Overlays"....!!! What is this now! Never heard or used it before? You might immediately say "NO"...But, actually all of you have already seen what an overlay is and you have also used it! Now you would say "When!?!?" Well, all of the markers, polylines, polygons that you have created are overlays. Overlays are objects on the map that are tied to latitude/ longitude coordinates, so they move when you drag or zoom the map. Overlays reflect objects that you add to the map to designate points, lines, areas or collection of objects.     Till now we have seen how to create an overlay. In today's example we will see how to remove an overlay. In this example we will create markers on click. But the difference will be that when you create a new marker, the earlier one will disappear from the map. So at any given time there would only be one single marker on the map! Note that removing the marker does not delete the overlay; it simply removes the overlay...

Geodesic, Draggable Onclick Polygon

    After creating a simple polygon and then creating an on-click polygon, we will today see how to create a polygon which can be changed, altered, dragged; basically edited on the fly. We will also cover the geodesic polygon in this example itself. If you want to read and understand more about what "geodesic" is, read this post.     Nothing exceptional about the code that I think needs explanation! You can create a geodesic polygon which can be edited on the fly. Click, hold and drag any marker, and the two polylines connected by this marker will change accordingly. By clicking on the marker, that marker would disappear, and the polygon will reform itself! Here is today's code.     You can copy the above code in a text file and save it with .html extension. Click on this file; it will open in your default browser and you will see an exciting map in action! The output of the above code appears as seen in the result section above! If yo...

Onclick polygon

    Yesterday we had a look at a simple polygon example. But that example was not exciting as it was all hard-coded with no user interaction and which means no fun! Today's example will deal with creating a polygon on the fly, i.e. an on-click polygon in action!     The code for creating a polygon is very much similar to creating a polyline! The only difference is that you need to replace "Polyline" by "Polygon" in such examples. Let's head on to our code.     The output of the above code can be viewed in the result section above. If you have any doubts or queries regarding the above code then please comment here or feel free to drop me a mail ! Till then, happy mapping!