kristian

Minimal How to Use Google Maps API With PHP

Google MapsThe goal of this how to is to easily add multiple markes into the map on your own site using Google Maps API and PHP.

Google Maps API key

  1. If you don’t already have, sign up for the Google Account.
  2. Sign up for the Google Maps API key. Read at least the higlights from the Google Maps API terms. Sign up for your site, forexample http://www.mysite.com/

After sign up you’ll get your key and an example html code. If you lost your key anytime, you can sign up again for the same site and use a new key. You can also sign up for multiple sites keys if needed. You’ll get different key for each site.

JavaScript

To add multiple markers on your map we’ll use some basic PHP code. Google’s example code is a good starting point for this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
  Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html;
    charset=utf-8"/>
    <title>Google Maps JavaScript API Example</title>
    <script src="http://maps.google.com/maps?
      file=api&amp;v=2&amp;key=ABCDEFGH..."
      type="text/javascript"></script>
    <script type="text/javascript">
    //<![CDATA[
    function load() {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(
        document.getElementById("map"));
        map.setCenter(
        new GLatLng(37.4419, -122.1419), 13);
      }
    }
    //]]>
    </script>
  </head>
  <body onload="load()" onunload="GUnload()">
    <div id="map"
    style="width: 500px; height: 300px"></div>
  </body>
</html>

You have your API key on the html head and JavaScript to load on page load and div element to show the map. With map.setCenter(new GLatLng(37.4419, -122.1419), 13) you define the map’s center point and the zoom level. Here is an example.

To have some controls in the map, add this line into JavaScript load funtion:

map.addControl(new GSmallMapControl());

Add this JavaScript function to make markers:

function createMarker(point, text, title) {
  var marker = new GMarker(point,{title:title});
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(text);
  });
  return marker;
}

Add marker with this code:

var marker = createMarker(
new GLatLng(37.4419, -122.1419),
'Marker text', 'Example Title text');
map.addOverlay(marker);

Note that you can add in the markers any html code like links and images for example. So, the code looks now like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
  Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html;
    charset=utf-8"/>
    <title>Google Maps JavaScript API Example</title>
    <script src="http://maps.google.com/maps?
      file=api&amp;v=2&amp;key=ABCDEFGH..."
      type="text/javascript"></script>
    <script type="text/javascript">
    //<![CDATA[
    function load() {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(
        document.getElementById("map"));
        map.addControl(new GSmallMapControl());
        map.setCenter(
        new GLatLng(37.4419, -122.1419), 13);
        function createMarker(point, text, title) {
          var marker =
          new GMarker(point,{title:title});
          GEvent.addListener(
          marker, "click", function() {
            marker.openInfoWindowHtml(text);
          });
          return marker;
        }
        var marker = createMarker(
        new GLatLng(37.4419, -122.1419),
        'Marker text', 'Example Title text');
        map.addOverlay(marker);
      }
    }
    //]]>
    </script>
  </head>
  <body onload="load()" onunload="GUnload()">
    <div id="map"
    style="width: 500px; height: 300px"></div>
  </body>
</html>

Here is an example.

PHP

In order to add multiple markes, we’ll use some PHP to iterate over some marker points. Let’s use an array in this example, but you could get the marker points for example from database or xml file. Put the following lines around marker adding lines:

<?php
$points = Array(1 => "37.4389, -122.1389",
2 => "37.4419, -122.1419",
3 => "37.4449, -122.1449");
foreach ($points as $key => $point) {
?>
var marker = createMarker(
new GLatLng(<?php echo $point ?>),
'Marker text <?php echo $key ?>',
'Example Title text <?php echo $key ?>');
map.addOverlay(marker);
<?php } ?>

And here is the final code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
  Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html;
    charset=utf-8"/>
    <title>Google Maps JavaScript API Example</title>
    <script src="http://maps.google.com/maps?
      file=api&amp;v=2&amp;key=ABCDEFGH..."
      type="text/javascript"></script>
    <script type="text/javascript">
    //<![CDATA[
    function load() {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(
        document.getElementById("map"));
        map.addControl(new GSmallMapControl());
        map.setCenter(
        new GLatLng(37.4419, -122.1419), 13);
        function createMarker(point, text, title) {
          var marker =
          new GMarker(point,{title:title});
          GEvent.addListener(
          marker, "click", function() {
            marker.openInfoWindowHtml(text);
          });
          return marker;
        }
        <?php
        $points = Array(
        1 => "37.4389, -122.1389",
        2 => "37.4419, -122.1419",
        3 => "37.4449, -122.1449");
        foreach ($points as $key => $point) {
        ?>
        var marker = createMarker(
        new GLatLng(<?php echo $point ?>),
        'Marker text <?php echo $key ?>',
        'Example Title text <?php echo $key ?>');
        map.addOverlay(marker);
        <?php } ?>
      }
    }
    //]]>
    </script>
  </head>
  <body onload="load()" onunload="GUnload()">
    <div id="map"
    style="width: 500px; height: 300px"></div>
  </body>
</html>

And here is the final example.

If you have let’s say hundereds or thousands and more markers in your map, you might want to give a try to Clusterer. It is an excellent and easy to use JavaScript library by Jef Poskanzer.

This how to showed only very basic usage of Google Maps API. To learn more Google Maps API has a good documentation and Mike has many excellent examples of the basics and more andvanced stuff.

Minimal How to Use Google Maps API With PHP Read More »

From Del.icio.us to Google Bookmarks

Google BookmarksIf you are for Google Bookamarks, you might be interested in about bookmarks importer from Del.icio.us to Google

Google Bookmarks needs still quite a lot improvements to become a competitior for other bookmarking services, but still it has it’s advantages if you use also other Google’s services. Google Bookmarks are also private. Persistent.info has a script for importing Del.icio.us bookmarks to Google. You can use the script on their website or copy the script and run your own copy.

From Del.icio.us to Google Bookmarks Read More »

Internet Explorer 7 Is Here

Internet Explorer 7Microsoft has released the final version of Internet Explorer 7. It is available now as a manual download or on November via Windows Automatic Update.

Microsoft has officially released the final version of Internet Explorer 7.0 (IE7) for Windows XP SP2 with build 7.0.5730.11. IE7 is available for free download at Internet Explorer Home. Though IE7 will be pushed via Automatic Windows Updates on November, it can be disabled from auto-installing. Some localised language edition of IE 7 will be released within the next few weeks and others later.

Internet Explorer 7 features tabbed browsing, web search widget from the toolbar, advanced printing options such as shrink-to-fit printing, anti-phishing filter, ability to discover, read and subscribe to RSS feeds directly from IE7. More importantly, IE7 has improved security features such as ActiveX opt-in and improved support for web standards and CSS.

Mozilla is also expected to release final Firefox 2 anytime this month. So you would better hurry up to test IE before that…

Internet Explorer 7 Is Here Read More »

Boot Up Windows Before You Even Log In

Windows XP LoginWith this tip you can make Windows boot up to feel a little faster.

If you wait couple minutes after turning your computer on to log in Windows and after log in you wait still some more minutes before Windows is ready to go, this tip is for you and it will save you minutes every day. After booting up to login screen Windows will login automatically and lock the desktop right after that.

So, you boot up your computer and next you unlock the desktop and can start surfing the web right away. Note that for security reasons, which are saving password as plaintext into Windows registry and desktop being unlock for a little moment during the booting up, this really probably suitable only for home computer with singel user.

Boot Up Windows Before You Even Log In Read More »

SlimTimer Makes Time Tracking Easy

SlimTimerSlimTimer makes time tracking an easy task. It is a free web based service and it has an public API.

SlimTimer is a web based time tracking service. It’s simple, free and makes time tracking easy. SlimTimer works like a stop watch on the web. According to builder Richard White, SlimTimer is about:

  • Making time tracking easier by eliminating timesheets in favor of using a web based timer.
  • Time tracking on an individual basis. You can set who your coworkers and stakeholders are on each specific task.
  • Getting rid of rigid client > project > task hierarchies in lieu of using tagging to create a workflow that works best for you.
  • Creating transparency between the people working on a project and those paying for it. Your clients can view time worked on their project in real time, avoiding any surprises when the invoice arrives.

SlimTimer keeps it’s promise being easy. Create yourself a free account and you’ll be tracking time in a minute.

SlimTimer API

On sunday the SlimTimer public API was released. It’s REST-based and fluent in XML, YAML and JSON. The Public API allows secure access to SlimTimer user data for syncing with external applications.

SlimTimer Makes Time Tracking Easy Read More »

Internet Explorer Information Bar And Javadocs

Internet Explorer Information Bar And JavadocsA little tip for disabling annoying Internet Explorer’s Information Bar when opening Javadocs locally.

It is quite annoing when you open Java API Javadoc with Internet Explorer 6 from your local computer and every time you get the IE’s Information Bar saying: “To help protect your security, Internet Explorer has restricted this file from showing active content that could access your computer. Click here for options…” You can select Allow Blocked Content…, but IE won’t remember the choise next time.

To disable this message to save little time and avoid frustration, go to Internet Explorer’s Tools > Internet options… > Advanced and under Security check the “Allow active content to run files on My Computer“.

Internet Explorer Information Bar And Javadocs Read More »

Scroll to Top