Jsp+jquery+google map shows the final results
In this article, readers will learn how to use the Spring MVC Framework and jquery and Google Maps to create a simple search application based on an IP location. The user can enter an IP address in the page and then show the approximate geographic location of the IP via Google Map (note: The database used in this article is Geolite).
Finally, in the page, we send AJAX requests via jquery to call the Spring MVC control layer and then present the returned results in the page with the following code:
- <html>
- <head>
- <script src="Http://maps.google.com/maps/api/js?sensor=true"></script >
- <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></ Script>
- </head>
- <body>
- <h2>spring MVC + jQuery + Google maps</H2>
- <div>
- <input type="text" placeholder="0.0.0.0" id="W-input-search " value= " ">
- <span>
- <button id="W-button-search" type="button">search</button>
- </span>
- </div>
- <script>
- $ (document). Ready (function () {
- $ ("#w-button-search"). Click (function () {
- $.getjson ("${pagecontext.request.contextpath}/getlocationbyipaddress",
- {
- IpAddress: $ (' #w-input-search '). Val ()
- },
- function (data) {
- var data = json.stringify (data);
- var json = json.parse (data);
- Showmap (json["latitude"],json["longitude"])
- $ ("#result"). HTML (data)
- })
- . Done (function () {
- })
- . Fail (function () {
- })
- . Complete (function () {
- });
- });
- var map;
- function Showmap (latitude,longitude) {
- var googlelatandlong = new Google.maps.LatLng (latitude,longitude);
- var mapoptions = {
- Zoom:5,
- Center:googlelatandlong,
- MapTypeId:google.maps.MapTypeId.ROADMAP
- };
- var mapdiv = document.getElementById ("map");
- map = new Google.maps.Map (Mapdiv, mapoptions);
- var title = "Server location";
- Addmarker (map, Googlelatandlong, title, "");
- }
- function Addmarker (map, Latlong, title, content) {
- var markeroptions = {
- Position:latlong,
- Map:map,
- Title:title,
- Clickable:true
- };
- var marker = new Google.maps.Marker (markeroptions);
- }
- });
- </Script>
- <br/>
- <div id="result"></div>
- <br/>
- <div style="width:600px;height:400px" id="map"></div >
- </body>
- </html>
Spring mvc+jquery+google map for IP location lookup applications (2)