Skip to content

Commit c782dec

Browse files
committed
User can set async true or false
1 parent 79bfdb1 commit c782dec

File tree

2 files changed

+63
-55
lines changed

2 files changed

+63
-55
lines changed

README.md

Lines changed: 57 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -35,61 +35,64 @@ Add the following script in your HTML page:
3535

3636
## Geolocation Lookup
3737

38-
There are four ways to query geolocation from IPGeolocation API. You can use the following functions to get the geolocation as you require.
38+
You can use this SDK without an API key if you're using the _Request Origin_ feaure on IP Geolocation API.
39+
Here are a few different ways of querying Geolocation for an IP address from IP Geolocation API.
3940

4041
```javascript
41-
// Query geolocation for the calling machine's IP address
42-
// Without API key
43-
geolocation(geoResponse)
44-
// with API key
45-
geolocationByAPIkey('YOUR_API_KEY', geoResponse)
42+
// Function to handle the response from IP Geolocation API.
43+
// **response** is a JSON object returned from IP Geolocation API.
44+
function handleResponse(response) {
45+
console.log(response);
46+
}
47+
48+
// Query geolocation for the calling machine's IP address with an API key (optional, if you're using _Request Origin_ feature at IP Geolocation API)
49+
getGeolocation(handleResponse, "YOUR_API_KEY");
50+
51+
// To use the _Request Origin_ feature at IP Geolocation API, you skip the API key parameter.
52+
getGeolocation(handleResponse);
4653

4754
// Query geolocation for an IP address e.g., '1.1.1.1'
48-
// Without API key
49-
geolocationByIP('1.1.1.1', geoResponse)
50-
// With API key
51-
geolocationByAPIkeyAndIP('YOUR_API_KEY', '1.1.1.1', geoResponse)
55+
setIPAddressParameter("1.1.1.1");
56+
getGeolocation(handleResponse, "YOUR_API_KEY");
5257

5358
// Query only specific geolocation fields e.g., 'country_code2,time_zone,currency' for the calling machine's IP address
54-
// Without API key
55-
geolocationByFields('geo,time_zone,currency', geoResponse)
56-
// With API key
57-
geolocationByAPIkeyAndFields('YOUR_API_KEY', 'geo,time_zone,currency', geoResponse)
58-
59-
// Query only specific geolocation fields e.g., 'country_code2,time_zone,currency' for an IP address e.g., '1.1.1.1'
60-
// Without API key
61-
geolocationByFieldsAndIP('geo,time_zone,currency', '1.1.1.1', geoResponse)
62-
// With API key
63-
geolocationByAPIkeyFieldsAndIp('YOUR_API_KEY', 'geo,time_zone,currency', '1.1.1.1', geoResponse)
59+
setFieldsParameter("geo,time_zone,currency");
60+
getGeolocation(handleResponse, "YOUR_API_KEY");
61+
62+
// Query only specific fields like 'country_code2,time_zone,currency' for an IP address like '1.1.1.1' and skip the 'ip' field in the response
63+
setFieldsParameter("geo,time_zone,currency");
64+
setIPAddressParameter("1.1.1.1");
65+
setExcludesParameter("ip");
66+
getGeolocation(handleResponse, "YOUR_API_KEY");
6467
```
6568
## Time Zone API
6669

67-
You can also query time zone information in four different ways. You can use the following functions to get the time zone information as you require.
70+
Here are a few different ways of querying Time Zone information from IP Geolocation API.
6871

6972
```javascript
70-
// Query time zone information for the calling machine's IP address
71-
// Without API key
72-
timezone(timezoneResponse)
73-
// With API key
74-
timezoneByAPIKey('YOUR_API_KEY', timezoneResponse)
73+
// Function to handle the response from IP Geolocation API.
74+
// **response** is a JSON object returned from IP Geolocation API.
75+
function handleResponse(response) {
76+
console.log(response);
77+
}
78+
79+
// Query time zone information for the calling machine's IP address with an API key (optional, if you're using _Request Origin_ feature at IP Geolocation API)
80+
getTimezone(handleResponse, "YOUR_API_KEY");
81+
82+
// To use the _Request Origin_ feature at IP Geolocation API, you skip the API key parameter.
83+
getTimezone(handleResponse);
7584

7685
// Query time zone information for an IP address e.g., '1.1.1.1'
77-
// Without API key
78-
timezoneByIP('1.1.1.1', timezoneResponse)
79-
// With API key
80-
timezoneByAPIKeyAndIP('YOUR_API_KEY', '1.1.1.1', timezoneResponse)
86+
setIPAddressParameter("1.1.1.1");
87+
getTimezone(handleResponse, "YOUR_API_KEY");
8188

8289
// Query time zone infomration for a time zone ID like 'America/New_York'
83-
// Without API key
84-
timezoneByTz('America/Los_Angeles', timezoneResponse)
85-
// with API key
86-
timezoneByAPIKeyAndTz('YOUR_API_KEY', 'America/Los_Angeles', timezoneResponse)
90+
setTimezoneParameter("America/Los_Angeles");
91+
getTimezone(handleResponse, "YOUR_API_KEY");
8792

8893
// Query time zone information by latitude and longitude of the location
89-
// Without API key
90-
timezoneByLatitudeAndLongitude( '31.4816', '74.3551', timezoneResponse)
91-
// With API key
92-
timezoneByAPIKeyLatitudeAndLongitude('YOUR_API_KEY', '31.4816', '74.3551', timezoneResponse)
94+
setCoordinatesParameter("31.4816", "74.3551");
95+
getTimezone(handleResponse, "YOUR_API_KEY");
9396
```
9497

9598
## Example
@@ -98,30 +101,30 @@ Here is a sample code to use IP Geolocation API using JQuery SDK:
98101

99102
```javascript
100103
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
101-
<script src="https://cdn.jsdelivr.net/npm/ip-geolocation-api-jquery-sdk@1.0.4/ipgeolocation.min.js"></script>
104+
<script src="https://cdn.jsdelivr.net/npm/ip-geolocation-api-jquery-sdk@1.0.5/ipgeolocation.min.js"></script>
102105

103106
<script>
104-
var ip = sessionStorage.getItem('ip');
105-
var country_name = sessionStorage.getItem('country_name');
106-
var country_code2 = sessionStorage.getItem('country_code2');
107+
var ip = sessionStorage.getItem("ip");
108+
var country_name = sessionStorage.getItem("country_name");
109+
var country_code2 = sessionStorage.getItem("country_code2");
107110

108111
if (!ip || !country_name || !country_code2) {
109-
var json = geolocationByFields('country_name,country_code2', geoResponse);
110-
ip = json.ip;
111-
country_name = json.country_name;
112-
country_code2 = json.country_code2;
113-
114-
sessionStorage.setItem('ip', ip);
115-
sessionStorage.setItem('country_name', country_name);
116-
sessionStorage.setItem('country_code2', country_code2);
112+
setFieldsParameter("country_name,country_code2");
113+
getGeolocation(handleGeolocationResponse, "YOUR_API_KEY");
117114
}
118115

119116
$(document).ready(function() {
120-
alert('Hello ' + country_name + '!');
117+
alert("Hello " + country_name + "!");
121118
});
122-
123-
function geoResponse(data){
124-
console.log(data);
119+
120+
function handleGeolocationResponse(json) {
121+
ip = json.ip;
122+
country_name = json.country_name;
123+
country_code2 = json.country_code2;
124+
125+
sessionStorage.setItem("ip", ip);
126+
sessionStorage.setItem("country_name", country_name);
127+
sessionStorage.setItem("country_code2", country_code2);
125128
}
126129
</script>
127130
```

ipgeolocation.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
var asyncParameter = true;
12
var ipAddressParameter = "";
23
var excludesParameter = "";
34
var fieldsParameter = "";
@@ -6,6 +7,10 @@ var tzParameter = "";
67
var latitudeParameter = "";
78
var longitudeParameter = "";
89

10+
function setAsyncParameter(async = true) {
11+
asyncParameter = async;
12+
}
13+
914
function setIPAddressParameter (ip = "") {
1015
ipAddressParameter = ip;
1116
}
@@ -86,7 +91,7 @@ function request (subUrl, callback, apiKey = "") {
8691
}
8792

8893
$.ajax ({
89-
async: true,
94+
async: asyncParameter,
9095
method: "GET",
9196
url: "https://api.ipgeolocation.io/".concat(subUrl, urlParameters, ""),
9297
contentType: "application/json",

0 commit comments

Comments
 (0)