Spring MVC+JQuery+Google Map打造IP位置查找应用

在本文中,读者将学习到如何使用Spring MVC框架和jQuery及Google Map,制作一个简单的根据IP位置查找应用。用户可以在页面中输入一个IP地址,然后通过Google Map显示该IP所在的大概地理位置.

我们将用到如下技术:

  1. Spring MVC frameworks
  2. jQuery(Ajax Request)
  3. GeoLite 数据库
  4. Google Map
  5. 其中用户的操作步骤如下:

    1. 用户输入IP地址,然后点提交按钮
    2. jQuery发出Ajax请求到Spring MVC中的控制器
    3. 在Spring MVC控制器中处理并返回JSON格式数据
    4. jQuery处理返回的json数据并通过Google Map展示给用户

      Spring MVC+Geo Lite

      下面首先编写根据IP查找地理位置的接口,命名为ServerLocationBo.java,代码为:

      1. package com.mkyong.web.location; 
      2.  
      3. ublic interface ServerLocationBo { 
      4.  
      5. ServerLocation getLocation(String ipAddress); 

      其实现类的代码为:

      1. package com.mkyong.web.location; 
      2.   
      3. import java.io.IOException; 
      4. import java.net.URL; 
      5. import org.springframework.stereotype.Component; 
      6. import com.maxmind.geoip.Location; 
      7. import com.maxmind.geoip.LookupService; 
      8. import com.maxmind.geoip.regionName; 
      9.   
      10. @Component 
      11. public class ServerLocationBoImpl implements ServerLocationBo { 
      12.   
      13.     @Override 
      14.     public ServerLocation getLocation(String ipAddress) { 
      15.   
      16.         String dataFile = "location/GeoLiteCity.dat"; 
      17.         return getLocation(ipAddress, dataFile); 
      18.   
      19.     } 
      20.   
      21.     private ServerLocation getLocation(String ipAddress, String locationDataFile) { 
      22.   
      23.       ServerLocation serverLocation = null; 
      24.   
      25.       URL url = getClass().getClassLoader().getResource(locationDataFile); 
      26.   
      27.       if (url == null) { 
      28.         System.err.println("location database is not found - " 
      29.             + locationDataFile); 
      30.       } else { 
      31.   
      32.       try { 
      33.   
      34.         serverLocation = new ServerLocation(); 
      35.   
      36.         LookupService lookup = new LookupService(url.getPath(), 
      37.                 LookupService.GEOIP_MEMORY_CACHE); 
      38.         Location locationServices = lookup.getLocation(ipAddress); 
      39.   
      40.         serverLocation.setCountryCode(locationServices.countryCode); 
      41.         serverLocation.setCountryName(locationServices.countryName); 
      42.         serverLocation.setRegion(locationServices.region); 
      43.         serverLocation.setRegionName(regionName.regionNameByCode( 
      44.             locationServices.countryCode, locationServices.region)); 
      45.         serverLocation.setCity(locationServices.city); 
      46.         serverLocation.setPostalCode(locationServices.postalCode); 
      47.         serverLocation.setLatitude( 
      48.                                 String.valueOf(locationServices.latitude)); 
      49.         serverLocation.setLongitude( 
      50.                                 String.valueOf(locationServices.longitude)); 
      51.   
      52.       } catch (IOException e) { 
      53.   
      54.         System.err.println(e.getMessage()); 
      55.   
      56.       } 
      57.   
      58.      } 
      59.   
      60.      return serverLocation; 
      61.   
      62.     } 

      在上面的这个方法中,在getLocations方法中,加载了GeoLite格式的IP地址库文件GeoLiteCity.dat,并且调用getLocation方法进行IP的查找,.在getLocation方法中,通过GeoLite创建一个LookupService类的实例,其中传入要查询的IP地址库文件,然后再调用其getLocation方法进行查询,这里查询出来的结果构造成serverLocation对象,下面来看下其serverlocation对象的代码:

      1. package com.mkyong.web.location; 
      2.   
      3. public class ServerLocation { 
      4.   
      5.     private String countryCode; 
      6.     private String countryName; 
      7.     private String region; 
      8.     private String regionName; 
      9.     private String city; 
      10.     private String postalCode; 
      11.     private String latitude; 
      12.     private String longitude; 
      13.   
      14.     //getter and setter methods 
      15.   

      然后我们使用Spring MVC框架中的Jackson对结果进行转换,转换为json,代码如下:

      1. package com.mkyong.web.controller; 
      2.   
      3. import org.codehaus.jackson.map.ObjectMapper; 
      4. import org.springframework.beans.factory.annotation.Autowired; 
      5. import org.springframework.stereotype.Controller; 
      6. import org.springframework.web.bind.annotation.RequestMapping; 
      7. import org.springframework.web.bind.annotation.RequestMethod; 
      8. import org.springframework.web.bind.annotation.RequestParam; 
      9. import org.springframework.web.bind.annotation.ResponseBody; 
      10. import org.springframework.web.servlet.ModelAndView; 
      11.   
      12. import com.mkyong.web.location.ServerLocation; 
      13. import com.mkyong.web.location.ServerLocationBo; 
      14.   
      15. @Controller 
      16. public class MapController { 
      17.   
      18.     @Autowired 
      19.     ServerLocationBo serverLocationBo; 
      20.   
      21.     @RequestMapping(value = "/", method = RequestMethod.GET) 
      22.     public ModelAndView getPages() { 
      23.   
      24.         ModelAndView model = new ModelAndView("map"); 
      25.         return model; 
      26.   
      27.     } 
      28.   
      29.     //return back json string 
      30.     @RequestMapping(value = "/getLocationByIpAddress", method = RequestMethod.GET) 
      31.     public @ResponseBody 
      32.     String getDomainInJsonFormat(@RequestParam String ipAddress) { 
      33.   
      34.         ObjectMapper mapper = new ObjectMapper(); 
      35.   
      36.         ServerLocation location = serverLocationBo.getLocation(ipAddress); 
      37.   
      38.         String result = ""; 
      39.   
      40.         try { 
      41.             result = mapper.writeValueAsString(location); 
      42.         } catch (Exception e) { 
      43.   
      44.             e.printStackTrace(); 
      45.         } 
      46.   
      47.         return result; 
      48.   
      49.     } 
      50.   

       

posted @ 2015-05-26 12:53  天大SCS小组  阅读(194)  评论(0编辑  收藏  举报