Code2night
  • Home
  • Guest Posts
  • Tutorial
  • Languages
    • Angular
    • C
    • C#
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
    • Word Counter
    • Base64 Encode/Decode
    • Diff Checker
    • JSON to CSV
    • Password Generator
  • Register
  • Login
  1. Home
  2. Blogpost

How to get visitors location like country and city

Date- Aug 31,2023

4140

Free Download Pay & Download
Visitor Location Browser Location

One popular option is the IP Geolocation API, which allows you to obtain geolocation information based on the user's IP address.It can be used for getting user details. Here's an example of how you can use the this API:

 <main>
   

    <div class="row">
        <section class="col-md-4" aria-labelledby="gettingStartedTitle">
            <h2 id="gettingStartedTitle">User Location and IP</h2>

            <p id="IP"></p>
            <p id="City"></p>
            <p id="Country"></p>
            <p id="currency"></p>
            <p id="timezone"></p>
        </section>
        
    </div>
</main>
<script>
     fetch('https://ipapi.co/json/')
                        .then(response => response.json())
                        .then(data => {

                            const city = data.city;
                            console.log(data);
                            
                            $('#IP').html("IP Address: " + data.ip);
                            $('#City').html("city: " + data.city);
                            $('#Country').html("Country: " + data.country_name);
                            $('#currency').html("currency: " + data.currency);
                            $('#timezone').html("timezone: " + data.timezone);

                        })
</script>

Now run the application and check the console tab after inspect element. You will be able to see following details

So, this is how  to get visitors location like country and city. And also we can get the user's ip address if we need.

In the code above, we use the IP Geolocation API provided by ipapi.co. The API endpoint 'https://ipapi.co/json/' returns JSON data containing geolocation information based on the user's IP address. We can extract the city information from the response and log it to the console or user it wherever we want .

One thing to be noted here is IP Geolocation api can have usage restriction for free quota . So don't forget to check the terms of usage and limits. And also results can vary in case of use of vpn's.

S
Shubham Batra
Programming author at Code2Night โ€” sharing tutorials on ASP.NET, C#, and more.
View all posts โ†’

Comments

Tags

AspNet
C#
programming
AspNet MVC
c programming
AspNet Core
C
software development
tutorial
MVC
memory management
Paypal
coding
coding best practices
data structures
programming tutorial
tutorials
object oriented programming
Slick Slider
StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp Join Us On Facebook
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blogs
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
Free Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Diff Checker
  • Base64 Encode/Decode
  • Word Counter
By Language
  • Angular
  • C
  • C#
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ยท  Terms
Translate Page