How can I create numbered map markers in Google Maps V3?

后端 未结 16 1003
予麋鹿
予麋鹿 2020-11-28 18:13

I\'m working on a map that has multiple markers on it.

These markers use a custom icon, but I\'d also like to add numbers on top. I\'ve seen how this has been accomp

16条回答
  •  醉酒成梦
    2020-11-28 18:47

    It's quite feasible to generate labeled icons server-side, if you have some programming skills. You'll need the GD library at the server, in addition to PHP. Been working well for me for several years now, but admittedly tricky to get the icon images in synch.

    I do that via AJAX by sending the few parameters to define the blank icon and the text and color as well as bgcolor to be applied. Here's my PHP:

    header("Content-type: image/png");
    //$img_url = "./icons/gen_icon5.php?blank=7&text=BB";
    
    function do_icon ($icon, $text, $color) {
    $im = imagecreatefrompng($icon);
    imageAlphaBlending($im, true);
    imageSaveAlpha($im, true);
    
    $len = strlen($text);
    $p1 = ($len <= 2)? 1:2 ;
    $p2 = ($len <= 2)? 3:2 ;
    $px = (imagesx($im) - 7 * $len) / 2 + $p1;
    $font = 'arial.ttf';
    $contrast = ($color)? imagecolorallocate($im, 255, 255, 255): imagecolorallocate($im, 0, 0, 0); // white on dark?
    
    imagestring($im, $p2, $px, 3, $text, $contrast);    // imagestring  ( $image, $font, $x, $y, $string, $color)
    
    imagepng($im);
    imagedestroy($im);
    }
    $icons =   array("black.png", "blue.png", "green.png", "red.png", "white.png", "yellow.png", "gray.png", "lt_blue.png", "orange.png");      // 1/9/09
    $light =   array( TRUE,         TRUE,       FALSE,       FALSE,     FALSE,      FALSE,      FALSE,          FALSE,      FALSE);     // white text?
    
    $the_icon = $icons[$_GET['blank']];             // 0 thru 8 (note: total 9)
    $the_text = substr($_GET['text'], 0, 3);        // enforce 2-char limit
    do_icon ($the_icon, $the_text,$light[$_GET['blank']] ); 
    

    It's invoked client-side via something like the following: var image_file = "./our_icons/gen_icon.php?blank=" + escape(icons[color]) + "&text=" + iconStr;

提交回复
热议问题