简体   繁体   English

如何使用 spring 引导从 HttpServletRequest 获取位置(城市、国家/地区..)?

[英]How can I get the location(city,country.. ) from HttpServletRequest using spring boot?

I want to know the location from httpServletRequest.我想知道来自 httpServletRequest 的位置。 Is there any way to do that?有没有办法做到这一点? I want to use one of the getters methods of httpServletRequest like getRemoteAddr().. Thanks in advance.我想使用 httpServletRequest 的 getter 方法之一,例如 getRemoteAddr().. 在此先感谢。

This looks old post.这看起来很旧。 For upcoming visitors adding this answer.对于即将到来的访客添加此答案。

Try to use httpServletRequest.getLocale().getCountry() this will return country name as string.尝试使用httpServletRequest.getLocale().getCountry()这将返回国家名称作为字符串。

Another option to get more precise values like ISO3 country code, ISO language codes use below methods Pass the HttpServletRequest object to RequestContext(org.springframework.web.servlet.support.RequestContext)获得更精确值的另一种选择,如 ISO3 国家代码、ISO 语言代码使用以下方法将 HttpServletRequest object 传递给 RequestContext(org.springframework.web.servlet.support.RequestContext)

RequestContext requestContext = new RequestContext(httpServletRequest);
String countryISO3 = requestContext.getLocale().getISO3Country();

It has many methods.它有很多方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我怎样才能从纬度和经度得到一个带有国家、城市和街道名称的字符串 - how can i get a string with country, city and street name from Latitude and Longitude 如何使用纬度和经度获取位置名称或城市? - How do I get location name or city using latitude & longitudes? 如何在php中使用mysql通过选定的国家获得城市 - how to get city by selected country using mysql in php 如何使用 JavaScript 从 BING 地图 API 获取地址(城市、州、国家、邮政编码)? - How to get address(city,state, country, zip code) from BING map API using JavaScript? 使用谷歌地图 api,如何从纬度和经度获取国家、state 和城市? - using google maps api, how to get country, state, and city from lat and long? 从经度和纬度获取城市,州,国家 - Get City, State, Country from Latitude and Longitude 如何在不使用地理位置的情况下获取用户所在的城市 - How to get user location city without using geo location 如何使用此处的反向地理编码从所有检索到的数据中获取城市名称? - How can I get the name of the city from all the retrieved data using here reverse-geocoding? 如何从地址解析器中仅获取城市名称? - How can I get only the city name from geocoder? 如何从经纬度点获取城市名称? - How can I get city name from a latitude and longitude point?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM