Spring mvc+jquery+google map for IP location lookup applications (2)

Source: Internet
Author: User

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:

  1. <html>
  2. <head>
  3. <script src="Http://maps.google.com/maps/api/js?sensor=true"></script >
  4. <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></ Script>
  5. </head>
  6. <body>
  7. <h2>spring MVC + jQuery + Google maps</H2>
  8. <div>
  9. <input type="text" placeholder="0.0.0.0" id="W-input-search " value= " ">
  10. <span>
  11. <button id="W-button-search" type="button">search</button>
  12. </span>
  13. </div>
  14. <script>
  15. $ (document). Ready (function () {
  16. $ ("#w-button-search"). Click (function () {
  17. $.getjson ("${pagecontext.request.contextpath}/getlocationbyipaddress",
  18. {
  19. IpAddress: $ (' #w-input-search '). Val ()
  20. },
  21. function (data) {
  22. var data = json.stringify (data);
  23. var json = json.parse (data);
  24. Showmap (json["latitude"],json["longitude"])
  25. $ ("#result"). HTML (data)
  26. })
  27. . Done (function () {
  28. })
  29. . Fail (function () {
  30. })
  31. . Complete (function () {
  32. });
  33. });
  34. var map;
  35. function Showmap (latitude,longitude) {
  36. var googlelatandlong = new Google.maps.LatLng (latitude,longitude);
  37. var mapoptions = {
  38. Zoom:5,
  39. Center:googlelatandlong,
  40. MapTypeId:google.maps.MapTypeId.ROADMAP
  41. };
  42. var mapdiv = document.getElementById ("map");
  43. map = new Google.maps.Map (Mapdiv, mapoptions);
  44. var title = "Server location";
  45. Addmarker (map, Googlelatandlong, title, "");
  46. }
  47. function Addmarker (map, Latlong, title, content) {
  48. var markeroptions = {
  49. Position:latlong,
  50. Map:map,
  51. Title:title,
  52. Clickable:true
  53. };
  54. var marker = new Google.maps.Marker (markeroptions);
  55. }
  56. });
  57. </Script>
  58. <br/>
  59. <div id="result"></div>
  60. <br/>
  61. <div style="width:600px;height:400px" id="map"></div >
  62. </body>
  63. </html>

Spring mvc+jquery+google map for IP location lookup applications (2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.