Is it possible to programmatically generate markers in Google? For example, I have a database of services and locations that I want to plot on a Google Map using PHP.
Using the Maps API v3, I have created a map and plotted the services fine using markers and info windows, but what I really want to do is generate numbers (1,2,3,4... etc.) in the default marker bubbles base开发者_开发技巧d on the order they get retrieved from the DB. Also, I'd like to change the colour of the marker icon programmatically based on the service type in the database.
I'm using PHP, so don't know if there's a script to do this, but I had hoped that Google would provide this custom marker generation through the API.
The Maps API requires that you use an image (rather than arbitrary DIV) to display markers, so you would need to render the images server side.
A simple way to do this is to use the Google Charts API to render marker icons: http://code.google.com/apis/chart/docs/gallery/dynamic_icons.html#pins
Example output:
Generated with: https://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=C|FF0000|000000
Have PHP spit out javascript code.
Here's a little convenience javascript function that I wrote to add a marker with a window associated.
function add_marker(opts, place) {
var marker = new google.maps.Marker(opts);
marker.place_id = place.id;
markers[place.id] = marker;
var infowindow = new google.maps.InfoWindow({
content: place.details
});
infowindows[place.id] = infowindow;
google.maps.event.addListener(marker, 'click', function() {
infowindows[marker.place_id].open(map,marker);
});
}
So after declaring that in some javascripty place, you'd then have some PHP that'd probably resemble (and assuming your map is a global called 'map'):
<script type="text/javascript">
<?php
$count = 0;
foreach ($rowset as $row): ?>
add_marker({
position: new google.maps.LatLng(<?php echo $row->lat ?>, <?php echo $row->lng ?>),
title:<?php echo $row->title ?>,
map:map
}, { id:'<?php echo $count ?>', details:'<?php echo $row->details ?>' });
<?php
$count++;
endforeach; ?>
</script>
I haven't tested any of this nor do I have any idea what the data looks like, but that method should work for what you're wanting. I'm not 100% sure what you mean by custom marker generation, because it sounds like you're already doing that, just not through PHP.
I'm not sure if anyone else had this trouble, but I couldn't loop a geocode to plot my points, and Google even consider this an abuse of their service. So my method was to geocode and store the lat/long coordinates at the user input stage instead of the display stage, relieving the bandwidth stress it puts on Google.
I'm not sure if this would help but it may help you to perhaps pre-code and store these marker images instead of doing it on the fly. As it stands with your chosen answer, you are relying on google to produce these images while your page is loading, perhaps you may wish to speed up your service by running this code at a different stage and storing the output with the co-ordinates in your PHP database.
Cheers,
Dan
精彩评论