在本文中,读者将学习到如何使用Spring MVC框架和jQuery及Google Map,制作一个简单的根据IP位置查找应用。用户可以在页面中输入一个IP地址,然后通过Google Map显示该IP所在的大概地理位置.
我们将用到如下技术:
- Spring MVC frameworks
- jQuery(Ajax Request)
- GeoLite 数据库
- Google Map
-
其中用户的操作步骤如下:
- 用户输入IP地址,然后点提交按钮
- jQuery发出Ajax请求到Spring MVC中的控制器
- 在Spring MVC控制器中处理并返回JSON格式数据
- jQuery处理返回的json数据并通过Google Map展示给用户
Spring MVC+Geo Lite
下面首先编写根据IP查找地理位置的接口,命名为ServerLocationBo.java,代码为:
- package com.mkyong.web.location;
- ublic interface ServerLocationBo {
- ServerLocation getLocation(String ipAddress);
其实现类的代码为:
- 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 is 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;
- }
- }
在上面的这个方法中,在getLocations方法中,加载了GeoLite格式的IP地址库文件GeoLiteCity.dat,并且调用getLocation方法进行IP的查找,.在getLocation方法中,通过GeoLite创建一个LookupService类的实例,其中传入要查询的IP地址库文件,然后再调用其getLocation方法进行查询,这里查询出来的结果构造成serverLocation对象,下面来看下其serverlocation对象的代码:
- 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
- }
然后我们使用Spring MVC框架中的Jackson对结果进行转换,转换为json,代码如下:
- 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;
- }
- }