Spring MVC – IPアドレスを使用して場所を見つける(jQuery + Googleマップ)

このチュートリアルでは、次のテクノロジーを使用して、IPアドレスを使用して場所を見つける方法を示します。
-
Spring MVCフレームワーク。
-
jQuery(Ajaxリクエスト)。
-
GeoLiteデータベース。
-
グーグルマップ。
チュートリアルのフローを確認する
-
テキスト入力とボタンのあるページ。
-
IPアドレスを入力し、ボタンをクリックします。
-
jQueryは、Spring ControllerにAjaxリクエストを送信します。
-
Springコントローラーが処理し、json文字列を返します。
-
jQueryは返されたjsonを処理し、Googleマップに場所を表示します。
1. プロジェクトディレクトリ
最終的なプロジェクトディレクトリ構造、標準のMavenプロジェクトを確認します。

Note
無料のGeoLite無料データベース–GeoLiteCity.datをダウンロードし、resourcesフォルダーに配置します。
2. プロジェクトの依存関係
Springフレームワーク、Jacksonおよびgeoip-apiの依存関係を宣言します。
pom.xml
//...
3.2.2.RELEASE
1.2.10
1.9.10
org.springframework
spring-core
${spring.version}
cglib
cglib
2.2.2
org.springframework
spring-web
${spring.version}
org.springframework
spring-webmvc
${spring.version}
com.maxmind.geoip
geoip-api
${maxmind.geoip.version}
org.codehaus.jackson
jackson-mapper-asl
${jackson.version}
//...
3. Spring MVC + GeoLite
GeoLiteデータベースで場所を取得します。
ServerLocationBo.java
package com.example.web.location;
public interface ServerLocationBo {
ServerLocation getLocation(String ipAddress);
}
ServerLocationBoImpl.java
package com.example.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;
}
}
ServerLocation.java
package com.example.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
}
コントローラーを起動し、ServerLocationをJacksonライブラリーで変換し、json文字列を返します。
MapController.java
package com.example.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.example.web.location.ServerLocation;
import com.example.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;
}
}
4. JSP + jQuery + Googleマップ
検索ボタンがクリックされると、jQueryはAjaxリクエストを実行し、データを処理してGoogleマップを更新します。
map.jsp
Spring MVC + jQuery + Google Map
5. Demo
IPアドレス:74.125.135.102

IPアドレス:183.81.166.110

ソースコードをダウンロード
ダウンロード–SpringMvc-jQuery-GoogleMap(18 KB)