Reputation: 2111
"www.xyz.com"
.Geo Location
who access my website and display it on the Google Map. Or can I get the latitude
or longitude
from where this Web-URL is access ?So, is this possible ?
Upvotes: 2
Views: 1086
Reputation:
This is HTML CODE
<asp:GridView ID="gridView1" runat="server" AutoGenerateColumns = "false">
<Columns>
<asp:BoundField DataField="IPAddress" HeaderText="IP Address" />
<asp:BoundField DataField="CountryName" HeaderText="Country" />
<asp:BoundField DataField="Latitude" HeaderText="Latitude" />
<asp:BoundField DataField="Longitude" HeaderText="Latitude" />
</Columns>
</asp:GridView>
This is C# Code:
protected void Page_Load(object sender, EventArgs e)
{
string ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(ipAddress))
{
ipAddress = Request.ServerVariables["REMOTE_ADDR"];
}
string APIKey = "<Your API Key>";
string url = string.Format("http://api.ipinfodb.com/v3/ip-city/?key={0}&ip={1}&format=json", APIKey, ipAddress);
using (WebClient client = new WebClient())
{
string json = client.DownloadString(url);
Location location = new JavaScriptSerializer().Deserialize<Location>(json);
List<Location> locations = new List<Location>();
locations.Add(location);
gridView1.DataSource = locations;
gridView1.DataBind();
}
}
public class Location
{
public string IPAddress { get; set; }
public string CountryName { get; set; }
public string Latitude { get; set; }
public string Longitude { get; set; }
}
Upvotes: 1
Reputation: 1803
Using HTML Geolocation. HTML Geolocation API is used to locate a user's position. you can get latitude && longitude !! here my codes :
if (navigator.geolocation) {
// support geolocation
var success = function(position){
latitude = position.coords.latitude;
longitude = position.coords.longitude;
console.log('here latitude :'+latitude+' && longitude :'+longitude);
}
var errors = function(){
console.log('not working');
}
navigator.geolocation.getCurrentPosition(success, errors);
} else {
alert('upgrade your browser !');
}
My out put is : here latitude :37.5030042 && longitude :127.0507571
you want to see the all users Geo location who access your website. i think two ways. Using google analytics api with google maps or use ajax(5sec update new geolocation data to html& your Database(mysql,dynamo,redis,etc..))
if (navigator.geolocation) {
// support geolocation
var success = function(position){
latitude = position.coords.latitude;
longitude = position.coords.longitude;
console.log('here latitude :'+latitude+' && longitude :'+longitude);
setInterval(function(e){
$.ajax({
type: "get",
dataType: 'json',
url: 'here request url',
cache: false,
data: { // request user geo data
latitude : latitude,
longitude : longitude
}
}).done(function(xhr) {
if(xhr.status){
// response users geo data and update your html DOM
$('#maps').html(xhr.geo.datas)
}else{
alert(xhr.msg);
}
})
},5000); // default 5 second
}
var errors = function(){
console.log('not working');
}
navigator.geolocation.getCurrentPosition(success, errors);
} else {
alert('upgrade your browser !');
}
Upvotes: 2
Reputation: 705
Try like this to obtain user's location: Unless you set a timeout and error, your request to get the current position might never return.
window.onload = function() {
var startPos;
var geoOptions = {
timeout: 10 * 1000
}
var geoSuccess = function(position) {
startPos = position;
document.getElementById('startLat').innerHTML = startPos.coords.latitude;
document.getElementById('startLon').innerHTML = startPos.coords.longitude;
};
var geoError = function(error) {
console.log('Error occurred. Error code: ' + error.code);
// error.code can be:
// 0: unknown error
// 1: permission denied
// 2: position unavailable (error response from location provider)
// 3: timed out
};
navigator.geolocation.getCurrentPosition(geoSuccess, geoError, geoOptions);
};
Upvotes: 1