1+ <!DOCTYPE html>
2+ <html >
3+ <head >
4+ <meta charset =" ISO-8859-1" >
5+ <title >Geo IP Test</title >
6+
7+ <!-- jquery dep -->
8+ <script src =" https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js" ></script >
9+ <script type =" text/javascript" >
10+ $ (document ).ready (function () {
11+ // getting the public ip address from api and setting on text box
12+ // ip api : https://www.ipify.org/
13+ $ .get ( " https://api.ipify.org?format=json" , function ( data ) {
14+ console .log (data);
15+ $ (" #ip" ).val (data .ip ) ;
16+ });
17+
18+ function showLocationOnMap (location ) {
19+ var map;
20+
21+ map = new google.maps.Map (document .getElementById (' map' ), {
22+ center: {lat: Number (location .latitude ), lng: Number (location .longitude )},
23+ zoom: 15
24+ });
25+
26+ var marker = new google.maps.Marker ({
27+ position: {lat: Number (location .latitude ), lng: Number (location .longitude )},
28+ map: map,
29+ title: " Public IP:" + location .ipAddress + " @ " + location .city
30+ });
31+
32+ }
33+
34+ $ ( " #ipForm" ).submit (function ( event ) {
35+ event .preventDefault ();
36+ $ .ajax ({
37+ url: " GeoIPTest" ,
38+ type: " POST" ,
39+ contentType: " application/x-www-form-urlencoded; charset=UTF-8" , // send as JSON
40+ data: $ .param ( {ipAddress : $ (" #ip" ).val ()} ),
41+
42+ complete : function (data ) {
43+ console .log (" Request complete" );
44+
45+ },
46+
47+ success : function (data ) {
48+ $ (" #status" ).html (JSON .stringify (data));
49+
50+ if (data .ipAddress != null ) {
51+ console .log (" Success:" + data .ipAddress );
52+ showLocationOnMap (data);
53+
54+ }
55+ },
56+
57+ error : function (err ) {
58+ console .log (err);
59+ $ (" #status" ).html (" Error:" + JSON .stringify (data));
60+ },
61+ });
62+
63+ });
64+
65+ });
66+ </script >
67+ </head >
68+ <body >
69+
70+
71+ <form id =" ipForm" action =" GeoIPTest" method =" POST" >
72+ <input type =" text" name = " ipAddress" id = " ip" />
73+ <input type =" submit" name =" submit" value =" submit" />
74+
75+ </form >
76+
77+ <div id =" status" ></div >
78+
79+ <div id =" map" style =" height : 500px ; width :100% ; position :absolute " ></div >
80+ <!-- AIzaSyDDr65sVtJtlbliOTAeXyZSDPvG9NROjJA -->
81+ <script src =" https://maps.googleapis.com/maps/api/js?key=AIzaSyA4vsDQWHeOrDnzS98XMXl5hgwA9raaQZ8"
82+ async defer ></script >
83+ </body >
84+ </html >
0 commit comments