Jump to content

Google map module


qwertyeee
 Share

Recommended Posts

Google map module.

for use

add in template

<link rel="stylesheet" type="text/css" href="<?php echo $config->urls->FieldtypeGMap?>styles/FieldtypeGMap.css" />

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="<?php echo $config->urls->FieldtypeGMap?>scripts/FieldtypeGMap.js"></script>

$out = '<textarea id="Fieldtype_GMap_location" class="GMap_hidden_textarea">'.htmlspecialchars($page->GMap_location).'</textarea>';
$out .= '<div class="GMap_canvas" id="GMap_location"></div>'; 
$out .= '<script type="text/javascript">';
$out .= 'var GMap_location = new Map("GMap_location");';
$out .= '</script>';
echo($out);

replace GMap_location with your field name

$out = '<textarea id="Fieldtype_your_filed_name" class="GMap_hidden_textarea">'.htmlspecialchars($page->your_filed_name).'</textarea>';
$out .= '<div class="GMap_canvas" id="your_filed_name"></div>'; 
$out .= '<script type="text/javascript">';
$out .= 'var your_filed_name = new Map("your_filed_name");';
$out .= '</script>';
echo($out);

GMap.zip

Link to comment
Share on other sites

Hope you don't mind, but I added some stuff to your GMap module for fun. I modified it so that it has a field configuration option for "add markup automatically". So if you check that option on the field, then you don't have to add any code in your template other than this:

<?=$page->map?> 

In the above, snippet, you would replace 'map' with your map field name.

The updated file is attached. I think this is what you were trying to do before when we were writing in the other thread, so I figured it would be worthwhile for me to add it, in case it helps to demonstrate how.

Another thing that I think would be cool is if there was a configuration option for it to pull an address from some other text field on the page. Then it would geocode and save it, so that it doesn't have to geocode again. Though I'm not yet sure how to do that, so just thinking... :)

Thanks again for your work in putting this module together. This is something I will definitely use.

Edit: Accidentally left a debug "echo" statement in the previous module, so have replaced it with a new version. 

GMap2a.zip

  • Like 1
Link to comment
Share on other sites

  • 3 weeks later...

I removed folder with GMap module by mistake and unpacked there new files, but now I get message:

"Exception: Fieldtype 'FieldtypeGMap' does not exist (in /var/www/pw/wire/core/Field.php line 156)

How can I:

a) uninstall old instance of GMap(I'm afraid of incoherence in database) or

b) set up installation manually?

Link to comment
Share on other sites

To uninstall something you want to do it through admin>modules. If the dir/files were removed without uninstalling, it's best to put them back where they were. It sounds like you may have already tried this? If so double check that the dirname and filenames are the same as before.  If that still doesn't work, you can always remove the entry from the modules table manually, if you are sure it's not actively being used on any fields. Better yet, try deleting the modules cache file in site/assets/. I dont recall what the filename is (I'm not at computer) but I think the name is recognizable as being for modules. Please let me know how this works out.

Link to comment
Share on other sites

  • 5 months later...

I would like to display the map (using

<?=$page->map?>

) only when I've set a value in the database, since I don't want to display a map with default location.

Is this possible?

How would the if-statement look like? I tried several things, but could not get it working...

Thanks in advance!

/Jasper

Link to comment
Share on other sites

This mightwork:

<? echo $page->map ? $page->map : ''; ?>

Thank you for your reply. Unfortunately it doesn't work.

And if it worked it wouldn't have solved my problem completely (but you couldn't know that).

What I am looking for is something like:

if "google_maps is exists" {
display map with surrounding div, makup etc.
}

/Jasper

Link to comment
Share on other sites

I didn't write the module (other than adding a few edits), so can't say for certain, but I'm thinking this might work:

<?php
$page->setOutputFormatting(false);
if($page->map) {
    // display the map
    $page->setOutputFormatting(true); 
    echo $page->map;
} else {
    // no map to display.
    $page->setOutputFormatting(true); 
}

If that doesn't work, you may want to just modify the module itself. I would  try editing the formatValue() function and adding this at the top:

if(!$value) return '';

Lastly, another option is that you could edit map field's config settings and turn off the auto-generated map markup. Then generate your own (like in the earlier examples in this thread).

Link to comment
Share on other sites

I didn't write the module (other than adding a few edits), so can't say for certain, but I'm thinking this might work:

<?php
$page->setOutputFormatting(false);
if($page->map) {
    // display the map
    $page->setOutputFormatting(true); 
    echo $page->map;
} else {
    // no map to display.
    $page->setOutputFormatting(true); 
}

This works!  ;D

Thanks!

/Jasper

Link to comment
Share on other sites

  • 2 months later...

Thanks for the module guys, and the clear installation instructions. I am impressed every time I see Ryan solve someone problems with just one quick reply...

Anyhow, I installed this module and tried it out. However, in the page edit screen, all I saw was a grey box where the map was supposed to load.

I added the field to the page template. I haven't yet added the $page->map into the template file...

Any ideas where to start debugging this? I am running this on my home Ubuntu server which I use for development.. so my configurations could be off~ I tried firefox and explorer and got the same result~

EDIT: wait... explorer gave the error message "stack overflow on line: 28"

Link to comment
Share on other sites

I have been meaning to report this bug for a while.

If you see the gray box, try resizing the browser window and the map should appear. All it needs is a slight resize of the window and it should work as expected (tested on processwire 2.1 with Chrome, Safari, Firefox).

You have to do this every-time you want to use the map (little bit annoying).

I have found this module very useful - would be nice if it was polished off and added as an official field type for Processwire. I'd be happy to discuss sponsoring the development if there was interest.

Link to comment
Share on other sites

  • 4 weeks later...

If you see the gray box, try resizing the browser window and the map should appear. All it needs is a slight resize of the window and it should work as expected (tested on processwire 2.1 with Chrome, Safari, Firefox).

You have to do this every-time you want to use the map (little bit annoying).

Sorry, I didn't notice the reply and ended up working on something else in the meantime, but thanks for the reply, I'll try this out again shortly :)

Link to comment
Share on other sites

  • 6 months later...

Small issue with this module: It assumes that jQuery is referenced in the <head> of the html, which quite a lot of people (including me :)) don't do. For perfomance reasons (and because it's the HTML5 boilerplates default), I like to reference jQuery before the closing </body>.

I assume it would suffice to edit the FieldtypeGMap.module like this:

$event->return = str_ireplace("</head>", "$css\n</head>", $out);
$event->return = str_ireplace("</body>", "$js\n</body>", $out);

(haven't tested it yet), but that's of course not safe in case of an update to the module. It would be more flexible to have this as an option in the module. Also, the script call in the content area should probably go there also.

I'm fine with adding a Google Map manually if this is not feasible for the module as a general option, I just thought I'd mention it. :)

  • Like 1
Link to comment
Share on other sites

  • 3 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...