简体   繁体   中英

Using .php MySQL to add custom .png markers to map

I am using the following code to add markers to a map, it works fine, but I want to add another field to my database for the marker icon so I can display a custom marker icon for each entry in the database.

Does anyone know the what I need to "echo" in order to a custom .png marker for each item as it queries the database?

<?php

     $query = mysqli_query($conn, "SELECT * FROM table")or die(mysql_error());

     while($row = mysqli_fetch_array($query)) {
        $logo = $row['logo'];
        $company = $row['company'];
        $lat = $row['lat'];
        $lon = $row['lon'];
        $desc = $row['desc'];

        echo("addMarker($lat, $lon, '<b>$logo</b><b>$company<br />$desc');\n");
     }
?>

Add another field called "marker" to your table (perhaps with a default value of "marker.png" if you don't explicitly specify a custom marker for that row). Then, include the name of the custom marker in the JS that you're echoing out to the client:

`

 $query = mysqli_query($conn, "SELECT * FROM table")or die(mysql_error());

 while($row = mysqli_fetch_array($query)) {
    $logo = $row['logo'];
    $company = $row['company'];
    $lat = $row['lat'];
    $lon = $row['lon'];
    $marker = $row['marker'];
    $desc = $row['desc'];

    echo("addMarker($lat, $lon, $marker, '<b>$logo</b><b>$company<br />$desc');\n");
 }

?>`

Then in your addMarker() JS function, user $marker to write out an image tag on the map that displays the image.

You need the full address of the image. For example, if you web are stored on www.example.com, on http protocol, you have these markers on images/markers folder, and you store the filename on the database, you can use this:

(Setting text on PHP while):

$icon = ' http://www.example.com/images/marker/ ' . $row['icon']; // Read a database row called icon with a filename, ex marker1.png After this, I understand your addMarker function call to google.maps.Marker() one, on these one, set a new param called icon and set on them the created $icon string.

Remember something: If you load your page on http, you can load marker on http or https. But if you load on https, you can get the marker image from a http resource.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM