php google maps v3

The third version of googlemaps api let You do many useful things You should need to do with a map, thoose api is medium huge, and some things are not so often used, on the other hand some stuff are more frequently used and speed up the usage without writing a single javascript line could be a good idea.

Having a php class that allows You to render a map and use a good subset of api v3 in few simple lines could be a good solution to achieve the result.

I came up with a clean, handy and useful class that render the map allowing the programmer to use many elements of the last Api in the easiest way. Moreover I added some extra functionalities like “flickr layer”. Take a look at the samples of usage of the class now and good mapping !!!

Here you can download the class, and here you`ll find many samples.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
Posted in javascript, php Tagged with: , ,
31 comments on “php google maps v3
  1. purple Edge says:

    Very nice class Federico, thank you!
    Is there already a method, or do you know of an easy way, to get the map to auto-zoom to the bounds of the points plotted?

    Also, if I add an to a baloon, it doesn’t reposition the map to show the entire image. It repositions the map to show the text on the baloon and then the image loads but the image part of the baloon is outside the bounds of the map. Is there a method to manually set the baloon size and would this help with repositioning the map to show the whole baloon? Or is there some other technique to show an image in a baloon?

    • purple Edge says:

      OK, putting some text before the img in the baloon fixes the repositioning problem!

      • Federico says:

        What about cache? please let me know if even with the text before the image, the problem come back if you disable browser cache… somehow it would confirm my previous thoughts.
        F.

    • Federico says:

      Thank You purple Edge,
      it`s great to have good feedback for old stuff.

      Seen that me too I have not the complete API picture, I took a look inside the class and I confirm that do not exists such a method.
      The easy way is to use fitBounds, after getting the south-west and north-east corners looping all points coords as far as you got’em all. It’s a nice idea thank You. I’ll give a try as soon as possible.
      As far as concerns the image in the baloon it’s a bit tricky, cause it seem like gmaps do not preload the baloon media content, and after rendering the baloon tries to pan the map to show the content of it at best. But what if the image load after that?
      So I suspect that preloading the image could solve it, but I must try out. I’ll tell You soon.

      See You

  2. Joerg says:

    Is there a way to retrieve the driving directions that are sent to the Map als Polyline ?
    I would like to display them next to the map with some css πŸ˜‰

    Best Regards
    Joerg

    • Federico says:

      Hi Joerg,
      a bit rusty about that version of the class… but I found a trivial decent solution adding a new method set_route_panel that before adding a path allows You to declare the id of the element that will be automatically filled with the driving directions. Look at the “Trace paths” sample and download the new class archive.
      I hope I helped You.
      Federico

  3. Arnaud says:

    Hello, Is there a way to add tags to markers and filter them with checkboxes ? would be great !

    • Federico says:

      Hi Arnauld,
      at the moment what You mean is not bundled into the class but I think it’s a good idea. At the moment I’m working on a new version that will include many useful features that cannot be found anywhere, and that feature will be included for sure. You can obviously do it on your own, it could take a couple of hours, let me know !
      .. .and stay tuned for the incoming version
      bye

  4. charleson reyes says:

    Hi,

    Is there a function that shows the user’s current location and centers it automatically? I can see that there is already a js script that gets the user’s location but there is no function that sets this coordinates as the map center. I am currently making a website that wants to automatically center on the user’s location like the Geolocation services and this class of yours is the best I found. I hope there would be a function that Im asking.

    Thanks

    • Federico says:

      Hi Charleson,
      I’m glad you found it useful. Thank you. I have a bad and a good news: tha bad one is that the class you downloaded do not have that functionality, the good one is that even though I wrote that class more than a year ago, (and now I’m working at a new version :D) I spotted the solution with a minimum effort, updated the class, the zip and the second sample (note the “sensor”=>”center” in place of “sensor”=>”true”).
      Hope it helps

      • charleson reyes says:

        Ok that’s great to know! πŸ™‚
        I will check it out right away!

        Thanks for the quick reply!

        • charleson reyes says:

          Hi,

          I’ve seen the new file and yes they are working as expected, one last question though, is there a way to like get the current latitude and longitude using a php function like getUserLatLng() and it will return the lat and lng of the user so we don’t need to use the javascript function given?
          I was planning to get the user’s lat and lng and add a marker for this with custom icon so I can distinguish the user location from other markers.

          Sorry if I’m asking too much but your previous help was really nice and thanks for that.

          Thanks!

          • Federico says:

            Hi, the information about client location comes from enabled browser as far as the page has loaded, told that is not possible
            to use that information from server at the moment you`re writing the response in php.

            You have two solutions:

            ON THE CLIENT
            The modifications, I`ve done to help you, export in
            the public scope (window) the function that returns the location object. You can use that function to get the localization info (standing
            the user agreed). Open console on that page http://www.freakstyle.it/classes/gmapsv3/samples/sample2.php (accept localization)
            and run
            get_initialLocation();
            and use the returned object to add needed marker and baloon with a few javascript lines.

            ON THE SERVER
            You can use a service (commercial) like MaxMind or similar, but beware, all these services accuracy is lousy.

            Greetings

  5. Viktor says:

    Hi,
    Just download your google maps class and tryed to use it and I got php error about undefined constant “CACHE_ACTIVE” in gmaps3simple class.

    PHP Error:
    Message: Use of undefined constant CACHE_ACTIVE – assumed ‘CACHE_ACTIVE’.
    Line Number: 275

    • Federico says:

      Hi Viktor,
      I made some mash with new version I’m working at, download it again or simply remove “CACHE_ACTIVE &&”.
      Bye πŸ™‚

  6. henry says:

    Something to be careful about: when points are not provided as coordinates the class calls the geocode function. It is VERY easy to hit the limit on it. It gave me problems in a page with just 15 points.
    It’s better to provide raw coordinates instead of place names whenever possible, or cache the results if they will be used later.

    • Federico says:

      Every request to G has a upper limit… I thought it was obvious, but it seems like I made a wrong assumption. Now You know and You can manage it; cache reduce the problem but do not resolve(standing your target points will change in time), spend some lines of code for a caching func if it makes You feel better. The only way to resolve is a real commercial account for Gmaps

      Anyway, thank you
      Bye

      • henry says:

        Daily upper limit is in the 1000’s, so there is little chance of hitting it for an occasional application.

        On top of that there seems to be a different limit (I didn’t see it in their docs) for a sequence of requests in a very short time.
        I hit it when adding 15 points to the same map. It seems to clear within a second, so if someone needs to display more points it may be useful to get coordinates first, and later feed them to the map.
        For static points it’s advisable to store their coordinates, so they don’t need to be requested every time the map is generated.

        • henry says:

          Found it in the docs: “the Web Service is rate-limited, so that requests that come in too quickly result in blocking.”

  7. henry says:

    Got your file. Your help is very much appreciated!

  8. Vitus says:

    Thanks for the great work, Federico! This is really handy and powerful.

    • Federico says:

      Thank You Vitus,
      stay rsstuned !… I’m working on a version with many amazing new features that cannot be found anywhere!…and I fixed some bugs. πŸ˜€
      Bye

  9. Mark says:

    Sweet! Yeah that helps me a lot. Thank you so much for the extra effort.

  10. Mark says:

    Wonderful class Frederico.
    Most instance I come across have a layout need to display the map (after user clicks on a link) in a popup window, div (after setting display or visible) or iframe.
    Have you made any efforts to providing samples for such a thing?

    • Federico says:

      Hi Mark,

      first of all thank You.

      If I didn’t misunderstood You mean something like this.

      I used jQuery (porting to any framework is trivial), and three really simple php file that uses a little variation of that sample.
      While popup or iframe are pretty trivial, I spent some more on loading the map on a div because I was trying to get content via ajax and then load sync the response in the div, but it doesn’t work, so I included a php in the hidden div, and at click I bind show of div and initialize of the map.
      Hope it helps!

      Bye

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

*