why I use ajax with Google GeoChart api failed - javascript

I'm new to javascript. And I tried to use Google Geochart to create a nice report.
From the example, the code should like:
function drawRegionsMap() {
var data = google.visualization.arrayToDataTable([
['Country', 'Popularity'],
['Germany', 200],
['United States', 300],
['Brazil', 400],
['Canada', 500],
['France', 600],
['RU', 700]
]);
var options = {};
var chart = new google.visualization.GeoChart(document.getElementById('chart_div'));
chart.draw(data, options);
};
And in my case, the data is from a ajax request. So I change my code as:
function drawRegionsMap() {
var jsonData = $.ajax({
url: "json/geo.pl",
dataType: 'json',
async: false
}).responseText;
var data = google.visualization.arrayToDataTable(jsonData);
var options = {};
var chart = new google.visualization.GeoChart(document.getElementById('chart_div'));
chart.draw(data, options);
};
But it not works for me. The Error message in Firefox is "Error: Not an array".
Hope someone can help me. Thanks!
The output of json/geo.pl is:
-bash-4.1$ json/geo.pl
Content-Type: application/json; charset=ISO-8859-1
[
[
"Country",
"Uploads"
],
[
"AL",
1
],
[
"AP",
1
],
[
"AR",
47
],
[
"AT",
25
],
[
"AU",
75
],
[
"AZ",
10
],
[
"BD",
4
],
[
"BE",
65
],
[
"BG",
15
],
[
"BN",
1
],
[
"BR",
177
],
[
"CA",
536
],
[
"CH",
37
],
[
"CI",
3
],
[
"CL",
35
],
[
"CN",
947
],
[
"CO",
4
],
[
"CS",
1
],
[
"CY",
2
],
[
"CZ",
20
],
[
"DE",
135
],
[
"DK",
51
],
[
"DO",
1
],
[
"EE",
26
],
[
"ES",
53
],
[
"EU",
2
],
[
"EU",
9
],
[
"FI",
19
],
[
"FR",
183
],
[
"GB",
341
],
[
"GE",
14
],
[
"GR",
11
],
[
"HK",
94
],
[
"HR",
2
],
[
"HU",
7
],
[
"ID",
37
],
[
"IE",
36
],
[
"IL",
122
],
[
"IN",
543
],
[
"IS",
5
],
[
"IT",
129
],
[
"JP",
200
],
[
"KG",
4
],
[
"KR",
451
],
[
"LB",
3
],
[
"LK",
1
],
[
"LT",
5
],
[
"LU",
4
],
[
"MA",
2
],
[
"MD",
3
],
[
"MO",
3
],
[
"MU",
2
],
[
"MX",
77
],
[
"MY",
43
],
[
"NG",
1
],
[
"NI",
1
],
[
"NL",
33
],
[
"NO",
27
],
[
"NZ",
4
],
[
"OM",
1
],
[
"PA",
4
],
[
"PH",
9
],
[
"PK",
20
],
[
"PL",
76
],
[
"PT",
14
],
[
"PY",
2
],
[
"QA",
2
],
[
"RO",
6
],
[
"RU",
164
],
[
"SA",
30
],
[
"SE",
148
],
[
"SG",
101
],
[
"SK",
34
],
[
"TH",
22
],
[
"TR",
28
],
[
"TT",
1
],
[
"TW",
110
],
[
"UA",
26
],
[
"US",
9386
],
[
"VE",
1
],
[
"VN",
28
],
[
"ZA",
89
],
[
"ZM",
1
]
]

I have fixed the issue by convert json/array from ajax responseText in to javascript array.
function drawRegionsMap() {
var jsonData = $.ajax({
url: "json/geo.pl",
dataType: 'json',
async: false
}).responseText;
var output = new Array();
output = JSON.Parse(jsonData);
var data = google.visualization.arrayToDataTable(output);
var options = {};
var chart = new google.visualization.GeoChart(document.getElementById('chart_div'));
chart.draw(data, options);
};

Related

Looping trough json and extract values

I'm training to work with json and I'm trying to get some values from. That looks like this:
{
"p:FatturaElettronica": {
"$": {
"xmlns:p": "http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2",
"xmlns:ds": "http://www.w3.org/2000/09/xmldsig#",
"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"versione": "FPR12",
"xsi:schemaLocation": "http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2 fatturaordinaria_v1.2.xsd"
},
"FatturaElettronicaHeader": [
{
"DatiTrasmissione": [
{
"IdTrasmittente": [
{
"IdPaese": [
"IT"
],
"IdCodice": [
"01895030995"
]
}
],
"ProgressivoInvio": [
"0HVHG"
],
"FormatoTrasmissione": [
"FPR12"
],
"CodiceDestinatario": [
"J6URRTW"
]
}
],
"CedentePrestatore": [
{
"DatiAnagrafici": [
{
"IdFiscaleIVA": [
{
"IdPaese": [
"IT"
],
"IdCodice": [
"12345678127"
]
}
],
"CodiceFiscale": [
"1127"
],
"Anagrafica": [
{
"Denominazione": [
"GE."
]
}
],
"RegimeFiscale": [
"RF01"
]
}
],
"Sede": [
{
"Indirizzo": [
"Via T. Fusco"
],
"NumeroCivico": [
"1"
],
"CAP": [
"846"
],
"Comune": [
"LAME"
],
"Provincia": [
"CZ"
],
"Nazione": [
"IT"
]
}
],
"Contatti": [
{
"Telefono": [
"+39 09"
],
"Fax": [
"+39 09"
],
"Email": [
"XXXX#hotmail.it"
]
}
]
}
],
"CessionarioCommittente": [
{
"DatiAnagrafici": [
{
"IdFiscaleIVA": [
{
"IdPaese": [
"IT"
],
"IdCodice": [
"03084390792"
]
}
],
"CodiceFiscale": [
"AAAAAAAA"
],
"Anagrafica": [
{
"Nome": [
"R"
],
"Cognome": [
"S"
]
}
]
}
],
"Sede": [
{
"Indirizzo": [
"VIA TIMAVO "
],
"CAP": [
"88"
],
"Comune": [
"LAMEZ"
],
"Provincia": [
"CZ"
],
"Nazione": [
"IT"
]
}
]
}
],
"TerzoIntermediarioOSoggettoEmittente": [
{
"DatiAnagrafici": [
{
"IdFiscaleIVA": [
{
"IdPaese": [
"IT"
],
"IdCodice": [
"01895030995"
]
}
],
"Anagrafica": [
{
"Denominazione": [
"CompEd "
]
}
]
}
]
}
],
"SoggettoEmittente": [
"TZ"
]
}
],
"FatturaElettronicaBody": [
{
"DatiGenerali": [
{
"DatiGeneraliDocumento": [
{
"TipoDocumento": [
"TD01"
],
"Divisa": [
"EUR"
],
"Data": [
"2019-12-21"
],
"Numero": [
"1777"
],
"ImportoTotaleDocumento": [
"30.71"
],
"Causale": [
"Fattura Vendita Accompagnatoria"
],
"Art73": [
"SI"
]
}
],
"DatiTrasporto": [
{
"CausaleTrasporto": [
"VENDITA"
],
"NumeroColli": [
"2"
],
"Descrizione": [
"A VISTA"
],
"DataInizioTrasporto": [
"2019-12-21"
]
}
]
}
],
"DatiBeniServizi": [
{
"DettaglioLinee": [
{
"NumeroLinea": [
"1"
],
"CodiceArticolo": [
{
"CodiceTipo": [
"INTERNO"
],
"CodiceValore": [
"8013170502498"
]
}
],
"Descrizione": [
"PNP BUSTE TRASP. 40X60 PZ 100"
],
"Quantita": [
"1.00000000"
],
"UnitaMisura": [
"PC"
],
"PrezzoUnitario": [
"9.83610000"
],
"PrezzoTotale": [
"9.84"
],
"AliquotaIVA": [
"22.00"
]
},
{
"NumeroLinea": [
"2"
],
"CodiceArticolo": [
{
"CodiceTipo": [
"INTERNO"
],
"CodiceValore": [
"15017"
]
}
],
"Descrizione": [
"TOVAGLIETTE CARTAPAGLIA 30x40 500 PZ"
],
"Quantita": [
"1.00000000"
],
"UnitaMisura": [
"PZ"
],
"PrezzoUnitario": [
"6.96720000"
],
"PrezzoTotale": [
"6.97"
],
"AliquotaIVA": [
"22.00"
]
},
{
"NumeroLinea": [
"3"
],
"CodiceArticolo": [
{
"CodiceTipo": [
"INTERNO"
],
"CodiceValore": [
"8014032035314"
]
}
],
"Descrizione": [
"CAPRICE ORO TONDI DIAM.30 PZ 2"
],
"Quantita": [
"4.00000000"
],
"UnitaMisura": [
"PZ"
],
"PrezzoUnitario": [
"1.06560000"
],
"PrezzoTotale": [
"4.26"
],
"AliquotaIVA": [
"22.00"
]
},
{
"NumeroLinea": [
"4"
],
"CodiceArticolo": [
{
"CodiceTipo": [
"INTERNO"
],
"CodiceValore": [
"8055329171339"
]
}
],
"Descrizione": [
"BASTONCINI LEGNO GOLDEN HILL"
],
"Quantita": [
"5.00000000"
],
"UnitaMisura": [
"PZ"
],
"PrezzoUnitario": [
"0.81970000"
],
"PrezzoTotale": [
"4.10"
],
"AliquotaIVA": [
"22.00"
]
}
],
"DatiRiepilogo": [
{
"AliquotaIVA": [
"22.00"
],
"ImponibileImporto": [
"25.17"
],
"Imposta": [
"5.54"
],
"EsigibilitaIVA": [
"I"
]
}
]
}
],
"DatiPagamento": [
{
"CondizioniPagamento": [
"TP02"
],
"DettaglioPagamento": [
{
"ModalitaPagamento": [
"MP01"
],
"DataRiferimentoTerminiPagamento": [
"2019-12-21"
],
"GiorniTerminiPagamento": [
"0"
],
"DataScadenzaPagamento": [
"2019-12-21"
],
"ImportoPagamento": [
"30.71"
]
}
]
}
]
}
]
}
}
My desired result is to extract the values in the child elements and put them in a table to display in the browser.
For starting, let's say I would like to put the values in an array, for example:
['IT','01895030995']
I really confuse how to get the values from the more internal child of this json. Maybe I should convert different to do so, because previously it was an XMLFile that I converted like this:
convert.parseString(xmlFile, (err, result) => {
if(err) {
throw err;
}
// `result` is a JavaScript object
// convert it to a JSON string
const json = JSON.stringify(result, null, 4);
I tried with forEach method, for loop and Object.keys/values trying to get at least a first result. In all the cases didn't work, it will console.log or undefined for thousands times or some numbers which I don't understand how....
Hoping someone could help.... thanks in advance!
Presented below is one possible way to achieve the desired objective.
Code Snippet
// method to collect string values
const collectAllValues = (arr, res = []) => {
// append result "res" with any "string" elt in array "arr"
const r = [
...res,
...(
arr?.filter(
elt => typeof elt !== 'object'
) ?? []
)
];
// filter out "arr" elements that are "objects"
const nx = arr?.filter(
elt => typeof elt === 'object'
) ?? [];
// if "objects" exist in "arr" elements-list
// use ".reduce()" to iterate over the filtered "arr"
// then, for each object, use ".reduce()" to
// iterate over values that are arrays and make
// recursive call to this method & return the value
if (nx && nx.length) {
return (
nx.reduce(
(acc, ob) => ([
...acc,
Object.values(ob)
.filter(v => v && Array.isArray(v))
.reduce((ac2, ar) => ([...ac2, ...collectAllValues(ar)]), acc)
.flat()
]),
r
)
);
} else return r;
// if no "objects" exist, simply return earlier
// result "r"
};
const xmlFile = {
"p:FatturaElettronica": {
"$": {
"xmlns:p": "http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2",
"xmlns:ds": "http://www.w3.org/2000/09/xmldsig#",
"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"versione": "FPR12",
"xsi:schemaLocation": "http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2 fatturaordinaria_v1.2.xsd"
},
"FatturaElettronicaHeader": [{
"DatiTrasmissione": [{
"IdTrasmittente": [{
"IdPaese": [
"IT"
],
"IdCodice": [
"01895030995"
]
}],
"ProgressivoInvio": [
"0HVHG"
],
"FormatoTrasmissione": [
"FPR12"
],
"CodiceDestinatario": [
"J6URRTW"
]
}],
"CedentePrestatore": [{
"DatiAnagrafici": [{
"IdFiscaleIVA": [{
"IdPaese": [
"IT"
],
"IdCodice": [
"12345678127"
]
}],
"CodiceFiscale": [
"1127"
],
"Anagrafica": [{
"Denominazione": [
"GE."
]
}],
"RegimeFiscale": [
"RF01"
]
}],
"Sede": [{
"Indirizzo": [
"Via T. Fusco"
],
"NumeroCivico": [
"1"
],
"CAP": [
"846"
],
"Comune": [
"LAME"
],
"Provincia": [
"CZ"
],
"Nazione": [
"IT"
]
}],
"Contatti": [{
"Telefono": [
"+39 09"
],
"Fax": [
"+39 09"
],
"Email": [
"XXXX#hotmail.it"
]
}]
}],
"CessionarioCommittente": [{
"DatiAnagrafici": [{
"IdFiscaleIVA": [{
"IdPaese": [
"IT"
],
"IdCodice": [
"03084390792"
]
}],
"CodiceFiscale": [
"AAAAAAAA"
],
"Anagrafica": [{
"Nome": [
"R"
],
"Cognome": [
"S"
]
}]
}],
"Sede": [{
"Indirizzo": [
"VIA TIMAVO "
],
"CAP": [
"88"
],
"Comune": [
"LAMEZ"
],
"Provincia": [
"CZ"
],
"Nazione": [
"IT"
]
}]
}],
"TerzoIntermediarioOSoggettoEmittente": [{
"DatiAnagrafici": [{
"IdFiscaleIVA": [{
"IdPaese": [
"IT"
],
"IdCodice": [
"01895030995"
]
}],
"Anagrafica": [{
"Denominazione": [
"CompEd "
]
}]
}]
}],
"SoggettoEmittente": [
"TZ"
]
}],
"FatturaElettronicaBody": [{
"DatiGenerali": [{
"DatiGeneraliDocumento": [{
"TipoDocumento": [
"TD01"
],
"Divisa": [
"EUR"
],
"Data": [
"2019-12-21"
],
"Numero": [
"1777"
],
"ImportoTotaleDocumento": [
"30.71"
],
"Causale": [
"Fattura Vendita Accompagnatoria"
],
"Art73": [
"SI"
]
}],
"DatiTrasporto": [{
"CausaleTrasporto": [
"VENDITA"
],
"NumeroColli": [
"2"
],
"Descrizione": [
"A VISTA"
],
"DataInizioTrasporto": [
"2019-12-21"
]
}]
}],
"DatiBeniServizi": [{
"DettaglioLinee": [{
"NumeroLinea": [
"1"
],
"CodiceArticolo": [{
"CodiceTipo": [
"INTERNO"
],
"CodiceValore": [
"8013170502498"
]
}],
"Descrizione": [
"PNP BUSTE TRASP. 40X60 PZ 100"
],
"Quantita": [
"1.00000000"
],
"UnitaMisura": [
"PC"
],
"PrezzoUnitario": [
"9.83610000"
],
"PrezzoTotale": [
"9.84"
],
"AliquotaIVA": [
"22.00"
]
},
{
"NumeroLinea": [
"2"
],
"CodiceArticolo": [{
"CodiceTipo": [
"INTERNO"
],
"CodiceValore": [
"15017"
]
}],
"Descrizione": [
"TOVAGLIETTE CARTAPAGLIA 30x40 500 PZ"
],
"Quantita": [
"1.00000000"
],
"UnitaMisura": [
"PZ"
],
"PrezzoUnitario": [
"6.96720000"
],
"PrezzoTotale": [
"6.97"
],
"AliquotaIVA": [
"22.00"
]
},
{
"NumeroLinea": [
"3"
],
"CodiceArticolo": [{
"CodiceTipo": [
"INTERNO"
],
"CodiceValore": [
"8014032035314"
]
}],
"Descrizione": [
"CAPRICE ORO TONDI DIAM.30 PZ 2"
],
"Quantita": [
"4.00000000"
],
"UnitaMisura": [
"PZ"
],
"PrezzoUnitario": [
"1.06560000"
],
"PrezzoTotale": [
"4.26"
],
"AliquotaIVA": [
"22.00"
]
},
{
"NumeroLinea": [
"4"
],
"CodiceArticolo": [{
"CodiceTipo": [
"INTERNO"
],
"CodiceValore": [
"8055329171339"
]
}],
"Descrizione": [
"BASTONCINI LEGNO GOLDEN HILL"
],
"Quantita": [
"5.00000000"
],
"UnitaMisura": [
"PZ"
],
"PrezzoUnitario": [
"0.81970000"
],
"PrezzoTotale": [
"4.10"
],
"AliquotaIVA": [
"22.00"
]
}
],
"DatiRiepilogo": [{
"AliquotaIVA": [
"22.00"
],
"ImponibileImporto": [
"25.17"
],
"Imposta": [
"5.54"
],
"EsigibilitaIVA": [
"I"
]
}]
}],
"DatiPagamento": [{
"CondizioniPagamento": [
"TP02"
],
"DettaglioPagamento": [{
"ModalitaPagamento": [
"MP01"
],
"DataRiferimentoTerminiPagamento": [
"2019-12-21"
],
"GiorniTerminiPagamento": [
"0"
],
"DataScadenzaPagamento": [
"2019-12-21"
],
"ImportoPagamento": [
"30.71"
]
}]
}]
}]
}
};
console.log(
'get sample 2 items only:\n[', xmlFile["p:FatturaElettronica"].FatturaElettronicaHeader[0].DatiTrasmissione[0].IdTrasmittente[0].IdPaese[0], ', ', xmlFile["p:FatturaElettronica"].FatturaElettronicaHeader[0].DatiTrasmissione[0].IdTrasmittente[0].IdCodice[0], '];');
console.log(
'collect all values:\n',
Object.values(xmlFile["p:FatturaElettronica"])
.filter(v => v && Array.isArray(v))
.reduce((acc, ar) => ([...acc, ...collectAllValues(ar)]), [])
.flat()
);
.as-console-wrapper { max-height: 100% !important; top: 0 }
Explanation
Inline comments added to the snippet above.

TopologyException when using turf.intersect

I'm getting the following error when using turf.intersect with a specific polygon:
"TopologyException" "found non-noded intersection between LINESTRING ( -0.140733 51.505327, -0.141097 51.505689 ) and LINESTRING ( -0.141136 51.505674, -0.141068 51.5057 )"
Here is the polygon with the point that's causing the issue:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-0.144414,
51.5057787
],
[
-0.1444,
51.5057669
],
[
-0.1444174,
51.5057565
],
[
-0.1444375,
51.5057437
],
[
-0.1444486,
51.5057591
],
[
-0.1448395,
51.5055465
],
[
-0.1456195,
51.5051519
],
[
-0.1461211,
51.5048921
],
[
-0.1467341,
51.5045993
],
[
-0.1472694,
51.5042539
],
[
-0.147728,
51.5039184
],
[
-0.1485316,
51.5035408
],
[
-0.1485702,
51.5035234
],
[
-0.1488964,
51.503376
],
[
-0.1489081,
51.5033859
],
[
-0.1491678,
51.503267
],
[
-0.1491573,
51.5032582
],
[
-0.1494585,
51.5031342
],
[
-0.1495505,
51.5031388
],
[
-0.1495754,
51.503085
],
[
-0.1496124,
51.5026853
],
[
-0.1495618,
51.5026316
],
[
-0.1495053,
51.5025839
],
[
-0.1490356,
51.5025596
],
[
-0.1480816,
51.5025194
],
[
-0.1464951,
51.5024587
],
[
-0.1453418,
51.5023882
],
[
-0.1444299,
51.5023513
],
[
-0.1424233,
51.502272
],
[
-0.1420358,
51.5022744
],
[
-0.1416246,
51.5022811
],
[
-0.1414344,
51.5022758
],
[
-0.14113,
51.5023094
],
[
-0.1409441,
51.50233
],
[
-0.1412107,
51.5026049
],
[
-0.1407386,
51.5026799
],
[
-0.1402814,
51.5026536
],
[
-0.1398146,
51.5025228
],
[
-0.1396726,
51.5024475
],
[
-0.1395941,
51.5023923
],
[
-0.1394956,
51.5024312
],
[
-0.1393765,
51.5024829
],
[
-0.1388305,
51.502698
],
[
-0.1387333,
51.5027403
],
[
-0.1387835,
51.5027906
],
[
-0.1392711,
51.5032406
],
[
-0.139465,
51.503466
],
[
-0.1399479,
51.5039078
],
[
-0.1403315,
51.504302
],
[
-0.1407059,
51.5046814
],
[
-0.1402579,
51.5048283
],
[
-0.1407332,
51.5053265
],
[
-0.1410967,
51.5056887
],
[
-0.1410841,
51.5056711
],
[
-0.1411158,
51.5056574
],
[
-0.1411356,
51.5056744
],
[
-0.141068,
51.5057001
],
[
-0.1411549,
51.5058139
],
[
-0.1412316,
51.5057886
],
[
-0.1412437,
51.5058054
],
[
-0.1411693,
51.5058326
],
[
-0.1411811,
51.5058428
],
[
-0.1412021,
51.5058461
],
[
-0.1412326,
51.5058618
],
[
-0.1412584,
51.5058575
],
[
-0.1413349,
51.5059475
],
[
-0.1413677,
51.5059833
],
[
-0.1413704,
51.5060066
],
[
-0.1413538,
51.506016
],
[
-0.1413494,
51.5060451
],
[
-0.1414301,
51.5061301
],
[
-0.1414221,
51.5061562
],
[
-0.1414889,
51.5062259
],
[
-0.1415374,
51.5062089
],
[
-0.1415669,
51.506238
],
[
-0.1415172,
51.5062553
],
[
-0.141583,
51.506326
],
[
-0.1416228,
51.5063353
],
[
-0.1416919,
51.5064173
],
[
-0.1416845,
51.5064298
],
[
-0.1416341,
51.5064488
],
[
-0.1416636,
51.5064833
],
[
-0.1416771,
51.5064797
],
[
-0.1416892,
51.5064881
],
[
-0.1418973,
51.5064092
],
[
-0.1419091,
51.5064204
],
[
-0.141691,
51.5065052
],
[
-0.1417355,
51.5065528
],
[
-0.1417624,
51.5065417
],
[
-0.1417862,
51.506569
],
[
-0.141812,
51.5065727
],
[
-0.141838,
51.5065954
],
[
-0.1418271,
51.5066109
],
[
-0.1418623,
51.5066435
],
[
-0.1418837,
51.5066417
],
[
-0.1419093,
51.5066617
],
[
-0.1419035,
51.5066764
],
[
-0.1419137,
51.5066845
],
[
-0.1419323,
51.506686
],
[
-0.1419469,
51.5067007
],
[
-0.1419413,
51.5067104
],
[
-0.1419583,
51.5067201
],
[
-0.1421823,
51.5066524
],
[
-0.1422288,
51.5066342
],
[
-0.1423373,
51.5067681
],
[
-0.1430385,
51.5064228
],
[
-0.143099,
51.5064704
],
[
-0.1439141,
51.5060467
],
[
-0.144414,
51.5057787
]
]
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [
-0.14109693591549397,
51.5056889362676
]
}
}
]
}
I've tried using cleanCoords and checking for intersections with kinks, but it didn't work.
Here's how I managed to make it work:
var options = {tolerance: 0.0001, highQuality: true};
poly = turf.cleanCoords(turf.simplify(poly, options));
I also checked for kinks, and divided the polygon if it returned true:
var kinks = turf.kinks(poly);
if (kinks.features.length){
polys = turf.unkinkPolygon(poly);
...
Based on the answer to this question
What does the following error mean: TopologyException: found non-nonded intersection between LINESTRING
I have found this solotion to be feasible, as well:
let myWorkingPolygon = turf.buffer(errorThrowingPolygon, 0, {units:"meters"});
Buffer calculates a buffer around the given feature (but here with buffer size 0), find more info here: https://turfjs.org/docs/#buffer

Google Map - Get location of marker

I'm having 2 polygon { "Country": "01", "Location": "09", "City": "156" } and { "Country": "01", "Location": "09", "City": "160" }
I created a centerMarker fixed center the map. When user drag or change center the map. I will get position of centerMarker.
google.maps.event.addListener(map,'idle', function() {
console.log(map.getCenter().lat());
console.log(map.getCenter().lng());
});
$('<div/>').addClass('centerMarker').appendTo(map.getDiv())
How can I check centerMarker located on "City": "160" or "City": "156"?
My full code
function initMap() {
var x = new google.maps.LatLng(11.200845,106.5746263,17);
var data =
{
"type":"FeatureCollection",
"name":"gadm36_VNM_3",
"crs":{
"type":"name",
"properties":{
"name":"urn:ogc:def:crs:OGC:1.3:CRS84"
}
},
"features":[
{
"type":"Feature",
"properties":{
"Country":"01",
"Location":"09",
"City":"156"
},
"geometry":{
"type":"MultiPolygon",
"coordinates":[
[
[
[
106.773910522460994,
11.140744209289601
],
[
106.764717102050994,
11.140742301941
],
[
106.764862060547003,
11.1326341629028
],
[
106.763977050780994,
11.128930091857899
],
[
106.762413024902003,
11.1263284683228
],
[
106.761451721190994,
11.126066207885801
],
[
106.760833740234006,
11.1245851516725
],
[
106.75707244873,
11.122723579406699
],
[
106.750144958495994,
11.1217250823975
],
[
106.747901916504006,
11.122282028198301
],
[
106.748207092285,
11.1209669113159
],
[
106.746643066405994,
11.1184997558594
],
[
106.746650695800994,
11.1169691085815
],
[
106.743644714355995,
11.1138353347779
],
[
106.742393493652003,
11.1109828948975
],
[
106.737831115722997,
11.108716964721699
],
[
106.737289428710994,
11.1100120544434
],
[
106.737907409667997,
11.113375663757299
],
[
106.73681640625,
11.114133834839
],
[
106.737213134765994,
11.114882469177299
],
[
106.735412597655994,
11.116992950439499
],
[
106.735473632812997,
11.1183891296387
],
[
106.737617492675994,
11.119469642639199
],
[
106.737586975097997,
11.1202993392944
],
[
106.735694885254006,
11.120556831359901
],
[
106.736740112305,
11.1254129409791
],
[
106.735290527344006,
11.1279869079591
],
[
106.73600769043,
11.132244110107401
],
[
106.735633850097997,
11.1326360702516
],
[
106.733108520507997,
11.131629943847701
],
[
106.732345581055,
11.1364440917969
],
[
106.72779083252,
11.136043548583901
],
[
106.727645874024006,
11.138410568237299
],
[
106.724494934082003,
11.136935234069901
],
[
106.718818664550994,
11.142228126526
],
[
106.716232299805,
11.1407585144044
],
[
106.713203430175994,
11.1368913650513
],
[
106.71263885498,
11.1488819122315
],
[
106.713729858399006,
11.1516876220704
],
[
106.725151062012003,
11.180347442626999
],
[
106.734558105469006,
11.1816873550416
],
[
106.736030578612997,
11.180858612060501
],
[
106.755981445312997,
11.1822462081909
],
[
106.761962890625,
11.1883802413941
],
[
106.762916564942003,
11.1905174255372
],
[
106.764389038085994,
11.189139366149799
],
[
106.76416015625,
11.188378334045501
],
[
106.790100097655994,
11.1885118484497
],
[
106.792953491210994,
11.192991256713899
],
[
106.794372558594006,
11.1930904388429
],
[
106.794372558594006,
11.193788528442299
],
[
106.796928405762003,
11.1949625015259
],
[
106.798072814940994,
11.194272994995099
],
[
106.803909301757997,
11.1985483169556
],
[
106.805290222167997,
11.198659896850501
],
[
106.811401367187003,
11.202071189880501
],
[
106.813606262207003,
11.205387115478599
],
[
106.818145751952997,
11.2080841064453
],
[
106.819854736327997,
11.2152719497682
],
[
106.823463439940994,
11.2161722183228
],
[
106.824119567870994,
11.213012695312401
],
[
106.828521728515994,
11.209532737731999
],
[
106.831550598145,
11.209297180175801
],
[
106.835960388184006,
11.2118206024169
],
[
106.835075378417997,
11.2105274200441
],
[
106.837257385254006,
11.2098264694213
],
[
106.83716583252,
11.2090320587159
],
[
106.836235046387003,
11.2091836929322
],
[
106.837463378905994,
11.207406044006399
],
[
106.836402893065994,
11.2071533203125
],
[
106.835906982422003,
11.2056436538696
],
[
106.836585998535,
11.204249382019199
],
[
106.837699890137003,
11.2045288085938
],
[
106.836631774902003,
11.2030754089356
],
[
106.837547302245994,
11.2036037445068
],
[
106.838134765625,
11.201634407043599
],
[
106.837783813477003,
11.199815750122101
],
[
106.836570739745994,
11.1994781494141
],
[
106.83723449707,
11.1985836029053
],
[
106.835731506347997,
11.197198867798001
],
[
106.835334777832003,
11.1953115463257
],
[
106.831237792969006,
11.193099975586
],
[
106.824752807617003,
11.196343421936101
],
[
106.821243286132997,
11.195686340331999
],
[
106.818969726562997,
11.19251537323
],
[
106.81795501709,
11.192292213439901
],
[
106.817756652832003,
11.190671920776399
],
[
106.81111907959,
11.1809148788453
],
[
106.811645507812003,
11.178315162658601
],
[
106.809783935547003,
11.1756134033203
],
[
106.809677124022997,
11.174137115478599
],
[
106.807357788085994,
11.1731281280518
],
[
106.805305480957003,
11.169690132141101
],
[
106.800598144530994,
11.1647386550903
],
[
106.791679382324006,
11.1622724533082
],
[
106.787879943847997,
11.163525581359799
],
[
106.778388977050994,
11.1562852859497
],
[
106.773651123047003,
11.155713081359799
],
[
106.773910522460994,
11.140744209289601
]
]
]
]
}
},
{
"type":"Feature",
"properties":{
"Country":"01",
"Location":"09",
"City":"160"
},
"geometry":{
"type":"MultiPolygon",
"coordinates":[
[
[
[
106.908004760742003,
11.129010200500399
],
[
106.903732299805,
11.125083923339799
],
[
106.887130737305,
11.1172142028809
],
[
106.882217407227003,
11.1076774597168
],
[
106.880096435547003,
11.101813316345201
],
[
106.87996673584,
11.0955753326416
],
[
106.876556396484006,
11.095563888549901
],
[
106.876106262207003,
11.093448638916099
],
[
106.87442779541,
11.0930519104004
],
[
106.870147705077997,
11.093951225280801
],
[
106.866645812987997,
11.093237876892101
],
[
106.860298156737997,
11.0947256088256
],
[
106.858062744140994,
11.0945234298707
],
[
106.856292724609006,
11.0934009552003
],
[
106.853942871094006,
11.0938158035279
],
[
106.850006103515994,
11.092667579650801
],
[
106.848297119140994,
11.0912990570068
],
[
106.845809936522997,
11.091200828552299
],
[
106.842636108399006,
11.0886402130128
],
[
106.839897155762003,
11.0885286331178
],
[
106.836471557617003,
11.086883544921999
],
[
106.833183288574006,
11.087131500244199
],
[
106.828132629395,
11.088861465454199
],
[
106.825546264647997,
11.0915222167969
],
[
106.823066711425994,
11.0958919525146
],
[
106.823875427245994,
11.1021842956544
],
[
106.822807312012003,
11.107276916504
],
[
106.822982788085994,
11.1099071502687
],
[
106.821441650390994,
11.1121578216553
],
[
106.821563720702997,
11.1142129898072
],
[
106.829162597655994,
11.1119136810303
],
[
106.856956481934006,
11.1134729385375
],
[
106.856575012207003,
11.116441726684601
],
[
106.858283996582003,
11.1262016296387
],
[
106.864997863769005,
11.1320905685425
],
[
106.871109008789006,
11.1428709030153
],
[
106.878143310547003,
11.143126487731999
],
[
106.880805969237997,
11.144726753234901
],
[
106.886619567870994,
11.1450748443604
],
[
106.889801025390994,
11.1470909118653
],
[
106.892097473145,
11.1469631195068
],
[
106.896003723145,
11.145208358764799
],
[
106.89820098877,
11.143099784851101
],
[
106.898323059082003,
11.1416568756104
],
[
106.900520324707003,
11.138403892516999
],
[
106.900169372559006,
11.1379747390747
],
[
106.901206970215,
11.137484550476101
],
[
106.900978088379006,
11.136289596557599
],
[
106.902084350585994,
11.135934829711999
],
[
106.902717590332003,
11.1330337524415
],
[
106.904357910155994,
11.1320133209229
],
[
106.905548095702997,
11.129722595214799
],
[
106.907104492187997,
11.1286296844484
],
[
106.908004760742003,
11.129010200500399
]
]
]
]
}
}
]
};
var map = new google.maps.Map(document.getElementById("map"),{
center: x,
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
google.maps.event.addListener(map,'idle', function() {
console.log(map.getCenter().lat());
console.log(map.getCenter().lng());
});
$('<div/>').addClass('centerMarker').appendTo(map.getDiv())
map.data.addGeoJson(data);
}
In order to figure out if the center of your map is located inside polygon of the city you can use a geometry library of Maps JavaScript API. The geometry library provides method containsLocation(point, polygon) which may be useful for you. Have a look at the documentation
https://developers.google.com/maps/documentation/javascript/reference/geometry#poly
First you have to include geometry library in the script tag that loads Maps JavaScript API adding the &libraries=geometry in the URL of Maps JavaScript API. Second, you create polygons of cities using the GeoJSON objects of data layer, so you have to convert multipolygons of data layer to google.maps.Polygons objects in order to use aforementioned method containsLocation(point, polygon).
I recommend investigating documentation to see how you can convert these objects
https://developers.google.com/maps/documentation/javascript/reference/data#Data.MultiPolygon
Here is a sample code that makes a conversion of Multipolygons and uses containsLocation(point, polygon) to check if map center is inside a city polygon
var map;
function initMap() {
var x = new google.maps.LatLng(11.200845,106.5746263,17);
var data =
{
"type":"FeatureCollection",
"name":"gadm36_VNM_3",
"crs":{
"type":"name",
"properties":{
"name":"urn:ogc:def:crs:OGC:1.3:CRS84"
}
},
"features":[
{
"type":"Feature",
"properties":{
"Country":"01",
"Location":"09",
"City":"156"
},
"geometry":{
"type":"MultiPolygon",
"coordinates":[
[
[
[
106.773910522460994,
11.140744209289601
],
[
106.764717102050994,
11.140742301941
],
[
106.764862060547003,
11.1326341629028
],
[
106.763977050780994,
11.128930091857899
],
[
106.762413024902003,
11.1263284683228
],
[
106.761451721190994,
11.126066207885801
],
[
106.760833740234006,
11.1245851516725
],
[
106.75707244873,
11.122723579406699
],
[
106.750144958495994,
11.1217250823975
],
[
106.747901916504006,
11.122282028198301
],
[
106.748207092285,
11.1209669113159
],
[
106.746643066405994,
11.1184997558594
],
[
106.746650695800994,
11.1169691085815
],
[
106.743644714355995,
11.1138353347779
],
[
106.742393493652003,
11.1109828948975
],
[
106.737831115722997,
11.108716964721699
],
[
106.737289428710994,
11.1100120544434
],
[
106.737907409667997,
11.113375663757299
],
[
106.73681640625,
11.114133834839
],
[
106.737213134765994,
11.114882469177299
],
[
106.735412597655994,
11.116992950439499
],
[
106.735473632812997,
11.1183891296387
],
[
106.737617492675994,
11.119469642639199
],
[
106.737586975097997,
11.1202993392944
],
[
106.735694885254006,
11.120556831359901
],
[
106.736740112305,
11.1254129409791
],
[
106.735290527344006,
11.1279869079591
],
[
106.73600769043,
11.132244110107401
],
[
106.735633850097997,
11.1326360702516
],
[
106.733108520507997,
11.131629943847701
],
[
106.732345581055,
11.1364440917969
],
[
106.72779083252,
11.136043548583901
],
[
106.727645874024006,
11.138410568237299
],
[
106.724494934082003,
11.136935234069901
],
[
106.718818664550994,
11.142228126526
],
[
106.716232299805,
11.1407585144044
],
[
106.713203430175994,
11.1368913650513
],
[
106.71263885498,
11.1488819122315
],
[
106.713729858399006,
11.1516876220704
],
[
106.725151062012003,
11.180347442626999
],
[
106.734558105469006,
11.1816873550416
],
[
106.736030578612997,
11.180858612060501
],
[
106.755981445312997,
11.1822462081909
],
[
106.761962890625,
11.1883802413941
],
[
106.762916564942003,
11.1905174255372
],
[
106.764389038085994,
11.189139366149799
],
[
106.76416015625,
11.188378334045501
],
[
106.790100097655994,
11.1885118484497
],
[
106.792953491210994,
11.192991256713899
],
[
106.794372558594006,
11.1930904388429
],
[
106.794372558594006,
11.193788528442299
],
[
106.796928405762003,
11.1949625015259
],
[
106.798072814940994,
11.194272994995099
],
[
106.803909301757997,
11.1985483169556
],
[
106.805290222167997,
11.198659896850501
],
[
106.811401367187003,
11.202071189880501
],
[
106.813606262207003,
11.205387115478599
],
[
106.818145751952997,
11.2080841064453
],
[
106.819854736327997,
11.2152719497682
],
[
106.823463439940994,
11.2161722183228
],
[
106.824119567870994,
11.213012695312401
],
[
106.828521728515994,
11.209532737731999
],
[
106.831550598145,
11.209297180175801
],
[
106.835960388184006,
11.2118206024169
],
[
106.835075378417997,
11.2105274200441
],
[
106.837257385254006,
11.2098264694213
],
[
106.83716583252,
11.2090320587159
],
[
106.836235046387003,
11.2091836929322
],
[
106.837463378905994,
11.207406044006399
],
[
106.836402893065994,
11.2071533203125
],
[
106.835906982422003,
11.2056436538696
],
[
106.836585998535,
11.204249382019199
],
[
106.837699890137003,
11.2045288085938
],
[
106.836631774902003,
11.2030754089356
],
[
106.837547302245994,
11.2036037445068
],
[
106.838134765625,
11.201634407043599
],
[
106.837783813477003,
11.199815750122101
],
[
106.836570739745994,
11.1994781494141
],
[
106.83723449707,
11.1985836029053
],
[
106.835731506347997,
11.197198867798001
],
[
106.835334777832003,
11.1953115463257
],
[
106.831237792969006,
11.193099975586
],
[
106.824752807617003,
11.196343421936101
],
[
106.821243286132997,
11.195686340331999
],
[
106.818969726562997,
11.19251537323
],
[
106.81795501709,
11.192292213439901
],
[
106.817756652832003,
11.190671920776399
],
[
106.81111907959,
11.1809148788453
],
[
106.811645507812003,
11.178315162658601
],
[
106.809783935547003,
11.1756134033203
],
[
106.809677124022997,
11.174137115478599
],
[
106.807357788085994,
11.1731281280518
],
[
106.805305480957003,
11.169690132141101
],
[
106.800598144530994,
11.1647386550903
],
[
106.791679382324006,
11.1622724533082
],
[
106.787879943847997,
11.163525581359799
],
[
106.778388977050994,
11.1562852859497
],
[
106.773651123047003,
11.155713081359799
],
[
106.773910522460994,
11.140744209289601
]
]
]
]
}
},
{
"type":"Feature",
"properties":{
"Country":"01",
"Location":"09",
"City":"160"
},
"geometry":{
"type":"MultiPolygon",
"coordinates":[
[
[
[
106.908004760742003,
11.129010200500399
],
[
106.903732299805,
11.125083923339799
],
[
106.887130737305,
11.1172142028809
],
[
106.882217407227003,
11.1076774597168
],
[
106.880096435547003,
11.101813316345201
],
[
106.87996673584,
11.0955753326416
],
[
106.876556396484006,
11.095563888549901
],
[
106.876106262207003,
11.093448638916099
],
[
106.87442779541,
11.0930519104004
],
[
106.870147705077997,
11.093951225280801
],
[
106.866645812987997,
11.093237876892101
],
[
106.860298156737997,
11.0947256088256
],
[
106.858062744140994,
11.0945234298707
],
[
106.856292724609006,
11.0934009552003
],
[
106.853942871094006,
11.0938158035279
],
[
106.850006103515994,
11.092667579650801
],
[
106.848297119140994,
11.0912990570068
],
[
106.845809936522997,
11.091200828552299
],
[
106.842636108399006,
11.0886402130128
],
[
106.839897155762003,
11.0885286331178
],
[
106.836471557617003,
11.086883544921999
],
[
106.833183288574006,
11.087131500244199
],
[
106.828132629395,
11.088861465454199
],
[
106.825546264647997,
11.0915222167969
],
[
106.823066711425994,
11.0958919525146
],
[
106.823875427245994,
11.1021842956544
],
[
106.822807312012003,
11.107276916504
],
[
106.822982788085994,
11.1099071502687
],
[
106.821441650390994,
11.1121578216553
],
[
106.821563720702997,
11.1142129898072
],
[
106.829162597655994,
11.1119136810303
],
[
106.856956481934006,
11.1134729385375
],
[
106.856575012207003,
11.116441726684601
],
[
106.858283996582003,
11.1262016296387
],
[
106.864997863769005,
11.1320905685425
],
[
106.871109008789006,
11.1428709030153
],
[
106.878143310547003,
11.143126487731999
],
[
106.880805969237997,
11.144726753234901
],
[
106.886619567870994,
11.1450748443604
],
[
106.889801025390994,
11.1470909118653
],
[
106.892097473145,
11.1469631195068
],
[
106.896003723145,
11.145208358764799
],
[
106.89820098877,
11.143099784851101
],
[
106.898323059082003,
11.1416568756104
],
[
106.900520324707003,
11.138403892516999
],
[
106.900169372559006,
11.1379747390747
],
[
106.901206970215,
11.137484550476101
],
[
106.900978088379006,
11.136289596557599
],
[
106.902084350585994,
11.135934829711999
],
[
106.902717590332003,
11.1330337524415
],
[
106.904357910155994,
11.1320133209229
],
[
106.905548095702997,
11.129722595214799
],
[
106.907104492187997,
11.1286296844484
],
[
106.908004760742003,
11.129010200500399
]
]
]
]
}
}
]
};
map = new google.maps.Map(document.getElementById("map"),{
center: x,
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var centerMarker = new google.maps.Marker({
position: x,
map: map
});
var features = map.data.addGeoJson(data);
var mapPolies = new Map();
features.forEach(function(feature) {
var city = feature.getProperty("City");
var geom = feature.getGeometry();
var polygons = geom.getArray();
var polys = [];
polygons.forEach(function(polygon) {
var paths = [];
var linearRings = polygon.getArray();
linearRings.forEach(function(linearRing){
var coords = linearRing.getArray();
paths.push(coords);
});
var poly = new google.maps.Polygon({
paths: paths,
map: null,
visible: false
});
polys.push(poly);
});
mapPolies.set(city, polys);
});
google.maps.event.addListener(map,'idle', function() {
//console.log(map.getCenter().lat());
//console.log(map.getCenter().lng());
centerMarker.setPosition(map.getCenter());
for (var entry of mapPolies.entries()) {
entry[1].forEach(function(polygon) {
var inside = google.maps.geometry.poly.containsLocation(map.getCenter(), polygon);
if (inside) {
console.log(`Map center inside polygon of ${entry[0]} city`);
}
});
}
});
}
#map {
height: 100%;
}
html, body {
height: 100%;
margin: 0;
padding: 0;
}
<div id="map"></div>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDztlrk_3CnzGHo7CFvLFqE_2bUKEq1JEU&callback=initMap&libraries=geometry" async defer></script>
I hope this helps!

JSON parse array loop

I'm trying to parse my array into representing data like this
-1.80511,53.84632:-1.80511,53.84632:-1.80511,53.84632:xxx,xxx:xxx
lat,lng:lat,lng
so that it can be read by an api. here is the code I have so far, i can only get it into this format (coordsnew)
-1.7651,53.88566,-1.76896,53.8866,-1.77125,53.88801
My current code
var coordsnew = [];
var i;
for (i = 0; i < coords.length; i++) {
coordsnew += coords[i];
console.log(coordsnew);
My coords array data is currently formed like this
var statesdata = {
"type": "FeatureCollection",
"features": [
{ "type": "Feature", "properties": { "Name": "BD1 1", "Description": "BD1 1 postcode sector<br \/><a href=\"http:\/\/www.doogal.co.uk\/UKPostcodes.php?Search=BD1 1\" >Details<\/a>" }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ -1.7526003839732, 53.795297076485198 ], [ -1.75078461615826, 53.795607063948303 ], [ -1.75078061651426, 53.795604064354201 ], [ -1.75051864373599, 53.795500080054303 ], [ -1.74977069342604, 53.794539203056601 ], [ -1.74974469120328, 53.794412218350601 ], [ -1.7492607290865, 53.793926281950498 ], [ -1.74918471905401, 53.793471336517399 ], [ -1.74939968041321, 53.793170369115202 ], [ -1.74925067188821, 53.792542445011001 ], [ -1.74902869183461, 53.792380467011299 ], [ -1.74897763675983, 53.790929638498604 ], [ -1.74976552406105, 53.790511677309198 ], [ -1.75025545811429, 53.790349689915303 ], [ -1.75108238214196, 53.790913612609998 ], [ -1.75116539246416, 53.791395554773104 ], [ -1.75217230177679, 53.7921254555514 ], [ -1.75238127314532, 53.792044462329898 ], [ -1.75393711035648, 53.792635372231103 ], [ -1.75434805085846, 53.792400394475202 ], [ -1.75315716556359, 53.791713491060101 ], [ -1.7536130920749, 53.791275536607898 ], [ -1.75509590228284, 53.7910175474209 ], [ -1.75604581420275, 53.791646460844603 ], [ -1.75617780646011, 53.791840436265602 ], [ -1.75572287565107, 53.792179402357696 ], [ -1.75536192519302, 53.792321390401902 ], [ -1.75511998619384, 53.793075304829799 ], [ -1.75459505898236, 53.793299285383803 ], [ -1.75372115084327, 53.792977334818701 ], [ -1.75370116224194, 53.793190310007297 ], [ -1.75374816559962, 53.793404284194501 ], [ -1.75376716436202, 53.793429281000698 ], [ -1.75431110539553, 53.793587255224999 ], [ -1.75449608126177, 53.793544257847103 ], [ -1.75468606209324, 53.793633244863699 ], [ -1.75451310097882, 53.794060196877098 ], [ -1.75454309938432, 53.794108190830698 ], [ -1.75468508313808, 53.794129186485499 ], [ -1.75508202351291, 53.7938512139769 ], [ -1.75526600519453, 53.793943200719497 ], [ -1.75589992558159, 53.793869201069697 ], [ -1.75611888768616, 53.7935972302026 ], [ -1.75644384581814, 53.793534233333297 ], [ -1.75653285692984, 53.794052171178201 ], [ -1.75652885758116, 53.794056170760101 ], [ -1.75585494164085, 53.794121171996501 ], [ -1.75585194229808, 53.794128171212002 ], [ -1.75542100357864, 53.794348150996697 ], [ -1.75538800907876, 53.794384147193803 ], [ -1.75516103908185, 53.794446142888802 ], [ -1.7546371262232, 53.795013083049803 ], [ -1.75439915177408, 53.794938095018303 ], [ -1.75387121644832, 53.794961099275 ], [ -1.75351226756764, 53.795146082231298 ], [ -1.75297532691447, 53.795017104501397 ], [ -1.75291433127843, 53.794946113664501 ], [ -1.75265236634886, 53.795028107467203 ], [ -1.7526003839732, 53.795297076485198 ] ] ], [ [ [ -1.7524052922723, 53.792566400562002 ], [ -1.75210831392775, 53.7922304440348 ], [ -1.75197033209524, 53.792266441617201 ], [ -1.75092249848606, 53.793213343959003 ], [ -1.75151443343543, 53.793364318372703 ], [ -1.75168241131046, 53.793320321336203 ], [ -1.75193937541024, 53.7932043316018 ], [ -1.7524052922723, 53.792566400562002 ] ] ] ] } }
this is how the data is carried through and how it is represented in the console
var coords = layer.feature.geometry.coordinates;
0:Array(1)
0:Array(94)
0:Array(2) [-1.75937311731598, 53.808343451011]
0:-1.75937311731598
1:53.808343451011
To get the string you want:
var colonString = statesdata
.features[0]
.properties
.geometry
.coordinates[0][0]
.map(pair => pair.reverse().join())
.join(':');
That is some deeply nested geoJSON.

Turf.js inside() method won't return true

I'm trying to write a script that, using the inside method of Turf.js, that takes user-submitted coordinates and matches it to the corresponding Census tract. The code below, as a test, I believe should return true. It does not.
Those coordinates definitely are Wrigley Field. The tract polygon does envelope it. I linted the two bits of geoJson and they're clean (except for the right-hand rule).
const wrigleyField = [41.947783, -87.655889];
const wrigleyCensusTract = { "type": "Feature", "properties": { "STATEFP10": "17", "COUNTYFP10": "031", "TRACTCE10": "061100", "GEOID10": "17031061100", "NAME10": "611", "NAMELSAD10": "Census Tract 611", "MTFCC10": "G5020", "FUNCSTAT10": "S", "ALAND10": 173037, "AWATER10": 0, "INTPTLAT10": "+41.9491397", "INTPTLON10": "-087.6568035" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -87.656985, 41.951084 ], [ -87.656858, 41.951086 ], [ -87.656806, 41.951087 ], [ -87.656704, 41.951088 ], [ -87.656661, 41.951088 ], [ -87.656535, 41.951091 ], [ -87.656494, 41.951092 ], [ -87.656325, 41.951094 ], [ -87.656245, 41.951096 ], [ -87.655821, 41.951102 ], [ -87.655653, 41.951105 ], [ -87.655411, 41.951109 ], [ -87.655059, 41.951115 ], [ -87.654685, 41.95112 ], [ -87.654444, 41.951124 ], [ -87.654431, 41.950707 ], [ -87.654395, 41.949459 ], [ -87.654384, 41.949043 ], [ -87.654373, 41.948683 ], [ -87.654344, 41.947698 ], [ -87.654341, 41.947604 ], [ -87.65433, 41.947245 ], [ -87.654576, 41.94724 ], [ -87.654938, 41.947234 ], [ -87.655317, 41.947228 ], [ -87.655565, 41.947225 ], [ -87.655755, 41.947221 ], [ -87.656313, 41.947213 ], [ -87.656325, 41.947212 ], [ -87.656516, 41.947202 ], [ -87.656663, 41.947194 ], [ -87.656849, 41.947184 ], [ -87.657104, 41.94718 ], [ -87.657252, 41.947178 ], [ -87.657382, 41.947176 ], [ -87.657635, 41.947171 ], [ -87.657739, 41.94717 ], [ -87.658784, 41.947154 ], [ -87.659168, 41.947149 ], [ -87.659165, 41.947254 ], [ -87.659167, 41.94733 ], [ -87.659176, 41.947623 ], [ -87.659181, 41.947876 ], [ -87.659186, 41.948059 ], [ -87.65919, 41.94824 ], [ -87.659196, 41.948487 ], [ -87.659204, 41.948785 ], [ -87.65921, 41.948967 ], [ -87.659219, 41.949292 ], [ -87.659229, 41.949598 ], [ -87.659245, 41.950269 ], [ -87.659254, 41.950595 ], [ -87.659257, 41.950685 ], [ -87.659266, 41.950957 ], [ -87.659269, 41.951048 ], [ -87.658928, 41.951052 ], [ -87.657908, 41.951068 ], [ -87.657569, 41.951074 ], [ -87.657468, 41.951075 ], [ -87.657167, 41.951081 ], [ -87.657067, 41.951083 ], [ -87.657014, 41.951083 ], [ -87.656985, 41.951084 ] ] ] } }
const wrigleyPoint = { "type": "Feature", "properties": { "marker-color": "#0f0" }, "geometry": { "type": "Point", "coordinates": wrigleyField } };
console.log(turf.inside(wrigleyPoint, wrigleyCensusTract)); // false
What am I missing? Why does the above snippet not return true?
Thanks!
The issue is that your point coordinates are swapped. If you switch their places, then it will return true. I just tested this myself with your data and I did get true after swapping the point coordinates. Here is a snippet so you can test it yourself.
p1 = { "type": "Feature", "properties": { "STATEFP10": "17", "COUNTYFP10": "031", "TRACTCE10": "061100", "GEOID10": "17031061100", "NAME10": "611", "NAMELSAD10": "Census Tract 611", "MTFCC10": "G5020", "FUNCSTAT10": "S", "ALAND10": 173037, "AWATER10": 0, "INTPTLAT10": "+41.9491397", "INTPTLON10": "-087.6568035" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -87.656985, 41.951084 ], [ -87.656858, 41.951086 ], [ -87.656806, 41.951087 ], [ -87.656704, 41.951088 ], [ -87.656661, 41.951088 ], [ -87.656535, 41.951091 ], [ -87.656494, 41.951092 ], [ -87.656325, 41.951094 ], [ -87.656245, 41.951096 ], [ -87.655821, 41.951102 ], [ -87.655653, 41.951105 ], [ -87.655411, 41.951109 ], [ -87.655059, 41.951115 ], [ -87.654685, 41.95112 ], [ -87.654444, 41.951124 ], [ -87.654431, 41.950707 ], [ -87.654395, 41.949459 ], [ -87.654384, 41.949043 ], [ -87.654373, 41.948683 ], [ -87.654344, 41.947698 ], [ -87.654341, 41.947604 ], [ -87.65433, 41.947245 ], [ -87.654576, 41.94724 ], [ -87.654938, 41.947234 ], [ -87.655317, 41.947228 ], [ -87.655565, 41.947225 ], [ -87.655755, 41.947221 ], [ -87.656313, 41.947213 ], [ -87.656325, 41.947212 ], [ -87.656516, 41.947202 ], [ -87.656663, 41.947194 ], [ -87.656849, 41.947184 ], [ -87.657104, 41.94718 ], [ -87.657252, 41.947178 ], [ -87.657382, 41.947176 ], [ -87.657635, 41.947171 ], [ -87.657739, 41.94717 ], [ -87.658784, 41.947154 ], [ -87.659168, 41.947149 ], [ -87.659165, 41.947254 ], [ -87.659167, 41.94733 ], [ -87.659176, 41.947623 ], [ -87.659181, 41.947876 ], [ -87.659186, 41.948059 ], [ -87.65919, 41.94824 ], [ -87.659196, 41.948487 ], [ -87.659204, 41.948785 ], [ -87.65921, 41.948967 ], [ -87.659219, 41.949292 ], [ -87.659229, 41.949598 ], [ -87.659245, 41.950269 ], [ -87.659254, 41.950595 ], [ -87.659257, 41.950685 ], [ -87.659266, 41.950957 ], [ -87.659269, 41.951048 ], [ -87.658928, 41.951052 ], [ -87.657908, 41.951068 ], [ -87.657569, 41.951074 ], [ -87.657468, 41.951075 ], [ -87.657167, 41.951081 ], [ -87.657067, 41.951083 ], [ -87.657014, 41.951083 ], [ -87.656985, 41.951084 ] ] ] } };
p2 = {"type": "Feature", "properties": {}, "geometry": {"type": "Point", "coordinates": [-87.655889, 41.947783]}};
alert(turf.inside(p2, p1))
<script src="https://npmcdn.com/#turf/turf/turf.min.js"></script>

Categories

Resources