How do I get the location of a user? - javascript

I am able to get the user's IP address in my website. Now i need his country, city, region using his IP i got.. I am trying Google's Geolocation API with the following code,
<div id="yourinfo"></div>
<script type="text/javascript" src="http://www.google.com/jsapi?key=ABQIAAAAp04yNttlQq-7b4aZI_jL5hQYPm-xtd00hTQOC0OXpAMO40FHAxQMnH50uBbWoKVHwgpklyirDEregg"></script>
<script type="text/javascript">
if(google.loader.ClientLocation)
{
visitor_lat = google.loader.ClientLocation.latitude;
visitor_lon = google.loader.ClientLocation.longitude;
visitor_city = google.loader.ClientLocation.address.city;
visitor_region = google.loader.ClientLocation.address.region;
visitor_country = google.loader.ClientLocation.address.country;
visitor_countrycode = google.loader.ClientLocation.address.country_code;
document.getElementById('yourinfo').innerHTML = '<p>Lat/Lon: ' + visitor_lat + ' / ' + visitor_lon + '</p><p>Location: ' + visitor_city + ', ' + visitor_region + ', ' + visitor_country + ' (' + visitor_countrycode + ')</p>';
}
else
{
document.getElementById('yourinfo').innerHTML = '<p>Whoops!</p>';
}
</script>
Really this code is not working for me. It is executing the else part and displays Whoops!.
How to get the Geolocation of users. Any other idea let me know.
I already tried the following code which works well, but takes 6 Seconds of delay in free version. Later i came to know that google is giving this sevice for free.. So i am try on that.
http://services.ipaddresslabs.com/iplocation/locateip?key=SAK58RWP983694534K4Z&ip=".$ClientIP
Thank you in Advance

If you are happy to go with HTML5 you should visit this tutorial on geolocation.

use this on client side:
http://services.ipaddresslabs.com/iplocation/locateip?key=SAK58RWP983694534K4Z&ip=local-ip
its working fine with me.
By the way, the above key is blocked, you have to get new key instead.

Related

force_reply works in the address bar but does not work from code in Google Apps Script

The telegram bot must send a message to the user with force_reply active.
Inserting the link in the address bar works; the bot sends the message and the answer function is already active. The same link, generated by the code in Google Apps Script, does not work; the bot does not send anything.
Working link: (I replaced the id of my bot and the ID of the chat with BOT_TOKEN and CHAT_ID)
https://api.telegram.org/BOT_TOKEN/sendMessage?chat_id=CHAT_ID&text=force_replyTest&reply_markup={"force_reply":true}
code to generate the link
var url = "https://api.telegram.org/bot" + token;
function sendText(idChat,text) {
var response = UrlFetchApp.fetch(url + "/sendMessage?chat_id=" + idChat + "&text=" + encodeURIComponent(text) + '&reply_markup={"force_reply":true}');
Logger.log(response.getContentText());
}
if(text == "/p"){
sendText(idChat,"force_reply test");
}
Thank to #TheMaster
Work with this code
var response = UrlFetchApp.fetch(url + "/sendMessage?chat_id=" + idChat + "&text=" + encodeURIComponent(text) + '&reply_markup=' + encodeURIComponent('{"force_reply":true}'));

Add permissions to channel for user

I've recently added this Discord bot to my server: https://github.com/critcola/discord-auto-grouping
It's working great and I've modified it slightly to ensure it creates a room based on the user's name, with a user limit and under a certain category in my server.
My last edit — which I can't seem to get my head around — is, when the channels are created, to add the user to the permissions and allow them full administrative rights on the channel only.
Below is what I've tried so far, it produces no errors it just doesn't do anything hence I'm now stumped!
Any help would be greatly appreciated.
// Check if the user entered a new channel.
if (member.voiceChannelID) {
const newChannel = member.guild.channels.get(member.voiceChannelID);
// If the user entered a game channel (prefixed with a game controller unicode emoji), group them into their own channel.
if (newChannel.name.startsWith(String.fromCodePoint('0x1F3AE'))) {
newChannel.clone(String.fromCodePoint('0x2501') + member.user.username + "'s Room", false)
.then(createdChannel => {
createdChannel.edit({
bitrate: 96000,
//position: newChannel.position + 0,
userLimit: 5,
parent: '409821646698971136'
})
//Set Permissions
.then(createdChannel.overwritePermissions(member.user.id,{'MANAGE_PERMISSIONS':true, 'MANAGE_CHANNELS':true}))
.then(createdChannel => {
member.setVoiceChannel(createdChannel)
.then(console.log('[' + new Date().toISOString() + '] Moved user "' + member.user.username + '#' + member.user.discriminator + '" (' + member.user.id + ') to ' + createdChannel.type + ' channel "' + createdChannel.name + '" (' + createdChannel.id + ') at position ' + createdChannel.position))
.catch(console.error);
})
.catch(console.error);
})
.catch(console.error);
}
}
You've made a little mistake. As MANAGE_PERMISSIONS is an invalid permission,
you need to use MANAGE_ROLES.

Searching for address using google api working in chrome and firefox but not IE

This script fetches an address when the user inputs a house number and postcode. It works fine in chrome and firefox but doesn't work in IE.
I have already added:
but still no luck. Any ideas?
<script type="text/javascript">
$('#submit').click(function() {
//Get Postcode
var number = $('#number').val();
var postcode = $('#postcode').val().toUpperCase();;
//Get latitude & longitude
$.getJSON('http://maps.googleapis.com/maps/api/geocode/json?address=' + number + " " + postcode + '&sensor=false',
function(data) {
//Insert
$('#text').text(number + ', ' + data.results[0].formatted_address );
});
});
</script>

google api not working for geolocation

i have found a script for geolocation resolve with google
in this code there is a key that is 86 char long
i have opened a google api key for the gmail of the website
it is a short key (only 39 chars)
i then changed the key and run the code.
i get the resolve country location that i need.
i then tried from another ip location (at home) and one with a proxy sever and didn't get any thing
i'm not sure if the api key i'm using is set for public or not- how do i check that?
do i have to pay for using it or can be used only with the free 25,000 request per day?
the code i have tried is:
<script type="text/javascript" src="http://www.google.com/jsapi?key="></script>
<script type="text/javascript">
function geoTest() {
if (google.loader.ClientLocation) {
var latitude = google.loader.ClientLocation.latitude;
var longitude = google.loader.ClientLocation.longitude;
var city = google.loader.ClientLocation.address.city;
var country = google.loader.ClientLocation.address.country;
var country_code = google.loader.ClientLocation.address.country_code;
var region = google.loader.ClientLocation.address.region;
var text = 'Your Location<br /><br />Latitude: ' + latitude + '<br />Longitude: ' + longitude + '<br />City: ' + city + '<br />Country: ' + country + '<br />Country Code: ' + country_code + '<br />Region: ' + region;
} else {
var text = 'Google was not able to detect your location';
}
document.write(text);
}
geoTest();
</script>
Seems like the geolocation is not reliable
See Philippe's answer here Is google.loader.clientlocation still supported

Detect event in Google Marker

In my code, how can I get the name of the User that someone clicked on in the marker?
Currently my code has:
function createMarker(point, user, studytopic) {
var marker = new GMarker(point);
var currUser = user;
var html = '<b>' + user + '</b> <br/>' + studytopic + '<br/>' +
' Contact ' + user + '' ;
GEvent.addListener(marker, 'click', function() {
marker.openInfoWindowHtml(html);
});
return marker;
}
currUser is a global field, however, it's not updated every time I click on a different marker in Google maps.
Basically what I'm looking for is a event to fire when a link (id=contactSBLink) within any marker is clicked. I want it to get the Username(which is a link) to pass the user variable to another function.
I'm not sure what's the best way to go about to get this?
You can pass the user u to the javascript:showContactSB(u). This is an exercise in setting quotes properly:
var u = "'" + user + "'";
var html = '<b>' + user + '</b> <br/>' + studytopic + '<br/>' +
' Contact ' + user + '' ;
Now, you get the user in the click-function:
function showContactSB(user) {
alert("Hi " + user);
}
BTW, I would recommend you to upgrade to Google Maps v3.

Categories

Resources