Extracting data from HTTP Response - Angular 5 - httpclient

I am trying to extract lat and lng values form the resulting JSON, from HTTP response -
lat: any;
lng: any;
geoResult: any;
public getGeoCoordinates(store) {
let apiUrl = '/assets/GoogleGeoCoordinates.json';
this.http.get(apiUrl).subscribe( resp => {
this.geoResult = resp;
});
}
from above method, I am trying to capture the values as follows
if (resp['status'] == 'OK' && resp['results']['length'] > 0) {
this.lat = resp['results'][0]['geometry']['location']['lat'];
this.lng = resp['results'][0]['geometry']['location']['lng'];
}
if I use alert(), like alert('Latitude : ' + resp['results'][0]['geometry']['location']['lat']);
alert is showing the value.
If I store that value in 'this.lat' variable, I am getting 'undefined'
can someone help me, to understand to get the values from the json HTTP response
JSON file content - GoogleGeoCoordinates.json
{
"results": [
{
"address_components": [
{
"long_name": "120",
"short_name": "120",
"types": [
"street_number"
]
},
{
"long_name": "West 56th Street",
"short_name": "W 56th St",
"types": [
"route"
]
},
{
"long_name": "Manhattan",
"short_name": "Manhattan",
"types": [
"political",
"sublocality",
"sublocality_level_1"
]
},
{
"long_name": "New York",
"short_name": "New York",
"types": [
"locality",
"political"
]
},
{
"long_name": "New York County",
"short_name": "New York County",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "New York",
"short_name": "NY",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "United States",
"short_name": "US",
"types": [
"country",
"political"
]
},
{
"long_name": "10019",
"short_name": "10019",
"types": [
"postal_code"
]
}
],
"formatted_address": "120 W 56th St, New York, NY 10019, USA",
"geometry": {
"location": {
"lat": 40.7640254,
"lng": -73.97896
},
"location_type": "ROOFTOP",
"viewport": {
"northeast": {
"lat": 40.7653743802915,
"lng": -73.97761101970849
},
"southwest": {
"lat": 40.7626764197085,
"lng": -73.98030898029151
}
}
},
"place_id": "ChIJE4YK3PlYwokRybTACneYny4",
"types": [
"street_address"
]
}
],
"status": "OK"
}

I have assigned the values outside the 'subscribe', and it seems started working.
working code as follows -
public getGeoCoordinates(store) {
let apiUrl = '/assets/GoogleGeoCoordinates.json';
this.http.get(apiUrl).subscribe( resp => {
this.geoResult = resp;
});
this.lat = this.geoResult['results'][0]['geometry']['location']['lat'];
this.lng = this.geoResult['results'][0]['geometry']['location']['lng'];
}

Related

Dart, Flutter - I change data in MapList at index 0 But data in MapList index 1 also change it

This is my data map list
data =
[
{
"group": "A",
"direction": "N",
"city": [
{
"city": "CITY1",
"code": "001",
"subCity": [
{
"subCityCode": "C1001",
"member": "15"
},
{
"subCityCode": "C1002",
"member": "20"
}
]
},
{
"city": "CITY2",
"code": "002",
"subCity": [
{
"subCityCode": "C2001",
"member": "42"
},
{
"subCityCode": "C2002",
"member": "65"
}
]
},
]
},
{
"group": "B",
"direction": "S",
"city": [
{
"city": "CITY1",
"code": "001",
"subCity": [
{
"subCityCode": "C1001",
"member": "21"
}
{
"subCityCode": "C1002",
"member": "47"
}
]
},
{
"city": "CITY2",
"code": "002",
"subCity": [
{
"subCityCode": "C2001",
"member": "65"
}
{
"subCityCode": "C2002",
"member": "17"
}
]
},
]
},
]
I would like to change "member": "100" of "subCityCode": "C1001" of "city": "CITY1" of "group": "A"
Therefore I write code like this
data[0]['city'][0]['subCity'][0]['member'] = 100;
And the result is
data =
[
{
"group": "A",
"direction": "N",
"city": [
{
"city": "CITY1",
"code": "001",
"subCity": [
{
"subCityCode": "C1001",
"member": "100"
},
{
"subCityCode": "C1002",
"member": "20"
}
]
},
{
"city": "CITY2",
"code": "002",
"subCity": [
{
"subCityCode": "C2001",
"member": "42"
},
{
"subCityCode": "C2002",
"member": "65"
}
]
},
]
},
{
"group": "B",
"direction": "S",
"city": [
{
"city": "CITY1",
"code": "001",
"subCity": [
{
"subCityCode": "C1001",
"member": "100"
}
{
"subCityCode": "C1002",
"member": "47"
}
]
},
{
"city": "CITY2",
"code": "002",
"subCity": [
{
"subCityCode": "C2001",
"member": "65"
}
{
"subCityCode": "C2002",
"member": "17"
}
]
},
]
},
]
why the data at index 1 also change it
Why data[1] at ['city'][0] at ['subCity'][0] at ['member'] = 100
and how to change only data[0]
--Thank you--
I suppose you have added this data by code, and since dart works on refrences the 2nd object has the same refrence as the first one (as i can see the values are the same)
You can try jsonDecode jsonEncode on object before adding to map.
I have tried the same code and it works fine
void main() {
var data =
[
{
"group": "A",
"direction": "N",
"city": [
{
"city": "CITY1",
"code": "001",
"subCity": [
{
"subCityCode": "C1001",
"member": "15"
},
{
"subCityCode": "C1002",
"member": "20"
}
]
},
{
"city": "CITY2",
"code": "002",
"subCity": [
{
"subCityCode": "C2001",
"member": "42"
},
{
"subCityCode": "C2002",
"member": "65"
}
]
},
]
},
{
"group": "B",
"direction": "S",
"city": [
{
"city": "CITY1",
"code": "001",
"subCity": [
{
"subCityCode": "C1001",
"member": "21"
},
{
"subCityCode": "C1002",
"member": "47"
}
]
},
{
"city": "CITY2",
"code": "002",
"subCity": [
{
"subCityCode": "C2001",
"member": "65"
},
{
"subCityCode": "C2002",
"member": "17"
}
]
},
]
},
];
(data[0]['city'] as List)[0]['subCity'][0]['member'] = "100";
print("NEW DATA --- ${data}");
}

can't able to store fhir resource in mongodb using asymmetrik mongodb fhir-core-server

i'm running node-fhir-server-mongo(Asymmetrik github repo)..when i put resource using PUT method and it stores in mongodb...everything works fine...but the data is partially stored...when i try to access the data i stored in database it only shows few pieces only...
below code is the data i want to store..
{
"resourceType": "Patient",
"id": "example3",
"text": {
"status": "generated",
},
"identifier": [
{
"use": "usual",
"type": {
"coding": [
{
"system": "http://terminology.hl7.org/CodeSystem/v2-0203",
"code": "MR"
}
]
},
"system": "urn:oid:1.2.36.146.595.217.0.1",
"value": "12345",
"period": {
"start": "2001-05-06"
},
"assigner": {
"display": "Acme Healthcare"
}
}
],
"active": true,
"name": [
{
"use": "official",
"family": "Chalmers",
"given": [
"Peter",
"James"
]
},
{
"use": "usual",
"given": [
"Jim"
]
},
{
"use": "maiden",
"family": "Windsor",
"given": [
"Peter",
"James"
],
"period": {
"end": "2002"
}
}
],
"telecom": [
{
"use": "home"
},
{
"system": "phone",
"value": "(03) 5555 6473",
"use": "work",
"rank": 1
},
{
"system": "phone",
"value": "(03) 3410 5613",
"use": "mobile",
"rank": 2
},
{
"system": "phone",
"value": "(03) 5555 8834",
"use": "old",
"period": {
"end": "2014"
}
}
],
"gender": "male",
"birthDate": "1974-12-25",
"_birthDate": {
"extension": [
{
"url": "http://hl7.org/fhir/StructureDefinition/patient-birthTime",
"valueDateTime": "1974-12-25T14:35:45-05:00"
}
]
},
"deceasedBoolean": false,
"address": [
{
"use": "home",
"type": "both",
"text": "534 Erewhon St PeasantVille, Rainbow, Vic 3999",
"line": [
"534 Erewhon St"
],
"city": "PleasantVille",
"district": "Rainbow",
"state": "Vic",
"postalCode": "3999",
"period": {
"start": "1974-12-25"
}
}
],
"contact": [
{
"relationship": [
{
"coding": [
{
"system": "http://terminology.hl7.org/CodeSystem/v2-0131",
"code": "N"
}
]
}
],
"name": {
"family": "du Marché",
"_family": {
"extension": [
{
"url": "http://hl7.org/fhir/StructureDefinition/humanname-own-prefix",
"valueString": "VV"
}
]
},
"given": [
"Bénédicte"
]
},
"telecom": [
{
"system": "phone",
"value": "+33 (237) 998327"
}
],
"address": {
"use": "home",
"type": "both",
"line": [
"534 Erewhon St"
],
"city": "PleasantVille",
"district": "Rainbow",
"state": "Vic",
"postalCode": "3999",
"period": {
"start": "1974-12-25"
}
},
"gender": "female",
"period": {
"start": "2012"
}
}
],
"managingOrganization": {
"reference": "Organization/1"
} }
and the actual data is stored on the mongodb is....
_id: "example3" id: "example3" meta: Object versionId: "1" lastUpdated: "2022-06-28T08:44:44+00:00" resourceType: "Patient"
if anyone know the answer or how to solve please let me know....thanks in advance!

Stuck on simple jolt transformation

So I have this json retrieved from the Google Maps API and I just want to get longitude and latitude. I am looking to use the jolt template to extract just the information that I need.
{
"results": [
{
"address_components": [
{
"long_name": "1115",
"short_name": "1115",
"types": [
"street_number"
]
},
{
"long_name": "West Idaho Avenue",
"short_name": "W Idaho Ave",
"types": [
"route"
]
},
{
"long_name": "Ontario",
"short_name": "Ontario",
"types": [
"locality",
"political"
]
},
{
"long_name": "Malheur County",
"short_name": "Malheur County",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "Oregon",
"short_name": "OR",
"types": [
"administrative_area_level_1",
"political"
]
},
{`enter code here`
"long_name": "United States",
"short_name": "US",
"types": [
"country",
"political"
]
},
{
"long_name": "97914",
"short_name": "97914",
"types": [
"postal_code"
]
},
{
"long_name": "2146",
"short_name": "2146",
"types": [
"postal_code_suffix"
]
}
],
"formatted_address": "1115 W Idaho Ave, Ontario, OR 97914, USA",
"geometry": {
"location": {
"lat": 44.0294445,
"lng": -116.9776502
},
"location_type": "ROOFTOP",
"viewport": {
"northeast": {
"lat": 44.03079348029149,
"lng": -116.9763012197085
},
"southwest": {
"lat": 44.02809551970849,
"lng": -116.9789991802915
}
}
},
"partial_match": true,
"place_id": "ChIJP3C3Z6uPr1QRUDkcSIXzx5g",
"types": [
"establishment",
"point_of_interest",
"school"
]
}
],
"status": "OK"
}
So this is the jolt spec that I am using:
[
{
"operation": "shift",
"spec": {
"results": {
"*": {
"geometry": {
"location": {
"lat": "employees[&1].firstName",
"lng": "employees[&1].lastName"
}
}
}
}
}
}
]
I would like to retrieve a json that looks like this:
{
"data" : [
{
"lng": "-116.9763012197085",
"lat": "44.0294445"
} ]
}
But I keep getting null... Any help would be appreciated thanks
Your original spec wasn't working because "lat": "employees[&1].firstName" should be "lat": "employees[&3].firstName".
In this case &1 evaluated to the word "location". &3 gets you up the tree to the index of the input results array, which is what I think you meant.
Shift maintains a stack as it doing its transform, the & wildcard lets you grab previously matched values from the stack / up the tree.
From where "lat" is in the spec, it is 4 levels up the stack 0,1,2,3 to get to the index of the results array, that was matched by the *.
Spec
[
{
"operation": "shift",
"spec": {
"results": {
"*": {
"geometry": {
"location": {
"lat": "data[&3].lat",
"lng": "data[&3].lng"
}
}
}
}
}
}
]

How to find all mongoDB documents based on geolocation

I'm using the react geosuggest package to save locations of my events in MongoDB. The package takes data from Google places and each time I store it, it gets stored like this:
"_id": "vvagbSbEginyrQGK8",
"name": "test3",
"googleLocation": {
"label": "Testaccio, Roma, Italia",
"placeId": "ChIJE47T5i5gLxMRhiCxCUAFq4Q",
"isFixture": false,
"gmaps": {
"address_components": [
{
"long_name": "Monte Testaccio",
"short_name": "Monte Testaccio",
"types": [
"establishment",
"natural_feature"
]
},
{
"long_name": "Rome",
"short_name": "Rome",
"types": [
"locality",
"political"
]
},
{
"long_name": "Rome",
"short_name": "Rome",
"types": [
"administrative_area_level_3",
"political"
]
},
{
"long_name": "Metropolitan City of Rome",
"short_name": "RM",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "Lazio",
"short_name": "Lazio",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "Italia",
"short_name": "IT",
"types": [
"country",
"political"
]
},
{
"long_name": "00153",
"short_name": "00153",
"types": [
"postal_code"
]
}
],
"formatted_address": "Monte Testaccio, 00153 Rome, Italia",
"geometry": {
"location": {},
"location_type": "APPROXIMATE",
"viewport": {
"f": {
"b": 41.87460301970851,
"f": 41.87730098029151
},
"b": {
"b": 12.474345019708494,
"f": 12.477042980291571
}
}
},
"place_id": "ChIJE47T5i5gLxMRhiCxCUAFq4Q",
"types": [
"establishment",
"natural_feature"
]
},
"location": {
"lat": 41.87595200000001,
"lng": 12.475693999999976
}
}
I've tried a lot of different queries with the mongodb near query, but I can't figure it out. Anyone know about a query that will for example find all documents within 10000 meter based on longitude and latitude that I send in.
According to the current version of mongodb, it has Gospatial query operators and you can achieve what you need after modifying the collection which contains the places as following:
First you should create "2dsphere" index for the collection, let's name it "places" as following:
db.places.createIndex( { loc : "2dsphere" } );
/*
the document should contain "loc" property as following:
{
loc : { type: "Point", coordinates: [ -73.88, 40.78 ] },
//+ the other needed properties.
}
*/
Then for a specific origin and distance range, you can apply the following query:
db.places.find(
{
loc:
{ $near :
{
$geometry: { type: "Point", coordinates: [ -73.9667, 40.78 ]
//the origin point.
},
$minDistance: 1000, //in meter
$maxDistance: 5000 //in meter
}
}
}
)
Note that the coordinates ordered as following [long,lat].
for more information you can check the mongodb document in the following link:
2Dshpere indexes
Geospatial operators

Google Geocoding API. I have a problem with parsing json response

I have a problem with parsing json response. My error is: [__NSArrayI objectForKey:]: unrecognized selector sent to instance. As I see the params should be NSDictionary. Why does the params is an NSArray?
enter code hereNSDictionary *dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error];
NSString* requestStatus = [dictionary objectForKeyNotNull:#"status"];
if ([requestStatus isEqualToString:#"OK"])
{
NSDictionary *params = [dictionary objectForKey:#"results"];
NSString *addresses = [params objectForKey:#"formatted_address"];
}`enter code here`
My json is :
{
"status": "OK",
"results": [ {
"types": [ "street_address" ],
"formatted_address": "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
"address_components": [ {
"long_name": "1600",
"short_name": "1600",
"types": [ "street_number" ]
}, {
"long_name": "Amphitheatre Pkwy",
"short_name": "Amphitheatre Pkwy",
"types": [ "route" ]
}, {
"long_name": "Mountain View",
"short_name": "Mountain View",
"types": [ "locality", "political" ]
}, {
"long_name": "California",
"short_name": "CA",
"types": [ "administrative_area_level_1", "political" ]
}, {
"long_name": "United States",
"short_name": "US",
"types": [ "country", "political" ]
}, {
"long_name": "94043",
"short_name": "94043",
"types": [ "postal_code" ]
} ],
"geometry": {
"location": {
"lat": 37.4219720,
"lng": -122.0841430
},
"location_type": "ROOFTOP",
"viewport": {
"southwest": {
"lat": 37.4188244,
"lng": -122.0872906
},
"northeast": {
"lat": 37.4251196,
"lng": -122.0809954
}
}
}
} ]
}
For #"results" key you have an array.
So you need
NSString *addresses = [[params objectAtIndex:0] objectForKey:#"formatted_address"];