if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position){
initialize(position.coords.latitude,position.coords.longitude);
});
}
function initialize(lat,lng) {
//directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);
//directionsService = new google.maps.DirectionsService();
var latlng = new google.maps.LatLng(lat, lng);
//alert(latlng);
getLocation(latlng);
}
function getLocation(latlng){
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
var loc = getCountry(results);
alert("location is::"+loc);
}
}
});
}
function getCountry(results)
{
for (var i = 0; i < results[0].address_components.length; i++)
{
var shortname = results[0].address_components[i].short_name;
var longname = results[0].address_components[i].long_name;
var type = results[0].address_components[i].types;
if (type.indexOf("country") != -1)
{
if (!isNullOrWhitespace(shortname))
{
return shortname;
}
else
{
return longname;
}
}
}
}
function isNullOrWhitespace(text) {
if (text == null) {
return true;
}
return text.replace(/\s/gi, '').length < 1;
}
//CHECK IF BROWSER HAS HTML5 GEO LOCATION
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
//GET USER CURRENT LOCATION
var locCurrent = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
//CHECK IF THE USERS GEOLOCATION IS IN AUSTRALIA
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'latLng': locCurrent }, function (results, status) {
var locItemCount = results.length;
var locCountryNameCount = locItemCount - 1;
var locCountryName = results[locCountryNameCount].formatted_address;
if (locCountryName == "Australia") {
//SET COOKIE FOR GIVING
jQuery.cookie('locCountry', locCountryName, { expires: 30, path: '/' });
}
});
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace SmartGuide.Core.Services.CountryLocators
{
public static class CountryLocator
{
private static readonly Lazy<List<CountryPolygons>> _countryPolygonsByCountryName = new(() =>
{
var dataGeoJsonFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data.geo.json");
var stream = new FileStream(dataGeoJsonFileName, FileMode.Open, FileAccess.Read);
var geoJson = _Deserialize<Root>(stream);
var countryPolygonsByCountryName = geoJson.Features.Select(
feature => new CountryPolygons
{
CountryName = feature.Properties.Name,
Polygons =
feature.Geometry.Type switch
{
"Polygon" => new List<List<GpsCoordinate>>(
new[]
{
feature.Geometry.Coordinates[0]
.Select(x => new GpsCoordinate(
Convert.ToDouble(x[1]),
Convert.ToDouble(x[0])
)
).ToList()
}
),
"MultiPolygon" => feature.Geometry.Coordinates.Select(
polygon => polygon[0].Select(x =>
new GpsCoordinate(
Convert.ToDouble(((JArray) x)[1]),
Convert.ToDouble(((JArray) x)[0])
)
).ToList()
)
.ToList(),
_ => throw new NotImplementedException($"Unknown geometry type {feature.Geometry.Type}")
}
}
).ToList();
return countryPolygonsByCountryName;
});
public static string GetCountryName(GpsCoordinate coordinate)
{
var country = _countryPolygonsByCountryName.Value.FirstOrDefault(country =>
country.Polygons.Any(polygon => _IsPointInPolygon(polygon, coordinate)));
return country?.CountryName;
}
// taken from https://stackoverflow.com/a/7739297/379279
private static bool _IsPointInPolygon(IReadOnlyList<GpsCoordinate> polygon, GpsCoordinate point)
{
int i, j;
bool c = false;
for (i = 0, j = polygon.Count - 1; i < polygon.Count; j = i++)
{
if ((((polygon[i].Latitude <= point.Latitude) && (point.Latitude < polygon[j].Latitude))
|| ((polygon[j].Latitude <= point.Latitude) && (point.Latitude < polygon[i].Latitude)))
&& (point.Longitude < (polygon[j].Longitude - polygon[i].Longitude) * (point.Latitude - polygon[i].Latitude)
/ (polygon[j].Latitude - polygon[i].Latitude) + polygon[i].Longitude))
{
c = !c;
}
}
return c;
}
private class CountryPolygons
{
public string CountryName { get; set; }
public List<List<GpsCoordinate>> Polygons { get; set; }
}
public static TResult _Deserialize<TResult>(Stream stream)
{
var serializer = new JsonSerializer();
using var sr = new StreamReader(stream);
using var jsonTextReader = new JsonTextReader(sr);
return serializer.Deserialize<TResult>(jsonTextReader);
}
public readonly struct GpsCoordinate
{
public GpsCoordinate(
double latitude,
double longitude
)
{
Latitude = latitude;
Longitude = longitude;
}
public double Latitude { get; }
public double Longitude { get; }
}
}
}
// Generated by https://json2csharp.com/ (with Use Pascal Case) from data.geo.json
public class Feature
{
public string Type { get; set; }
public string Id { get; set; }
public Properties Properties { get; set; }
public Geometry Geometry { get; set; }
}
public class Geometry
{
public string Type { get; set; }
public List<List<List<object>>> Coordinates { get; set; }
}
public class Properties
{
public string Name { get; set; }
}
public class Root
{
public string Type { get; set; }
public List<Feature> Features { get; set; }
}