PHP json_encode to JS Array of Objects - javascript

I'm trying to figure out how to translate a PHP array to json, to a javascript array of objects:
$latlng = array();
$properties = get_posts(array(
'posts_per_page' => -1,
'post_type' => array('house', 'townhouse', 'condo'),
'status' => 'publish'
));
foreach ($properties as $prop) {
$lat = get_field('latitude', $prop, false);
$lng = get_field('longitude', $prop, false);
array_push($latlng, array($lat, $lng));
}
echo json_encode($latlng);
wp_die();
But I need the final JS array to look like this:
locations = [
{lat: -31.563910, lng: 147.154312},
{lat: -33.718234, lng: 150.363181},
{lat: -33.727111, lng: 150.371124},
{lat: -33.848588, lng: 151.209834},
{lat: -33.851702, lng: 151.216968},
{lat: -34.671264, lng: 150.863657},
{lat: -35.304724, lng: 148.662905},
{lat: -36.817685, lng: 175.699196},
{lat: -36.828611, lng: 175.790222},
{lat: -37.750000, lng: 145.116667},
{lat: -37.759859, lng: 145.128708},
{lat: -37.765015, lng: 145.133858},
{lat: -37.770104, lng: 145.143299},
{lat: -37.773700, lng: 145.145187},
{lat: -37.774785, lng: 145.137978},
{lat: -37.819616, lng: 144.968119},
{lat: -38.330766, lng: 144.695692},
{lat: -39.927193, lng: 175.053218},
{lat: -41.330162, lng: 174.865694},
{lat: -42.734358, lng: 147.439506},
{lat: -42.734358, lng: 147.501315},
{lat: -42.735258, lng: 147.438000},
{lat: -43.999792, lng: 170.463352}
]
This is so I can use locations.map(function(location, i) { to iterate over the bounds.

Your $latlng is an array of arrays, and you want your JSON to be an array of objects, with lat and lng fields. So you need to do:
$latlng = array();
$properties = get_posts(array(
'posts_per_page' => -1,
'post_type' => array('house', 'townhouse', 'condo'),
'status' => 'publish'
));
foreach ($properties as $prop) {
$lat = get_field('latitude', $prop, false);
$lng = get_field('longitude', $prop, false);
array_push($latlng, array('lat' => $lat, 'lng' => $lng));
}
echo json_encode($latlng, JSON_NUMERIC_CHECK /* PHP >= 5.3.3 */);
wp_die();
We just pushed an object instead of an array containing the values:
array_push($latlng, array('lat' => $lat, 'lng' => $lng));
Hope it helps.

Related

Marker Clustering on Google Maps with JSON multi markers

I've created a map with multi markers from a JSON file. I'm trying to add a marker cluster, but I'm ending up with the cluster and markers showing at the same time.
This is what I see at the moment:
I've added the script below to my index.html page:
<script src="https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js"></script>
This is the relevant part of my maps.js file:
let map;
let jsonData = "assets/Data/maps.json";
const countries = [
{ lat: 48.857497, lng: 2.347628, zoom: 5, name: "France" },
//Brazil
{ lat: -15.793889, lng: -47.882778, zoom: 5, name: "Brazil" }
];
function initMap() {
const mapOptions = {
center: {
lat: 9.072264,
lng: 7.491302
},
zoom: 2,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map"), mapOptions);
countries.forEach(function (data) {
let countriesMarker = new google.maps.Marker({
map: map,
position: { lat: data.lat, lng: data.lng },
title: data.name
});
$("#selectlocation").append('<option value="' + [data.lat, data.lng, data.zoom].join('|') + '">' + data.name + '</option>');
});
let infowindow = new google.maps.InfoWindow();
//Method found on StackOverflow: https://stackoverflow.com/questions/28606149/load-data-from-json-file-into-map-markers-in-google-maps
$.getJSON(jsonData, function (jsonMarkers) {
$.each(jsonMarkers.markers, function (key, data) {
let latLng = new google.maps.LatLng(data.lat, data.lng);
let marker = new google.maps.Marker({
position: latLng,
map: map,
title: data.title
});
var clusterMarkers = jsonMarkers.markers.map(function (location) {
return new google.maps.Marker({
position: location
});
});
var markerCluster = new MarkerClusterer(map, clusterMarkers,
{ imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m' });
});
});
}
You have multiple issues.
As #xomena indicated you are creating the markers twice, remove one of those sets of code.
You are creating a MarkerClusterer for each marker. Move the creation of the MarkerClusterer out of the loop that creates the markers.
Something like this should work:
$.each(jsonMarkers.markers, function (key, data) {
let latLng = new google.maps.LatLng(data.lat, data.lng);
let marker = new google.maps.Marker({
position: latLng,
map: map,
title: data.title
});
clusterMarkers.push(marker);
});
var markerCluster = new MarkerClusterer(map, clusterMarkers,
{ imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'
});
proof of concept fiddle
code snippet:
let map;
let jsonData = "assets/Data/maps.json";
const countries = [
{ lat: 48.857497, lng: 2.347628, zoom: 5, name: "France" },
//Brazil
{ lat: -15.793889, lng: -47.882778, zoom: 5, name: "Brazil" }
];
function initMap() {
const mapOptions = {
center: {
lat: -19.072264,
lng: 117.491302
},
zoom: 1,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map"), mapOptions);
countries.forEach(function (data) {
let countriesMarker = new google.maps.Marker({
map: map,
position: { lat: data.lat, lng: data.lng },
title: data.name
});
$("#selectlocation").append('<option value="' + [data.lat, data.lng, data.zoom].join('|') + '">' + data.name + '</option>');
});
let infowindow = new google.maps.InfoWindow();
let clusterMarkers = [];
//Method found on StackOverflow: https://stackoverflow.com/questions/28606149/load-data-from-json-file-into-map-markers-in-google-maps
// $.getJSON(jsonData, function (jsonMarkers) {
var jsonMarkers = {markers: [
{lat: -31.563910, lng: 147.154312},
{lat: -33.718234, lng: 150.363181},
{lat: -33.727111, lng: 150.371124},
{lat: -33.848588, lng: 151.209834},
{lat: -33.851702, lng: 151.216968},
{lat: -34.671264, lng: 150.863657},
{lat: -35.304724, lng: 148.662905},
{lat: -36.817685, lng: 175.699196},
{lat: -36.828611, lng: 175.790222},
{lat: -37.750000, lng: 145.116667},
{lat: -37.759859, lng: 145.128708},
{lat: -37.765015, lng: 145.133858},
{lat: -37.770104, lng: 145.143299},
{lat: -37.773700, lng: 145.145187},
{lat: -37.774785, lng: 145.137978},
{lat: -37.819616, lng: 144.968119},
{lat: -38.330766, lng: 144.695692},
{lat: -39.927193, lng: 175.053218},
{lat: -41.330162, lng: 174.865694},
{lat: -42.734358, lng: 147.439506},
{lat: -42.734358, lng: 147.501315},
{lat: -42.735258, lng: 147.438000},
{lat: -43.999792, lng: 170.463352}
]};
$.each(jsonMarkers.markers, function (key, data) {
let latLng = new google.maps.LatLng(data.lat, data.lng);
if (!data.title)
data.title = ""+key;
let marker = new google.maps.Marker({
position: latLng,
map: map,
title: data.title
});
clusterMarkers.push(marker);
});
var markerCluster = new MarkerClusterer(map, clusterMarkers,
{ imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'
});
// });
}
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="map"></div>
<script src="https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js"></script>
<!-- Replace the value of the key parameter with your own API key. -->
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap">
</script>

Google Map Clustering not working with single location

I am using Google Map Clustering and opening Info Windows after clicking on particular cluster using clusterclick which is working absolutely fine.
However, when I have single location, its not going inside clusterclick event and hence I am unable to open my Info Window.
I am trying to add below events but its not firing when I have single location as data.
google.maps.event.addListener(markers, 'click', (function (markers) {
alert('markersclick');
})(markers));
google.maps.event.addListener(map, 'click', function () {
alert('mapclick');
});
Here is my full source code (I have not added Info Window code because as of not even above events not firing).
<div id="map"></div>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=MY_API_KEY">
</script>
<script src="https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js"></script>
<script>
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 3,
center: {lat: -28.024, lng: 140.887}
});
// Create an array of alphabetical characters used to label the markers.
var labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var markers = locations.map(function(location, i) {
return new google.maps.Marker({
position: location,
label: labels[i % labels.length]
});
});
google.maps.event.addListener(markers, 'click', (function (markers) {
alert('markersclick');
})(markers));
google.maps.event.addListener(map, 'click', function () {
alert('mapclick');
});
// Add a marker clusterer to manage the markers.
var markerCluster = new MarkerClusterer(map, markers,
{imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m',
gridSize: 100,
zoomOnClick: true,
maxZoom: 10});
google.maps.clusterclick.addListener(markerCluster, "clusterclick", function(cluster, event) {
alert('hey');
});
}
var locations = [
{lat: -31.563910, lng: 147.154312},
{lat: -31.563910, lng: 147.154312},
{lat: -31.563910, lng: 147.154312},
{lat: -31.563910, lng: 147.154312},
{lat: -31.563910, lng: 147.154312},
{lat: -31.563910, lng: 147.154312},
{lat: -31.563910, lng: 147.154312},
{lat: -31.563910, lng: 147.154312},
{lat: -31.563910, lng: 147.154312},
{lat: -31.563910, lng: 147.154312},
{lat: -31.563910, lng: 147.154312},
{lat: -31.563910, lng: 147.154312},
{lat: -31.563910, lng: 147.154312},
{lat: -31.563910, lng: 147.154312},
{lat: -31.563910, lng: 147.154312},
{lat: -31.563910, lng: 147.154312},
{lat: -31.563910, lng: 147.154312},
{lat: -31.563910, lng: 147.154312},
{lat: -31.563910, lng: 147.154312},
{lat: -31.563910, lng: 147.154312},
{lat: -31.563910, lng: 147.154312},
{lat: -31.563910, lng: 147.154312},
{lat: -31.563910, lng: 147.154312},
{lat: -31.563910, lng: 147.154312}
];
initMap();
</script>
<style>
#map {
height: 100%;
}
</style>
If I change my locations array to something like this.
var locations = [
{lat: -31.563910, lng: 147.154312},
];
Then my clusterclick not firing as well as markers click event.
Also I am getting an error saying..
TypeError: google.maps.clusterclick is undefined
But I can have records like this.
Can someone guide me what I am doing wrong here ? Why its not working with single location ?
In the code you posted, markers is an array of google.maps.Marker objects. You can't add the listener on the array. You have to loop through your array and add the listener to each Marker individually.
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 3,
center: {
lat: -28.024,
lng: 140.887
}
});
// Create an array of alphabetical characters used to label the markers.
var labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var locations = [{
lat: -31.563510,
lng: 147.154312
},
{
lat: -31.563610,
lng: 147.154312
},
{
lat: -31.563710,
lng: 147.154312
},
{
lat: -31.563810,
lng: 147.154312
}
];
var markers = [];
var infowindow = new google.maps.InfoWindow();
for (var i = 0; i < locations.length; i++) {
var marker = new google.maps.Marker({
position: locations[i],
map: map,
label: labels[i % labels.length]
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent('Label: ' + this.getLabel());
infowindow.open(map, this);
});
markers.push(marker);
}
// Add a marker clusterer to manage the markers.
var markerCluster = new MarkerClusterer(map, markers, {
imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m',
gridSize: 100,
zoomOnClick: true,
maxZoom: 10
});
}
initMap();
#map {
height: 200px;
}
<div id="map"></div>
<script src="https://maps.googleapis.com/maps/api/js"></script>
<script src="https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js"></script>

Autozoom and autocenter for Google maps Clustermap

What is the best way to add auto-zoom and auto-center a google cluster maps? Currently the center and zoom is hard-coded with conflicts with dynamically generated markers.
<<div id="map"></div>
<script>
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 3,
center: {lat: -28.024, lng: 140.887}
});
var labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var markers = locations.map(function(location, i) {
return new google.maps.Marker({
position: location,
label: labels[i % labels.length]
});
});
// Add a marker clusterer to manage the markers.
var markerCluster = new MarkerClusterer(map, markers,
{imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
}
var locations = [
{lat: -31.563910, lng: 147.154312},
{lat: -33.718234, lng: 150.363181},
{lat: -33.727111, lng: 150.371124},
{lat: -33.848588, lng: 151.209834},
{lat: -33.851702, lng: 151.216968},
{lat: -34.671264, lng: 150.863657},
{lat: -35.304724, lng: 148.662905},
{lat: -36.817685, lng: 175.699196},
{lat: -36.828611, lng: 175.790222},
{lat: -37.750000, lng: 145.116667},
{lat: -37.759859, lng: 145.128708},
{lat: -37.765015, lng: 145.133858},
{lat: -43.999792, lng: 170.463352}
]
</script>
<script src="https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js">
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">
</script>
Add the marker locations to a google.maps.LatLngBounds object, then call map.fitBounds() on with the resulting bounds.
var bounds = new google.maps.LatLngBounds();
var markers = locations.map(function(location, i) {
bounds.extend(location);
map.fitBounds(bounds);
// ...
proof of concept fiddle
code snippet:
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 3,
center: {
lat: -28.024,
lng: 140.887
}
});
var labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var bounds = new google.maps.LatLngBounds();
var markers = locations.map(function(location, i) {
bounds.extend(location);
map.fitBounds(bounds);
return new google.maps.Marker({
position: location,
label: labels[i % labels.length]
});
});
// Add a marker clusterer to manage the markers.
var markerCluster = new MarkerClusterer(map, markers, {
imagePath: 'https://cdn.jsdelivr.net/gh/googlemaps/v3-utility-library#07f15d84/markerclustererplus/images/m'
// was 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'
});
}
var locations = [{
lat: -31.563910,
lng: 147.154312
}, {
lat: -33.718234,
lng: 150.363181
}, {
lat: -33.727111,
lng: 150.371124
}, {
lat: -33.848588,
lng: 151.209834
}, {
lat: -33.851702,
lng: 151.216968
}, {
lat: -34.671264,
lng: 150.863657
}, {
lat: -35.304724,
lng: 148.662905
}, {
lat: -36.817685,
lng: 175.699196
}, {
lat: -36.828611,
lng: 175.790222
}, {
lat: -37.750000,
lng: 145.116667
}, {
lat: -37.759859,
lng: 145.128708
}, {
lat: -37.765015,
lng: 145.133858
}, {
lat: -43.999792,
lng: 170.463352
}]
google.maps.event.addDomListener(window, "load", initMap);
html,
body,
#map {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<script src="https://cdn.jsdelivr.net/gh/googlemaps/v3-utility-library#07f15d84/markerclustererplus/src/markerclusterer.js"></script>
<!-- was https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js -->
<div id="map"></div>

Post an Array in Javascript to Node.js

First thank you for trying to help me.
I want to know how pass(post,get) an Array in JS to Node.js backend to parse it then.
This is my Array:
var Coordinates = [
{lat: 37.772, lng: -122.214},
{lat: 21.291, lng: -157.821},
{lat: -18.142, lng: 178.431},
{lat: -27.467, lng: 153.027}
];
In the real stage, this Array is created when the user click the Google map. But this Array is an example what i need to send to my Node.js backend.
Thank you
You can do JSON.stringify(). This will convert your array into a string, which can be passed across the network via GET or POST. In the node server, you can parse this string and get the array using JSON.parse. Adding a working Example below
var Coordinates = [
{lat: 37.772, lng: -122.214},
{lat: 21.291, lng: -157.821},
{lat: -18.142, lng: 178.431},
{lat: -27.467, lng: 153.027}
];
var a = JSON.stringify(Coordinates)
console.log('Coordinates string: ', a)
var parsed = JSON.parse(a)
console.log('Parsed Coordinates: ', parsed)

Google Maps. Overlay over map excluding the area

I'm using the Google Maps JavaScript API and I have to gray out the whole map excluding two dots on the map (ideally the line between two dots). The dot itself it's a GM coordinates.
As result I expect something similar with image below:
I can give you a hint.
If you array<array<LatLng>> to Polygon, you can make a hole polygon.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<style type="text/css">
#map_canvas {
width: 600px;
height: 400px;
border: 1px solid gray;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?language=ja"></script>
<script>
function initialize() {
var mapDiv = document.getElementById("map_canvas");
var map = new google.maps.Map(mapDiv, {
mapTypeId : google.maps.MapTypeId.ROADMAP
});
var paths = [[
{lat: 41.795692, lng: 140.756214},
{lat: 41.795492, lng: 140.756150},
{lat: 41.794700, lng: 140.757051},
{lat: 41.795556, lng: 140.757813},
{lat: 41.795764, lng: 140.757759},
{lat: 41.796140, lng: 140.758349},
{lat: 41.796044, lng: 140.758628},
{lat: 41.796468, lng: 140.759819},
{lat: 41.797283, lng: 140.759014},
{lat: 41.797243, lng: 140.758746},
{lat: 41.797811, lng: 140.758478},
{lat: 41.797915, lng: 140.758714},
{lat: 41.798995, lng: 140.758532},
{lat: 41.798611, lng: 140.757158},
{lat: 41.798459, lng: 140.757105},
{lat: 41.798403, lng: 140.756343},
{lat: 41.798587, lng: 140.756246},
{lat: 41.798763, lng: 140.754884},
{lat: 41.797795, lng: 140.754959},
{lat: 41.797667, lng: 140.755152},
{lat: 41.797067, lng: 140.754970},
{lat: 41.797035, lng: 140.754648},
{lat: 41.796180, lng: 140.753993},
{lat: 41.795892, lng: 140.755302},
{lat: 41.796004, lng: 140.755517}
], [
{lat: 41.79873502198214,lng: 140.75676172883607},
{lat: 41.79916701538921, lng: 140.75850996560666},
{lat: 41.79914485048169, lng: 140.7587355674591},
{lat: 41.7990097958377, lng: 140.75889128372955},
{lat: 41.79765499919136, lng: 140.75906845767213},
{lat: 41.796517596211274,lng: 140.76016803505706},
{lat: 41.796368000728584,lng: 140.76021618650816},
{lat: 41.79622196495789, lng: 140.76011429828645},
{lat: 41.795668, lng: 140.75844600000005},
{lat: 41.79445009984378, lng: 140.75732211342245},
{lat: 41.794408009120424,lng: 140.75713163558203},
{lat: 41.79443604749871, lng: 140.75693313624572},
{lat: 41.79504399484126, lng: 140.75620355952447},
{lat: 41.79469004147552, lng: 140.7551119011191},
{lat: 41.79468803224198, lng: 140.7549643802871},
{lat: 41.794772, lng: 140.75484099999994},
{lat: 41.795688013079555,lng: 140.75483010185235},
{lat: 41.79588801219829, lng: 140.75383221957395},
{lat: 41.796148, lng: 140.75363900000002},
{lat: 41.79740700714976, lng: 140.75463119510653},
{lat: 41.798787067556184,lng: 140.75458362698373},
{lat: 41.79894300457668, lng: 140.7547121759186},
{lat: 41.79898498098061, lng: 140.75494811176304},
{lat: 41.79872702373399, lng: 140.7566860846557}
], [
{lat: 41.79509359115337, lng: 140.7559088009109},
{lat: 41.795123461144776, lng: 140.75608584124757},
{lat: 41.79546948885738, lng: 140.7556779973297},
{lat: 41.79554756063853, lng: 140.7555651964035},
{lat: 41.795647713509155, lng: 140.7550391871414},
{lat: 41.794831758258425, lng: 140.75507730157472}
]];
// Draw polygon
var polygon = new google.maps.Polygon({
paths: paths,
map: map,
strokeColor: "blue",
strokeWeight: 2,
fillColor: "red",
fillOpacity: 0.75
});
var bounds = new google.maps.LatLngBounds();
polygon.getPath().forEach(function(position) {
bounds.extend(position);
});
map.fitBounds(bounds);
}
google.maps.event.addDomListener(window, "load", initialize);
</script>
</head>
<body>
<div id="map_canvas"></div>
</body>
</html>

Categories

Resources