How to get Exact User location by Using maps API? - google-maps-api-3

May be similar question asked before. But I am in the middle of finding the location. When I try the following code it shows my internet providers location. and which doesnot show my current location.
function ipLookUp () {
$.ajax('http://ip-api.com/json')
.then(
function success(response) {
console.log('User\'s Location Data is ', response);
console.log('User\'s Country', response.country);
getAdress(response.lat, response.lon)
},
function fail(data, status) {
console.log('Request failed. Returned status of',
status);
}
);
}
function getAddress (latitude, longitude) {
$.ajax('https://maps.googleapis.com/maps/api/geocode/json?latlng=' + latitude + ',' + longitude + '&key=***************************************')
.then(
function success (response) {
console.log('User\'s Address Data is ', response)
},
function fail (status) {
console.log('Request failed. Returned status of',
status)
}
)
}
if ("geolocation" in navigator) {
// check if geolocation is supported/enabled on current browser
navigator.geolocation.getCurrentPosition(
function success(position) { // for when getting location is a success
console.log('latitude', position.coords.latitude, 'longitude', position.coords.longitude);
getAddress(position.coords.latitude, position.coords.longitude);
},
function error(error_message) { // for when getting location results in an error
console.error('An error has occured while retrieving location', error_message);
ipLookUp();
}
);
} else {
// geolocation is not supported
// get your location some other way
console.log('geolocation is not enabled on this browser')
ipLookUp()
}
It shows my ISP city name. But I want my current location name like this sample image.
This is sample image. and it shows my current city and street name in google search results page. But when I use the above code.it shows like this.
AS55836 Reliance Jio Infocomm Limited"
city: "Coimbatore"
country: "India"
isp: "RJIL Tamilnadu LTE SUBSCRIBER PUBLIC"
org: "Reliance Jio infocomm ltd"
Looking some experts advice to trace the location exactly.

Related

HTTP request to a google service returns Error: read ECONNRESET firebase cloud functions

I am trying to translate the name of a user from english to an indian language using google translate api and storing the data back in realtime database with a cloud function.
This function is invoked by a write to the database, and I am using a HTTP POST request to send a request to the cloud translate api and the response is stored back to the database. My code for the translate request is this.
var translate_options = { method: 'POST',
url: 'https://translation.googleapis.com/language/translate/v2',
qs:
{ key: 'key goes here',
},
form: {
q: fullData.name,
target: "te"
},
};
request(translate_options, function (error, translate_response, translate_body) {
if (error){
console.log("In translating, got an error");
console.log(error);
}
// Query to the database goes here.
});
This code, if tried in my laptop, gives me the correct translation, but if I deploy it as a cloud function, it gives me an error. Very specifically
{ Error: read ECONNRESET
at exports._errnoException (util.js:1020:11)
at TLSWrap.onread (net.js:568:26) code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' }
I am on firebase blaze plan, and I am able to sent POST request to my other services, but not a google service.
Can anybody help me with this issue. Thanks in advance.
Edit :
The full code is
var functions = require('firebase-functions');
var admin = require('firebase-admin');
var request = require("request");
admin.initializeApp(functions.config().firebase);
exports.whenUserIsAdded = functions.database.ref('users/{companyId}/{uid}').onCreate(event => {
var fullData = event.data.val();
var lang_code = {
"bengali": "bn",
"telugu": "te",
"english": "en"
}
var lang_var = lang_code[fullData['edition']];
var translate_options = { method: 'POST',
url: 'https://translation.googleapis.com/language/translate/v2',
qs:
{ key: 'Key goes here',
},
form: {
q: fullData.name,
target: lang_var
},
};
request(translate_options, function (error, translate_response, translate_body) {
var farmer_name = "";
if(error){
console.log("There is an error in translation");
console.log(error);
}
translate_body = JSON.parse(translate_body);
if(translate_body.data.translations){
farmer_name = translate_body.data.translations[0].translatedText;
console.log("The farmer name is " + fullData.name +" : " + farmer_name);
// Code to write to the database;
} else{
console.log("The translation failed");
farmer_name = fullData.name;
console.log("The famrer name is " + farmer_name);
}
})
});
You're not returning a promise that's resolved when all the work of your function is complete. If the work was completing in the past, that possibly just means you were lucky. Without returning a promise, Cloud Functions may terminate and clean up any work that wasn't complete when the function returns. Properly returning a promise will prevent Cloud Functions from cleaning up before the work is done.
Please consider reading my blog post about this. There is a section special just for ECONNRESET.

How to publish data based on URL in Meteor with Flow Router

I'm trying to publish data specific to the author of a document in my Jobs collection. My route is setup specifically to each unique author, which I then get via FlowRouter.getParam, but it still does not produce any data. I am subscribed to the 'refiJobs' publication but I'm still struggling. Thanks for reading - help is much appreciated!
My Publication
Meteor.publish('refiJobs', function () {
if (Roles.userIsInRole(this.userId, 'admin')) {
var author = FlowRouter.getParam('author');
return Jobs.find({author: author});
} else {
this.error(new Meteor.Error(403, "Access Denied"));
}
});
My route:
authenticatedRoutes.route( '/admin/:author', {
action: function() {
BlazeLayout.render( 'default', { yield: 'user' } );
}
});
The route parameters are not directly available on the server where you are creating your publication. You need to pass your route parameter through to your publication via your subscription as follows:
Client:
Meteor.subscribe('refiJobs',FlowRouter.getParam('author'));
Server:
Meteor.publish('refiJobs',(author)=>{
check(author,String); // be sure to check the parameter(s) to your publication
if (Roles.userIsInRole(this.userId, 'admin')) {
return Jobs.find({author: author});
} else {
this.error(new Meteor.Error(403, "Access Denied"));
}
});

Meteor geo location from browser package

I've tried searching atmosphere and the web for any package to get users Geo location (latitude, longitude) while accessing the app using their desktop browsers but couldn't find any. Existing applications serve mobile only ex. MDG Geo Location Can someone please tell me how to get the user geo location (latitude, longitude) from desktop through Meteor? Thanks
I know this is an old post, however, thought of putting this through.
You can try using the gunjansoni:html5-api package. It has geo location and lot of other HTML5 Apis to play with.
After you install the package on you client
var html5api = new Html5Api();
and then request for geo location permissions:
var geoLocation = html5api.geoLocation();
Once you have the permissions, get the location.
if (geoLocation) {
geoLocation.getLocation(function(err, res){
if (err) console.log(err);
else console.log(res);
});
}
The result will be something like this:
{
accuracy: 30 // in meters
altitude: null
altitudeAccuracy: null
heading: null
latitude: <latitude>
longitude: <longitude>
speed: null
timestamp: 1457901207220
}
Or you can watch a user's location
if (geoLocation) {
geoLocation.watch(function (err, res) {
if (err) console.log(err);
else {
// the result will be in res.result
console.log(res.result);
Meteor.setInterval(function () {
// to stop watching a location, you need a watchId which is available in res.watchId when the watch started
geoLocation.stopWatching(res.watchId);
}, 30 * 1000);
}
});
}

Firebase on(child_added) some field 'undefined'

I am working on a real time application and i am using firebase with pure html and javascript (not angularJS).
I am having a problem where i saved user's data to firebase with the given code by firebase :
var isNewUser = true;
ref.onAuth(function(authData) {
if (authData && isNewUser) {
authData['status'] = 'active';
authData['role'] = 'member';
ref.child("users").child(authData.uid).set(authData);
}
});
This will add the authData to the /users/ node. As you can see that i also appended some custom fields to the authData, status and role.
Now i am using this code to get the user's data from firebase and display them.
ref4.on("value", function(snapshot) {
var snapshotData = snapshot.val();
console.log('username: '+snapshotData.status);
});
If i use on('value'), the status get printed out on the console but if i do it this way,
ref4.on("child_added", function(snapshot) {
var snapshotData = snapshot.val();
console.log('status: '+snapshotData.status);
});
It is showing undefined for the status. May i know what's wrong and how to fix this problem. Thank you.
Since value is returning the path provided by ref4, and child_added is returning each child of that path, it's unlikely both are going to have a key status.
Consider this data structure:
{
"users": {
"brucelee": {
"status": "awesome"
},
"chucknorris": {
"status": "awesomerest"
}
}
}
If I now query for this according to your incomplete example:
var ref = new Firebase('https://<instance>firebaseio.com/users/brucelee');
ref.on('value', function(snap) {
// requests the brucelee record
console.log(snap.name(), ':', snap.val().status); // "brucelee: awesome"
});
ref.on('child_added', function(snap) {
// iterates children of the brucelee path (i.e. status)
console.log(snap.name(), ':', snap.val().status); // THROWS AN ERROR, because status is a string
});
So to do this on child_added with a data structure like this (and presumably somewhat like yours), it would look as follows:
ref.on('child_added', function(snap) {
// iterates children of the brucelee path (i.e. status)
console.log(snap.name(), ':', snap.val()); // "status: awesome"
});

Apigee Collection using geolocation returns entities in reverse order

when I query my collection based on a distance (i.e. 10km) the API returns the items in reverse order (i.e. farthest first). This is contrary to what the documentation states. How can I get them in the proper order. Here is my code:
var options = {
type:"businesses",
client:dataClient,
qs:{ ql:"location within 10000 of " + geo.lat + ", " + geo.lon }
},
collection = new Apigee.Collection( options );
//Call request to initiate the API call
collection.fetch( function ( error, response ) {
if (error) {
//error
console.log( error );
} else {
//success
populateList( response );
}
});
This is a known bug that we are woking on. Currently, there is no ETA of when a fix will be made.

Resources