Reverse geocoding not available for China - geocoding

Hey there I am using here.com API for reverse geocoding. However when I try to fetch a location for China based on the latitude and longitde, I get "undefined" for the city.
What is that so?
Does someone know an alternative for reverse geocoding that works good, fast and affordable, instead of here.com ?
Thanks.

Data coverage from a lot of providers in China is spotty at best. Try a few and see what works for you. https://geocode.xyz/CN and https://geocode.earth are two alternatives.

You could give the Open Street Map Nominatim Webservice a go, this will perform geocoding and reverse geocoding for you, though I would check out the terms of use:
Below is an example in Node.js (you must install request and request-promise-native):
const rp = require('request-promise-native');
async function testReverseGeocode(latitude, longitude) {
const options = {
url: "https://nominatim.openstreetmap.org/reverse",
qs: {
lat: latitude,
lon: longitude,
format: 'json'
},
json: true,
headers: {
'User-Agent': "Node.js",
'Accept-Language': 'en-GB,en;q=0.7,en-US;q=0.3'
},
};
let result = await rp(options);
console.log("Result: ", result);
}
testReverseGeocode(31.2304, 121.4737);
If you're happy to go with a paid service, the Google Geocoding API is very good and is not expensive if you don't perform a lot of requests:
const rp = require('request-promise-native');
async function testReverseGeocodeGoogle(latitude, longitude) {
const options = {
url: `https://maps.googleapis.com/maps/api/geocode/json?latlng=${latitude},${longitude}`,
qs: {
key: API_KEY
},
json: true
};
let result = await rp(options);
console.log("Result: ", result);
}
testReverseGeocodeGoogle(31.2304, 121.4737);

Related

Is there an up to date Django geolocation module that can give me countries using IP?

Is there something as simple as plug in the code and go for geolocation? I only want countries so that my website can switch to the appropriate language.
It seems that the ones I found are either deprecated, have less than 10 users, are paid/trial versions.
Faced this problem recently - indeed there are only paid options out there. However, there is an almost free solution is to use Google Cloud functions for that.
https://cloud.google.com/functions/docs/create-deploy-gcloud
Here is the code of a function you want to implement:
const cors = require('cors')
const corsOptions = {
origin: true
}
function _geolocation(req, res) {
const data = {
country: req.headers["x-appengine-country"],
region: req.headers["x-appengine-region"],
city: req.headers["x-appengine-city"],
cityLatLong: req.headers["x-appengine-citylatlong"],
userIP: req.headers["x-appengine-user-ip"],
}
res.json(data)
};
exports.geolocation = (req, res) => {
const corsHandler = cors(corsOptions);
return corsHandler(req, res, function() {
return _geolocation(req, res);
});
};
Then in the JS code of your web-page you call this function and get country from data.country.
However, if what you want is the language selection - I recommend using auto-selection of the language included in Django. Add to your middlewares:
MIDDLEWARE = [
...
'django.middleware.locale.LocaleMiddleware',
]
It will autodetect user's language based on browser settings.
More info: https://docs.djangoproject.com/en/4.1/topics/i18n/translation

Read data from Google Cloud Datastore by dialogflow agent

I am newbie in the chatbot domain. I need to develop a dialogflow chatbot which can store the data collected from user to Google Cloud Datastore Entities(not Firebase real time database) and retrieve it back when the user want to search.
I can able to write the data collected from user to datastore. But I am struggling in retrieving the data. I am writing the function in the dialogflow inline editor.
Write function :
function order_pizza(agent) {
var pizza_size = agent.parameters.size;
var pizza_topping = agent.parameters.pizza_topping;
var date_time = agent.parameters.size;
const taskKey = datastore.key('order_item');
const entity = {
key: taskKey,
data: {
item_name: 'pizza',
topping: pizza_topping,
date_time: date_time,
order_time: new Date().toLocaleString(),
size: pizza_size }
};
return datastore.save(entity).then(() => {
console.log(`Saved ${entity.key.name}: ${entity.data.item_name}`);
agent.add(`Your order for ${pizza_topping} pizza has been placed!`);
});
}
where "order_item" is the kind(table in datastore) the data is being stored. It is storing the data successfully.
Read data:(the function not working)
function search_pizza(agent){
const taskKey = datastore.key('order_item');
var orderid = agent.parameters.id;
const query = datastore.createQuery('taskKey').filter('ID','=', orderid);
return datastore.runQuery(query).then((result) =>{
agent.add(result[0]);
});
}
This is what i tried so far! Whereever i search I can find the result for firebase realtime database. But can't find solution for google datastore!
Followed many tutorial. But can't quite get it right! Kindly help!

How to get TRC20 transactions to an address

I am using tron web to query transactions of an address but it does not return transactions sent to that address where token transferred is TRC20.
This does not work.
I want to get the transactions on an address and get both TRX, trc10 and trc20 transactions.
What am I doing wrong or how to do that?
Here is my code block:
tronWeb.setDefaultBlock("latest");
var result = await tronGrid.account.getTransactions(address, {
only_confirmed: true,
only_to: true,
limit: 10
});
console.log(JSON.stringify(result));
})();
After a lot of research, I found out one can easily query contract events at intervals to get transactions on that contract address and you can then filter it for the address you are watching since you can't get a webhook or websocket with your trongrid/tronweb implementation.
Here is a sample file I used to achieve this and it works great for monitoring many address even with different contract addresses.
Note: In my own implementation, this node file is called from another file and other logistics are handled in the other file, but below you see how I queried the transfer events emitted by the specified contract
const TronWeb = require("tronweb");
const TronGrid = require("trongrid");
const tronWeb = new TronWeb({
fullHost: "https://api.trongrid.io"
});
const tronGrid = new TronGrid(tronWeb);
const argv = require("minimist")(process.argv.slice(2));
var contractAddress = argv.address;
var min_timestamp = Number(argv.last_timestamp) + 1; //this is stored for the last time i ran the query
(async function() {
tronWeb.setDefaultBlock("latest");
tronWeb.setAddress("ANY TRON ADDRESS"); // maybe being the one making the query not necessarily the addresses for which you need the transactions
var result = await tronGrid.contract.getEvents(contractAddress, {
only_confirmed: true,
event_name: "Transfer",
limit: 100,
min_timestamp: min_timestamp,
order_by: "timestamp,asc"
});
result.data = result.data.map(tx => {
tx.result.to_address = tronWeb.address.fromHex(tx.result.to); // this makes it easy for me to check the address at the other end
return tx;
});
console.log(JSON.stringify(result));
})();
You are free to customize the config data passed to the tronGrid.contract.getEvents method. Depending on how frequently transactions come on the contract you are monitoring you should DYOR to know at what interval is great for you and what limit value you should pass.
Refer to https://developers.tron.network/docs/trongridjs for details.
I found a API that can take TRC20 transactions, but I haven't found an implementation in webtron.
https://api.shasta.trongrid.io/v1/accounts/address/transactions
Related document:
https://developers.tron.network/reference#transaction-information-by-account-address

What is the clean way to store information get from web service in angularJS?

I've got a front-end Angular App which get information from server side on a REST API.
When I make requests, I'd like to do it only once and so, keep the data in the front app.
I found several examples but either:
the logic is in controller -> not a good practice
variables are stored in the root scope -> not a good practice in general
I was told to use services to do this.
For example, from what I understood, I should have a service which GET user profile, and another one which keep these information (a "profile" service) once the GET request has been made.
Could someone show me how to do this if my service to call GET request is something like this:
.factory('userPromise', ['$http', '$q', 'baseUrl',
function ($http, $q, baseUrl) {
return {
getUser: function(usn, uid, key) {
return $http.get(baseUrl + 'business_infos/' + parseInt(uid) + '/'
+ '?format=json&username=' + usn + '&api_key=' + key).then(function(response) {
return response.data;
});
}
};
}])
Note that I don't want to just cache information. I want to create a user profile from what I get that can contain other information.
it would be something like:
{
profile: {
firstname: "Thierry",
lastname: "Chatel"
},
hasRole: function (role) {
return ...
}
}
Thank you!
There is documentation for cacheFactory, you can cahce data and get it.
http://code.angularjs.org/1.2.14/docs/api/ng/service/$cacheFactory

Ext.data.jSonP Sencha API with Coldfusion

I am trying to get a JSon from my server. I am calling the API like this:
Ext.data.JsonP.request({
url: 'http://dev.mysite.com/temp.cfm',
callbackKey: 'callback',
timeout: 40000,
params: {
format: 'json'
},
success: function(result, request) {
// Get the weather data from the json object result
var weather = result; console.log('Succ');
},
failure: function(result, request) {
// Get the weather data from the json object result
var weather = result; console.log('Fail');
},
callback: function(result, request) {
// Get the weather data from the json object result
var weather = result; console.log('CallB');
}
});
I am using Coldfusion as Serverside. So, I am simply doing this:
<cfreturn '#url.callback#({\"LOGINSTATUS\":\"fail\"})'>
That returns the following string:
Ext.data.JsonP.callback1({\"LOGINSTATUS\":\"fail\"})
But my request always times out.
I couldn't figure out what was the actual problem. I just tried using cfm file instead of cfc on server side and everything started working.
If anyone could explain why this happened I'll accept that explanation as correct answer.
Thanks DmitryB and Sharondio for you time and trying to help me fix it. I really appreciate your help.