开发者

How to create a GMap marker manager on Drupal?

what I need to do is quite simple - I have a content type having CCK field called Number (surprisingly, it contains a decimal number) and this content type also has a location (always 1 location per node). Now instead of typical GMap markers I need to display those numbers from CCK field Number on a GMap, like this:

http://cl.ly/2Y3T3J2B2X1M2w2d0u2n

Any idea how to do it? I had a brief look at the marker managers included开发者_如何学JAVA in GMap module and I might by able to create my own, but the only thing I do not know is how to pass the node data (CCK fields content) into the marker manager.

Any help will be much appreciated.


In the following example, I'm assuming that your numbers go 100, 101, 110, 200. This simple example sequence allows me to illustrate all aspects of the solution.

You can take advantage of the fact that GMap supports sequences of custom markers.

You could therefore create a custom marker folder and in that folder, create a .ini file that looks something like this:

; Defaults
[defaults]
; Note: An empty shadow property will break IE.
; Leave it commented out if you aren't supplying an image.
;shadow = ""
anchorX = 16
anchorY = 51
infoX = 24
infoY = 4

; Marker sets
[numbermarkers]
name = "Number markers"
sequence = "100.png,101.png,110.png,200.png"

; Files

Then create the associated PNG files (you can use other image types) for 100.png, 101.png etc. Press the Regenerate button on the GMap settings page to get it to re-read the ini files, so that your new marker type shows up.

In your GMap settings, choose your marker type as "Number markers" (or whatever name you used in your ini file).

The markers will now be applied in order to your Number type, so ensure that you're sorting the Numbers (e.g. in the View settings if you're using a view) by that decimal number field that you mentioned.

As your nodes are added to the map in numeric order, and the images in the marker sequence also go in numeric order, you should find that each one has the right marker!

Updating in future for new Numbers

If you were to add a Number 105, for example, you'd need to:

  1. Create an associated PNG
  2. update the ini file - add 105.png to the sequence line, keeping the entries there in numeric order (i.e. add 105.png between 101.png and 110.png),
  3. Press the Regenerate button on the GMap settings page to get it to re-read the ini files.
  4. You might also need to clear your standard Drupal cache (drush cc all is your friend).

An issue you may encounter

In my experience, sequences of markers have a bug in GMap, described here and here. Thanks to this bug, only one item from the sequence is used.

An easy way to test this problem is to use GMap's built-in "week" marker type, and check that they don't all show up as "Sun"!

I worked around this by creating a custom module with this code in:

function mymodule_gmap($op, &$map)
{
    if ($op == 'pre_theme_map') {
        if (($map != null) && (array_key_exists('markers', $map)))
        {
            for ($count = 0; $count < sizeof($map['markers']); $count++) {
                $map['markers'][$count]['offset'] = strval($count);
            }
        }
    }
}

This manually sets the "offset" field to ensure that it's correct.

You might be able to adapt this code to make the procedure for adding new Numbers simpler, but I'm not sure - I'll leave you to investigate!

Marker folders

I recommend that you create your custom markers in their own module rather than putting them into GMap's directory, as adding them to GMap makes upgrading harder.

For more on how to do this, see:

  • http://www.hydrant.co.uk/tech/google-maps-custom-markers
  • http://mydrupaltricks.be/article/drupal-7-custom-gmap-markers


Probably You should dynamically create markers with help of imageMagick and attach it to nodes ?


While there may be a straightforward solution to do it within the gmap module (I guess that is what you are referring to), the solution I used faced with a similar problem was to get the field value from Drupal ( 7 ) (longitude and latitude ) and then place the custom marker using Googles javascript API .

Editing the location and gmap modules was a bit too time consuming.

The custom marker can be added by the addMarker function of google's API : http://code.google.com/apis/maps/documentation/javascript/overlays.html#ComplexIcons

Sample code looks like this :

    <?php
    drupal_add_js('http://maps.googleapis.com/maps/api/js?sensor=false');

    $locationID = $node->field_mylocationField_location['und'][0]['lid'] ; 
    $theLocation = location_load_location($locationID);


     drupal_add_js('jQuery(document).ready(function(){                   
               initialize();
               });', 'inline');

    ?>

    <script type="text/javascript">
function initialize() {
var latlng = new google.maps.LatLng(<?php print($theLocation['latitude']) ?>, <?php   print($theLocation['longitude'])?>);
var myOptions = {
  zoom: 12,
  center: latlng,
  mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
    myOptions);

 }

 </script>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜