Spring mvc+jquery+google map to create IP location finder applications

Source: Internet
Author: User
Tags geoip maxmind

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. Users can enter an IP address in the page and then show the approximate geographic location of the IP via Google Maps.

We will use the following techniques:

  1. Spring MVC Frameworks
  2. JQuery (Ajax Request)
  3. GeoLite Database
  4. Google Map
  5. The following steps are provided for the user:

    1. The user enters the IP address and then the Submit button.
    2. jquery sends Ajax requests to the controller in spring MVC
    3. Processing and returning JSON-formatted data in the Spring MVC controller
    4. jquery processes the returned JSON data and presents it to the user via Google Maps

      Spring Mvc+geo Lite

      The following first write the interface based on IP lookup location, named Serverlocationbo.java, the code is:

        1. Package com.mkyong.web.location;
        2. Ublic interface Serverlocationbo {
        3. Serverlocation getLocation (String ipAddress);

      The code for its implementation class is:

      1. Package com.mkyong.web.location;
      2. Import java.io.IOException;
      3. Import Java.net.URL;
      4. Import org.springframework.stereotype.Component;
      5. Import com.maxmind.geoip.Location;
      6. Import Com.maxmind.geoip.LookupService;
      7. Import Com.maxmind.geoip.regionName;
      8. @Component
      9. public class Serverlocationboimpl implements Serverlocationbo {
      10. @Override
      11. Public serverlocation getLocation (String ipAddress) {
      12. String datafile = "Location/geolitecity.dat";
      13. Return GetLocation (ipAddress, datafile);
      14. }
      15. Private Serverlocation getLocation (string ipAddress, String locationdatafile) {
      16. Serverlocation serverlocation = null;
      17. URL url = getclass (). getClassLoader (). getresource (Locationdatafile);
      18. if (URL = = null) {
      19. SYSTEM.ERR.PRINTLN ("Location database was not found-"
      20. + Locationdatafile);
      21. } else {
      22. try {
      23. serverlocation = new Serverlocation ();
      24. Lookupservice lookup = new Lookupservice (Url.getpath (),
      25. Lookupservice.geoip_memory_cache);
      26. Location locationservices = lookup.getlocation (ipAddress);
      27. Serverlocation.setcountrycode (Locationservices.countrycode);
      28. Serverlocation.setcountryname (Locationservices.countryname);
      29. Serverlocation.setregion (locationservices.region);
      30. Serverlocation.setregionname (Regionname.regionnamebycode (
      31. Locationservices.countrycode, locationservices.region));
      32. Serverlocation.setcity (locationservices.city);
      33. Serverlocation.setpostalcode (Locationservices.postalcode);
      34. Serverlocation.setlatitude (
      35. String.valueof (Locationservices.latitude));
      36. Serverlocation.setlongitude (
      37. String.valueof (Locationservices.longitude));
      38. } catch (IOException e) {
      39. System.err.println (E.getmessage ());
      40. }
      41. }
      42. return serverlocation;
      43. }
      44. }

      In this method above, in the Getlocations method, the Geolite format IP address library file GeoLiteCity.dat is loaded, and the GetLocation method is called for IP lookup. In the GetLocation method, Create an instance of the Lookupservice class through Geolite, which passes in the IP address library file to be queried, and then calls its GetLocation method for querying, and the resulting query is constructed as a Serverlocation object. Here's a look at the code for its Serverlocation object:

      1. Package com.mkyong.web.location;
      2. Public class Serverlocation {
      3. private String CountryCode;
      4. private String countryname;
      5. private String region;
      6. private String regionname;
      7. private String City;
      8. private String PostalCode;
      9. private String latitude;
      10. private String longitude;
      11. //getter and Setter methods
      12. }

      Then we use the Spring MVC framework of Jackson to convert the results into JSON, the code is as follows:

      1. Package Com.mkyong.web.controller;
      2. Import Org.codehaus.jackson.map.ObjectMapper;
      3. Import org.springframework.beans.factory.annotation.Autowired;
      4. Import Org.springframework.stereotype.Controller;
      5. Import org.springframework.web.bind.annotation.RequestMapping;
      6. Import Org.springframework.web.bind.annotation.RequestMethod;
      7. Import Org.springframework.web.bind.annotation.RequestParam;
      8. Import Org.springframework.web.bind.annotation.ResponseBody;
      9. Import Org.springframework.web.servlet.ModelAndView;
      10. Import com.mkyong.web.location.ServerLocation;
      11. Import Com.mkyong.web.location.ServerLocationBo;
      12. @Controller
      13. Public class Mapcontroller {
      14. @Autowired
      15. Serverlocationbo Serverlocationbo;
      16. @RequestMapping (value = "/", method = Requestmethod.get)
      17. Public Modelandview getpages () {
      18. Modelandview model = new Modelandview ("map");
      19. return model;
      20. }
      21. //return back JSON string
      22. @RequestMapping (value = "/getlocationbyipaddress", method = Requestmethod.get)
      23. public @ResponseBody
      24. String Getdomaininjsonformat (@RequestParam string ipAddress) {
      25. Objectmapper mapper = new Objectmapper ();
      26. Serverlocation location = serverlocationbo.getlocation (ipAddress);
      27. String result = "";
      28. try {
      29. result = mapper.writevalueasstring (location);
      30. } catch (Exception e) {
      31. E.printstacktrace ();
      32. }
      33. return result;
      34. }
      35. }

Spring mvc+jquery+google map to create IP location finder applications

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.