Getting an ID as well as Coordinates from Leaflet marker on dragend - leaflet

I am looping through an array and loading markers on a map. I would like to update the coordinates of the array element if the marker is moved.
My code so far is like this:
var m;
for (var i = 0; i < data.length; i++) {
var plateNo = data[i].PLATE_NUMBER;
var trackingNo = data[i].TRACKING_NUMBER;
var inventoryId = data[i].INVENTORY_ID;
if (data[i].INVENTORY_STATUS !== 'Complete') {
var icon = epsMarker.incompleteIcon;
var popup = '<h5>EPS</h5>' + 'Plate:' + plateNo + '<br/>' + '</p>';
m = L.marker([data[i].REF_LATITUDE, data[i].REF_LONGITUDE], {
icon: icon,
draggable: 'true'
})
.bindPopup(popup);
m.on('dragend', function (event) {
var marker = event.target;
var position = marker.getLatLng();
console.log(position);
//Call Database and Update Position by INVENTORY_ID
});
}
}
I can get the Lat and Long but I would also like to get the INVENTORY_ID parameter. The idea is to look up the data on the Database by INVENTORY_ID and then update the lat and long.
I appreciate any help or pointers on this. Thanks in advance...

You could store the ID in your marker as an option or member property so you can later retrieve it:
// Store as option
var marker = L.marker([0,0], {id: INVENTORY_ID}).addTo(map);
console.log(marker.options.id);
// Store as member property
var marker = L.marker([0,0]).addTo(map);
marker.id = INVENTORY_ID;
console.log(marker.id);

Related

Insert multiple elements in .bindPopup in Leaflet

I have a function which extracts multiple elements from JSON datatype. I try to bind now multiple elements from my function in my popup divided by paragraphs, but I'm not able to set multiple variables in the popup. How can integrate multiple elements? I tried paste() or & but it won't work.
function getUsers() {
$.getJSON("getData.php", function (data) {
for (var i = 0; i < data.length; i++) {
var location = new L.LatLng(data[i].lat, data[i].lng);
var species = data[i].species;
var diameter = data[i].average_diameter;
var quality = data[i].quality;
var damage = data[i].damage;
var notes = data[i].additional_information;
var marker = L.marker([data[i].lat, data[i].lng], {icon: greenIcon}).addTo(map);
marker.bindPopup(diameter);
}
})
}
You can do this:
marker.bindPopup(diameter + "|" + quality);
// or
marker.bindPopup(`${diameter} | ${quality}`);

how to get 1 item from a data binding in the controller?

I have a table that is filled with data via data binding. And now in my controller i'm trying to loop over all items of the databinding. This is what i have so far but i can't get it to work.
colorRows : function(oTable) {
var items = oTable.getBinding("items");
var rowCount = items.length; //number of visible rows
var currentRowContext;
for (var i = 0; i < rowCount; i++) {
currentRowContext = items[i].getValue(); //this won't work
}
}
so i need to get a value from the item with the index that matches i.
edit: i'm using sap.m.table
As Keshet mentioned, it depends on the Table you are using. Here is an exapmle for the sap.ui.table.Table. First you get the Context of each Row and then you can access the Data that is saved on the Row (btw, there is no such thing as RowValue):
colorRows: function(oTable) {
var aRows = oTable.getRows();
var currentRowValue;
for (var i = 0; i < aRows.length; i++) {
var oRowContext = oTable.getContextByIndex(i);
if (oRowContext) {
var oRowObject = oRowContext.getObject();
// or you can use the getProperty method
var oSingleValue = oRowContext.getProperty("yourPropertyName");
}
}
}
I assume you use sap.m.Table.
colorRows : function(oTable) {
var sPath = oTable.getBinding("items").getPath(); //path to table's data
var oModel = this.getView().getModel(); //model which is bound to the table
//or var oModel = oTable.getModel(); if the model is bound directly to the table
var aData = oModel.getProperty(sPath);//array of rows
var rowCount = aData.length;
var currentRowContext;
for (var i = 0; i < rowCount; i++) {
currentRowContext = aData[i];
}
}
Here is a working example.

css/javascript multiple card flip: reset other cards

So I'm currently using this one: http://jsfiddle.net/nawdpj5j/10/
Now what I need is that when I flip one card (doesn't matter which one) and then flip another one the first one resets/turnes back.
I think I need to add something in here:
var init = function() {
var flippers = document.getElementsByClassName("flip");
for(i = 0; i < flippers.length; i++){
flippers[i].addEventListener( 'click', function(){
var cardID = this.dataset.targetid;
var card = document.getElementById(cardID);
card.toggleClassName('flipped');
}, false);
}
};
Thank you in advance!
You can get an array of all flipped cards and flip them back whenever a card is flipped like so:
var init = function() {
var flippers = document.getElementsByClassName("flip");
for (i = 0; i < flippers.length; i++) {
flippers[i].addEventListener('click', function() {
var cardID = this.dataset.targetid;
var card = document.getElementById(cardID);
var flipped = document.getElementsByClassName('flipped');
for (i = 0; i < flipped.length; i++) {
if (card !== flipped[i]) {
flipped[i].toggleClassName('flipped');
}
}
card.toggleClassName('flipped');
}, false);
}
};
window.addEventListener('DOMContentLoaded', init, false);
Here is a link to a working demo JS FIDDLE

Leaflet : Map container is already initialized

The User types some address (Hyderabad,Telanagna) on text field and clicks on Go button
I am fetching the latitude_res and longitude from google Map API as shown below
$(document).on('click', '.gobtn', function(event) {
$.getJSON('https://maps.googleapis.com/maps/api/geocode/json?address=' + address + '', function(data) {
latitude_res = data.results[0].geometry.location.lat;
longitude_res = data.results[0].geometry.location.lng;
}).done(function() {
});
});
I am able to fetch the latitude and longitude , from google API say for example i got the following values
**17.385044
78.910965**
Then i am making a Ajax call again to fetch all the Markers present at this location in our Database .
And finally initializaing the map as shown below
initializeMap(lator,lonor,markers);
function initializeMap(lator, lonor, markers) {
var map = new L.Map('map_canvas', {
center: new L.LatLng(lator, lonor),
zoom: 5
});
var ggl = new L.Google();
var ggl2 = new L.Google('ROADMAP');
map.addLayer(ggl2);
if (markers.length > 0) {
var markers_leafcontainer = new L.MarkerClusterGroup();
var markersList = [];
for (var i = 0; i < markers.length; i++) {
var lat = parseFloat(markers[i].latitude);
var lng = parseFloat(markers[i].longititude);
var trailhead_name = markers[i].address;
var dealerId = markers[i].dealerID_db;
var dealername = markers[i].dealerName_db;
var contentString = "<html><body><div><p><h2>" + dealername + "</h2></p></div></body></html>";
var marker = L.marker(new L.LatLng(lat, lng), {}).on('click', onClick);
$(".howmanyfound").text(markers.length + ' Found');
markers_leafcontainer.addLayer(marker);
}
map.addLayer(markers_leafcontainer);
map.fitBounds(markers_leafcontainer.getBounds()); //set view on the cluster extent
}
}
Only for the first time this is working , from there on i am getting Uncaught Error: Map container is already initialized.
I am using leaflet with google Maps
So, don't initialize the map inside your function.
var map = new L.Map('map_canvas');
initializeMap(lator,lonor,markers);
function initializeMap(lator, lonor, markers) {
map.setView(L.latLng(lator, lonor));
}

get values from XML file in my Titanium Project

I am having the following .xml which i used in my android project.now,am trying to create the same in iPhone using titanium.Can some one tell me how to get this values from the xml file and show them in a table view.? if i give MainCategories the 3 values in it should appear in the table view.Also can some one tell me is there any other better option to achieve this?or can we use plist??thank you.
<string-array name="MainCategories">
<item>Acceleration</item>
<item>Angle</item>
<item>Area</item>
</string-array>
<string-array name="Acceleration_array">
<item>meter/sq sec</item>
<item>km/sq sec</item>
<item>mile/sq sec</item>
<item>yard/sq sec</item>
</string-array>
<string-array name="Angle_array">
<item>degree</item>
<item>radian</item>
<item>grad</item>
<item>gon</item>
</string-array>
Try This.
var result = [];
var fileName = 'arrays1.xml'; //save xml file
var tableView = Ti.UI.createTableView({ data : result, width : '100%', height : '100%' });
function readXML(fileName)
{
var file = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, fileName);
var xmltext = file.read().text;
var doc = Ti.XML.parseString(xmltext);
var parentNodeLength = doc.documentElement.getElementsByTagName('string-array').length;
for (var i = 0; i < parentNodeLength; i++) {
var attrValue = doc.documentElement.getElementsByTagName('string-array').item(i).attributes.getNamedItem('name').nodeValue;
if (attrValue === 'Angle_array') {
var parentNode = doc.documentElement.getElementsByTagName('string-array').item(i);
var subNodeLength = parentNode.getElementsByTagName('item').length;
for (var j = 0; j < subNodeLength; j++) {
var title = parentNode.getElementsByTagName('item').item(j).text;
var row = Ti.UI.createTableViewRow({
height : 110
});
var label = Ti.UI.createLabel({
height : Ti.UI.SIZE,
width : Ti.UI.SIZE,
text : title
});
row.add(label);
result.push(row);
}
}
}
}
readXML(fileName);
tableView.setData(result);
win1.add(tableView);
win1.open();
i am not familiar with titanium but maybe this should help you out:
var result = this.responseText;
var xml = Ti.XML.parseString(result);
var params = xml.documentElement.getElementsByTagName("member");
var name = xml.documentElement.getElementsByTagName("name");
var value = xml.documentElement.getElementsByTagName("string");
var data = [];
for (var i=0;i<params.item.length;i++) {
Ti.API.log('Param '+i+': Name: '+n.item(i).text);
Ti.API.log('Param '+i+': Value: '+v.item(i).text);
// Add to array
data.push({"name":n.item(i).text,"value":v.item(i).text});
}
copied from this link
for displaying it in uitableview, save your values in nsarray and display your array in tableview. here is a tutorial for UITableView.