最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Custom Map Marker Icons with Dynamically Generated Numbers - Stack Overflow

matteradmin6PV0评论

I want to have numbered map markers for my google maps and currently I'm using the Google Charts API method to dynamically create numbered markers. However I am not able to use my own icons with that method.

Is there a way to use my own custom map marker icons, then overlay/have a number on top of it?

Alternatively, is there a quick way to create 1000 .PNG markers running from number 1 to 1000? Like a batch process in Photoshop

I want to have numbered map markers for my google maps and currently I'm using the Google Charts API method to dynamically create numbered markers. However I am not able to use my own icons with that method.

Is there a way to use my own custom map marker icons, then overlay/have a number on top of it?

Alternatively, is there a quick way to create 1000 .PNG markers running from number 1 to 1000? Like a batch process in Photoshop

Share Improve this question asked Jun 24, 2011 at 6:24 NyxynyxNyxynyx 63.9k163 gold badges507 silver badges856 bronze badges 5
  • Alternatively, is there a quick way to create 1000 .PNG markers... yes you can do that in PHP+GD. Infact, you do not need to pre-create the markers. You can create them on demand like marker-gen.php?text=123. Will that work? – Salman Arshad Commented Jun 24, 2011 at 6:27
  • I dont have much experience with PHP+GD. Suppose I manage to create numbered markers using PHP+GD, can they be brought into Google Maps using icon: "http://www.site./marker-gen.php?text=123"? – Nyxynyx Commented Jun 24, 2011 at 6:40
  • yes, theoretically there is no difference between "http://www.site./marker-123.png" and "http://www.site./marker.php?text=123" provided PHP supplies correct header along with the image. – Salman Arshad Commented Jun 24, 2011 at 8:11
  • 1 I just updated my code a little, minor improvements. – Salman Arshad Commented Jun 24, 2011 at 11:00
  • @Salman A: thank you, you rock :) – Nyxynyx Commented Jun 24, 2011 at 13:22
Add a ment  | 

2 Answers 2

Reset to default 5

I borrowed this code from an article I wrote and tweaked it a little. You should download this image, edit it a bit in Photoshop and place it in the same directory as that of the PHP script. Tweak the numbers in the script until you get something decent.

<?php
define("FONT_SIZE", 6);                            // font size in points
define("FONT_PATH", "c:/windows/fonts/arial.ttf"); // path to a ttf font file
define("FONT_COLOR", 0x00000000);                  // 4 byte color
                                                   // alpha  -- 0x00 thru 0x7F; solid thru transparent
                                                   // red    -- 0x00 thru 0xFF
                                                   // greeen -- 0x00 thru 0xFF
                                                   // blue -- 0x00 thru 0xFF
$text = $_GET["text"];
$gdimage = imagecreatefrompng("marker.png");
imagesavealpha($gdimage, true);
list($x0, $y0, , , $x1, $y1) = imagettfbbox(FONT_SIZE, 0, FONT_PATH, $text);
$imwide = imagesx($gdimage);
$imtall = imagesy($gdimage) - 14;                  // adjusted to exclude the "tail" of the marker
$bbwide = abs($x1 - $x0);
$bbtall = abs($y1 - $y0);
$tlx = ($imwide - $bbwide) >> 1; $tlx -= 1;        // top-left x of the box
$tly = ($imtall - $bbtall) >> 1; $tly -= 1;        // top-left y of the box
$bbx = $tlx - $x0;                                 // top-left x to bottom left x + adjust base point
$bby = $tly + $bbtall - $y0;                       // top-left y to bottom left y + adjust base point
imagettftext($gdimage, FONT_SIZE, 0, $bbx, $bby, FONT_COLOR, FONT_PATH, $text);
header("Content-Type: image/png");
header("Expires: " . gmdate("D, d M Y H:i:s", time() + 60 * 60 * 24 * 180) . " GMT");
imagepng($gdimage);
?>

Sample output on my system:

You can now do this with Google Charts. Here is an example syntax:

(scale|rotation|color|fontsize|bold(b) or normal(_)|text[|line2]
https://chart.googleapis./chart?chst=d_map_spin&chld=1.0|0|FF8844|12|_|221B

Here is the documentation.

Post a comment

comment list (0)

  1. No comments so far