Programmatically generate Google Maps tokens (API v3)

Is it possible to programmatically generate tokens on Google? For example, I have a database of services and places that I want to map to Google using PHP.

Using the Maps API v3, I created a map and drew the services well using markers and info windows, but I really want to create numbers (1,2,3,4 ... etc.) in the default marker. bubbles depending on the order of their receipt from the database. In addition, I would like to change the color of the marker icon programmatically depending on the type of service in the database.

I use PHP, so I don’t know if there is a script for this, but I was hoping that Google would generate this custom token through the API.

+3
source share
3 answers

Ask PHP to set javascript code.

Here is a small handy javascript function that I wrote to add a marker with the appropriate window.

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, declaring that in some javascripty place you will have some PHP, which will probably resemble (and if your map is 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 have not tested anything and do not know what the data looks like, but this method should work for what you want. I'm not 100% sure what you mean by creating custom markers, because it looks like you're already doing this, just not through PHP.

+4
source

, - , , , Google . , lat/long , , , Google.

I'm not sure if this will help, but it may help you, perhaps pre-encode and save these marker images instead of doing it on the fly. According to the answer of your choice, you rely on Google to create these images while loading your page, you may want to speed up your work by running this code at another stage and saving the output with coordinates to your PHP database.

Greetings

Dan

0
source

Source: https://habr.com/ru/post/1751082/


All Articles