bjorn2404 / jQuery-Store-Locator-Plugin

A store locator plugin using Google Maps API version 3
MIT License
493 stars 235 forks source link

How to work with data that doesn't look like the example? Eg set Latitude and Longitude to `contact_locations.latitude` #249

Closed mvaneijgen closed 5 years ago

mvaneijgen commented 5 years ago

I have data coming from an external API (JSON) where the data is formatted in a different way than the example. My longitude and latitude are note formatted like this

"lat": "44.947464",
"lng": "-93.320826",

But look like this

"contact_locations": [
{
"latitude": "52.3670468",
"longitude": "4.6646194",
}
],

So I would figure I need to access them in the like contact_locations.latitude, but I have no idea where to set them... Is there a tutorial how to work with this plugin?

mvaneijgen commented 5 years ago

For now I've loaded my external .json file from there created a new json object with the correct key/value pair and fed that to the dataRaw object. This is what it looks like

<?php
$base = 'URL-TO-MY-API.com;
$posttype = "school";
$queries = "?orderby=date&order=desc&per_page=100&_embed";
$dataURL = $base . $posttype . $queries; // path to your JSON file
$JSON = file_get_contents($dataURL);
$JSONraw = json_decode($JSON, true);
$JSONclean = array();
?>
<?php foreach ($JSONraw as $item): ?>
  <?php
  $pushItem = array(
    'id' => $item['id'],
    'title' => $item['title']['rendered'],
    'lat' => $item['acf']['contact_locations'][0]['latitude'],
    'lng' => $item['acf']['contact_locations'][0]['longitude'],
  );
  array_push($JSONclean, $pushItem);
  ?>
<?php endforeach; ?>

than within the .storeLocator() options I load

$("#bh-sl-map-container").storeLocator({
  ...
  dataRaw:<?php echo json_encode($JSONclean) ?>,
  dataType: "json",
  ...
});