map api v2 - how to get coordinates of every object on map - yandex-api

Yandex map api v2 shows objects on map well.
When I execute in browser console:
window.myMap.geoObjects.each(function(obj){console.log(obj.geometry);})
It prints
s {_Mh: Array[2], events: s, options: o, _S: l, _Xr: s…}
_Bb: Array[2]
_EC: 3
_GC: Array[2]
_HC: s
_Mh: Array[2]
_S: l
_Xr: s
events: s
options: o
__proto__: Object
I want to get coordinates of every geo object on map. Where are they?

window.myMap.geoObjects.each(function(obj) {
console.log(obj.geometry.getCoordinates());
});

Related

Flutter sort Firebase snapshot by timestamp

I'm trying to sort snapshot by timestamp but returns original order.
data structure looks like this
I have two snapshot and timestamps are -1536025466539 and -1536025893015.
So, I expect -1536025893015 to come first after sorted.
Does anyone know how to sort correctly?
Code:
Map<dynamic, dynamic> map = snapshot.data?.snapshot?.value;
map.values.toList().sort((a, b) {
return a['timestamp'].compareTo(b['timestamp']);
// also not work return b['timestamp'].compareTo(a['timestamp']);
});
From the above code, it looks like you are not having a variable to hold the list.
Map<dynamic, dynamic> map = {
"one": {"timestamp": 1},
"two": {"timestamp": 2}
};
List list = map.values.toList(); //variable which holds new list created from Map.
//As it new list, any change in list will not have no impact on map.
list.sort((a, b) {
return b["timestamp"].compareTo(a["timestamp"]);
}); // inplace sort
print(list); // [{timestamp: 2}, {timestamp: 1}]
If you want key also in the result,
var list = map.entries.toList();
list.sort((a, b) => b.value["timestamp"].compareTo(a.value["timestamp"]));
var list2 = list.map((a) => {a.key: a.value}).toList();
print(list2); // [{two: {timestamp: 2}}, {one: {timestamp: 1}}]
Unfortunately when you call snapshot.data?.snapshot?.value, the resulting Map doesn't have space for the order of the items anymore.
In most Firebase SDKs you can solve this by looping over the children of the snapshot in the value event listener. But in Flutter you will need to listen to onChildAdded events to maintain the value of the items in Flutter, at least until this issue is addressed: https://github.com/flutter/flutter/issues/20745.
From one of my own projects:
ref.orderByKey().equalTo("child1").onChildAdded.listen((event) {
print(event.snapshot.key);
});
ref.child("child1").once().then((snapshot) {
print("Done loading all data for child1");
});
This will first print the keys of all child nodes (in the requested order), and then "Done loading all data for child1".

search in maps dart2 , same as list.indexOf?

I Use this sample for search in Map but not work :|:
var xmenList = ['4','xmen','4xmen','test'];
var xmenObj = {
'first': '4',
'second': 'xmen',
'fifth': '4xmen',
'author': 'test'
};
print(xmenList.indexOf('4xmen')); // 2
print(xmenObj.indexOf('4xmen')); // ?
but I have error TypeError: xmenObj.indexOf$1 is not a function on last code line.
Pelease help me to search in map object simple way same as indexOf.
I found the answer:
print(xmenObj.values.toList().indexOf('4xmen')); // 2
or this:
var ind = xmenObj.values.toList().indexOf('4xmen') ;
print(xmenObj.keys.toList()[ind]); // fifth
Maps are not indexable by integers, so there is no operation corresponding to indexOf. If you see lists as specialized maps where the keys are always consecutive integers, then the corresponding operation should find the key for a given value.
Maps are not built for that, so iterating through all the keys and values is the only way to get that result.
I'd do that as:
K keyForValue<K, V>(Map<K, V> map, V value) {
for (var entry in map.entries) {
if (entry.value == value) return key;
}
return null;
}
The entries getter is introduced in Dart 2. If you don't have that, then using the map.values.toList().indexOf(value) to get the iteration position, and then map.keys.elementAt(thatIndex) to get the corresponding key.
If you really only want the numerical index, then you can skip that last step.
It's not amazingly efficient (you allocate a new list and copy all the values). Another approach is:
int indexOfValue<V>(Map<Object, V> map, V value) {
int i = 0;
for (var mapValue in map.values) {
if (mapValue == value) return i;
i++;
}
return -1;
}
You can search using .where(...) if you want to find all that match or firstWhere if you assume there can only be one or you only want the first
var found = xmenObj.keys.firstWhere(
(k) => xmenObj[k] == '4xmen', orElse: () => null);
print(xmenObj[found]);

How to summarize values by key of an Erlang map list?

This is what my list of maps looks like:
Map = [#{votes=>3, likes=>20, views=> 100},#{votes=>0, likes=>1, views=> 70},#{votes=>1, likes=>14, views=> 2000}].
I would like to return a summary of all map entries. I have attempted to solve this with fun()s but the logic does not make sense, and I only got non-executeable code.
The problem is that one cannot change variables in Erlang, otherwise this would work:
Summary = #{
votes=>0,
likes=>0,
views=>0,
},
[maps:update(Key, maps:get(Key, MapItem) + maps:get(Key, Summary), Summary) || MapItem <- Map, Key <- [votes, likes, views]].
How ought one go about this and successfully summarize the values of a list of maps?
The functions of fold family are designed to be used in such situations. In your case the following code calculates the map containing totals of entries in maps in the list:
MapsList = [#{votes=>3, likes=>20, views=> 100},
#{votes=>0, likes=>1, views=> 70},
#{votes=>1, likes=>14, views=> 2000}],
Summary = lists:foldl(fun (Map, AccL) ->
maps:fold(fun (Key, Value, Acc) ->
Acc#{Key => Value + maps:get(Key, Acc, 0)}
end, AccL, Map)
end, #{}, MapsList)
Summary value is the map #{votes => 4, likes => 35, views => 2170}.

Is it possible to make an iterator in Lua that can iterate over a dictionary?

Lets say that I have a dictionary in Lua (i.e. a Lua table that contains indexes of type string) like so:
local my_dictionary = {a = 123; b = 321; c = 456; d = 654};
What I am trying to do is create an iterator function that can iterate over a table even if its indexes are of type string; kind of like pairs, however whenever I try to call next() to get the next index,value it will only return the index,value if the index is of type int. An idea I had was maybe to call (index):byte(1, -1) and add up the tuple of ints, and use that as a sort of pretend index, just to keep track of the indexes, but I do not think that would work with next. Here is basically what I have so far:
local function each(list)
if #list > 0 then
local function my_itr(lst, ind)
return next(lst, ind);
end
return my_itr, List, 0;
end
return function() end, nil, nil;
end
this only works for a table with int indexes (an array table), so I was wondering if anyone could help me out.
Thanks.
Edit: To make this less vague here is an example piece of code of what I am trying to accomplish:
local mytable = {a = 123; b = 321; 3, 2, 1, c = "bca"};
for i,v in each(mytable) do
print(i,v);
end
what it should output:
>a 123
>b 321
>1 3
>2 2
>3 1
>c bca
The output would not have to be in exact order.
It should work exactly as you want it to with a couple of tweaks: fix typo in List and pass nil instead of 0:
local function each(list)
local function my_itr(lst, ind)
return next(lst, ind)
end
return my_itr, list, nil
end
local mytable = {a = 123; b = 321; 3, 2, 1, c = "bca"}
for i,v in each(mytable) do
print(i,v)
end
This prints the following for me, which is what you'd need:
1 3
2 2
3 1
a 123
b 321
c bca
You can achieve this very behavior by using pairs. Don't confuse it with ipairs though - these are two different table traversal functions!
While ipairs only traverses integer keys of the table (usually it also stops at the first non-existent integer key), pairs traverses all key-value pairs in the table.
So, by writing
local mytable = {a = 123; b = 321; 3, 2, 1, c = "bca"};
for i, v in pairs(mytable) do
print(i, v);
end
You'll get all key-value pairs printed, in some random order. Here's the demo.
As a sidenote, there's no such thing as 'dictionary' in Lua - all associative arrays are referred to as 'tables'.

Groovy create map from lines of text

I have raw data that is contained in 3 separate lines. I want to build a single map record using parts of each line. I then read the next 3 lines and create the next map record and so on. All the groovy examples I've found on maps show them being created from data on a single line, or possibly i am misunderstanding the examples. Here is what the raw data looks like.
snmp v2: data result = "Local1"
snmp v2: data result ip = "10.10.10.121"
snmp v2: data result gal = "899"
new
snmp v2: data result = "Local2"
snmp v2: data result ip = "192.168.10.2"
snmp v2: data result gal = "7777"
new
I want to put this data into a map. In this example Local1 and Local2 would be keys and they would each have 2 associated values. I will show you my latest attempt but it is little more then a guess that failed.
def data = RAW
def map = [:]
data.splitEachLine("="){
it.each{ x ->
map.put(it[0], it[1])
map.each{ k, v -> println "${k}:${v}" } }}
The desired output is:
[ Local1 : [ ip: "10.10.10.121", gal: "899" ],
Local2: [ ip: "192.168.10.2", gal: "7777" ] ]
You can build a new data structure from an existing one using aggregate operations defined on collections; collect produces a list from an existing list, collectEntries creates a map from a list.
The question specifies there are always three lines for an entry, followed by a line with "new" on it. If I can assume they're always in the same order I can grab the last word off each line, use collate to group every four lines into a sublist, then convert each sublist to a map entry:
lines = new File('c:/temp/testdata.txt').readLines()
mymap = lines.collect { it.tokenize()[-1] }
.collate(4)
.collectEntries { e-> [(e[0].replace('"', ''))) : [ip: e[1], gal: e[2]]] }
which evaluates to
[Local1:[ip:"10.10.10.121", gal:"899"], Local2:[ip:"192.168.10.2", gal:"7777"]]
or remove all the quotes in the first step:
mymap = lines.collect { (it.tokenize()[-1]).replace('"', '') }
.collate(4)
.collectEntries { e-> [(e[0]) : [ip: e[1], gal: e[2]]] }
in order to get
[Local1:[ip:10.10.10.121, gal:899], Local2:[ip:192.168.10.2, gal:7777]]
If you want to get a nested map as suggested by dmahapatro try this:
def map = [:]
data=data.eachLine() { line ->
if(line.startsWith("new")) return
tokens=line.replace("snmp v2: data","").split("=")
tokens=tokens.collect() { it.trim().replace("result ","").replaceAll(/"/, "") }
if(tokens[0]=="result") {
nested=[:]
map[tokens[1]]=nested
}
else
nested[tokens[0]]=tokens[1]
}
println("map: $map")
here we:
iterate over lines
skip lines with "new" at the beginning
remove "snmp v2: data" from the text of the line
split each line in tokens, trim() each token, and remove "result " and quotes
tokens are in pairs and now look like:
result, Local1
ip, 10.10.10.121
gal, 899
next when the first token is "result", we build a nested map and place in the main map at the key given by the value of token[1]
otherwise we populate the nested map with key=token[0] and value=token[1]
the result is:
map: [Local1:[ip:10.10.10.121, gal:899], Local2:[ip:192.168.10.2, gal:7777]]
edit: fixed to remove quotes

Resources