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:
- Spring MVC Frameworks
- JQuery (Ajax Request)
- GeoLite Database
- Google Map
The following steps are provided for the user:
- The user enters the IP address and then the Submit button.
- jquery sends Ajax requests to the controller in spring MVC
- Processing and returning JSON-formatted data in the Spring MVC controller
- 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:
- Package com.mkyong.web.location;
- Ublic interface Serverlocationbo {
- Serverlocation getLocation (String ipAddress);
The code for its implementation class is:
- Package com.mkyong.web.location;
- Import java.io.IOException;
- Import Java.net.URL;
- Import org.springframework.stereotype.Component;
- Import com.maxmind.geoip.Location;
- Import Com.maxmind.geoip.LookupService;
- Import Com.maxmind.geoip.regionName;
- @Component
- public class Serverlocationboimpl implements Serverlocationbo {
- @Override
- Public serverlocation getLocation (String ipAddress) {
- String datafile = "Location/geolitecity.dat";
- Return GetLocation (ipAddress, datafile);
- }
- Private Serverlocation getLocation (string ipAddress, String locationdatafile) {
- Serverlocation serverlocation = null;
- URL url = getclass (). getClassLoader (). getresource (Locationdatafile);
- if (URL = = null) {
- SYSTEM.ERR.PRINTLN ("Location database was not found-"
- + Locationdatafile);
- } else {
- try {
- serverlocation = new Serverlocation ();
- Lookupservice lookup = new Lookupservice (Url.getpath (),
- Lookupservice.geoip_memory_cache);
- Location locationservices = lookup.getlocation (ipAddress);
- Serverlocation.setcountrycode (Locationservices.countrycode);
- Serverlocation.setcountryname (Locationservices.countryname);
- Serverlocation.setregion (locationservices.region);
- Serverlocation.setregionname (Regionname.regionnamebycode (
- Locationservices.countrycode, locationservices.region));
- Serverlocation.setcity (locationservices.city);
- Serverlocation.setpostalcode (Locationservices.postalcode);
- Serverlocation.setlatitude (
- String.valueof (Locationservices.latitude));
- Serverlocation.setlongitude (
- String.valueof (Locationservices.longitude));
- } catch (IOException e) {
- System.err.println (E.getmessage ());
- }
- }
- return serverlocation;
- }
- }
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:
- Package com.mkyong.web.location;
- Public class Serverlocation {
- private String CountryCode;
- private String countryname;
- private String region;
- private String regionname;
- private String City;
- private String PostalCode;
- private String latitude;
- private String longitude;
- //getter and Setter methods
- }
Then we use the Spring MVC framework of Jackson to convert the results into JSON, the code is as follows:
- Package Com.mkyong.web.controller;
- Import Org.codehaus.jackson.map.ObjectMapper;
- Import org.springframework.beans.factory.annotation.Autowired;
- Import Org.springframework.stereotype.Controller;
- Import org.springframework.web.bind.annotation.RequestMapping;
- Import Org.springframework.web.bind.annotation.RequestMethod;
- Import Org.springframework.web.bind.annotation.RequestParam;
- Import Org.springframework.web.bind.annotation.ResponseBody;
- Import Org.springframework.web.servlet.ModelAndView;
- Import com.mkyong.web.location.ServerLocation;
- Import Com.mkyong.web.location.ServerLocationBo;
- @Controller
- Public class Mapcontroller {
- @Autowired
- Serverlocationbo Serverlocationbo;
- @RequestMapping (value = "/", method = Requestmethod.get)
- Public Modelandview getpages () {
- Modelandview model = new Modelandview ("map");
- return model;
- }
- //return back JSON string
- @RequestMapping (value = "/getlocationbyipaddress", method = Requestmethod.get)
- public @ResponseBody
- String Getdomaininjsonformat (@RequestParam string ipAddress) {
- Objectmapper mapper = new Objectmapper ();
- Serverlocation location = serverlocationbo.getlocation (ipAddress);
- String result = "";
- try {
- result = mapper.writevalueasstring (location);
- } catch (Exception e) {
- E.printstacktrace ();
- }
- return result;
- }
- }
Spring mvc+jquery+google map to create IP location finder applications