Spring MVC - Standort anhand der IP-Adresse ermitteln (jQuery + Google Map)

In diesem Tutorial zeigen wir Ihnen, wie Sie mithilfe einer IP-Adresse einen Standort mit den folgenden Technologien finden:
-
Spring MVC Frameworks.
-
jQuery (Ajax Request).
-
GeoLite-Datenbank.
-
Google Karte.
Überprüfen Sie die Abläufe des Lernprogramms
-
Eine Seite mit Texteingabe und Schaltfläche.
-
Geben Sie eine IP-Adresse ein und klicken Sie auf die Schaltfläche.
-
jQuery löst eine Ajax-Anfrage an Spring Controller aus.
-
Spring-Controller-Prozess und geben einen Json-String zurück.
-
jQuery verarbeitet den zurückgegebenen json und zeigt den Standort in Google Map an.
1. Projektverzeichnis
Überprüfen Sie die endgültige Projektverzeichnisstruktur, ein Standard-Maven-Projekt.

Note
Laden Sie die kostenlosen GeoLite-freien Datenbanken -GeoLiteCity.dat herunter und legen Sie sie im Ordnerresources ab.
2. Projektabhängigkeiten
Deklariert Spring-Frameworks, Jackson- und Geoip-API-Abhängigkeiten.
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
Abrufen des Standorts mit der GeoLite-Datenbank.
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
}
Spring Controller, konvertieren Sie dieServerLocation mit derJackson Bibliothek und geben Sie einen JSON-String zurück.
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 Map
Wenn Sie auf die Schaltfläche "Suchen" klicken, löst jQuery eine Ajax-Anfrage aus, verarbeitet die Daten und aktualisiert die Google Map.
map.jsp
Spring MVC + jQuery + Google Map
5. Demo
IP-Adresse: 74.125.135.102

IP-Adresse: 183.81.166.110

Quellcode herunterladen
Laden Sie es herunter -SpringMvc-jQuery-GoogleMap (18 KB)