用Objectify查询Geopt

时间:2015-08-10 13:33:24

标签: java google-app-engine objectify geopoints

我最难用Objectify查询GeoPt字段。

我确定有关于GeoPt字段的@Index注释。

但是,以下声明似乎不起作用

ofy().load()
     .type(GeoStat.class)
     .filter("geoPt.latitude >=", neLat)
     .limit(10);

是否可以在Objectify中查询GeoPt字段?

1 个答案:

答案 0 :(得分:5)

数据存储区不支持以这种方式查询@WebServlet("/SessionDemo") public class SessionDemo extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); response.setCharacterEncoding("utf-8"); request.setCharacterEncoding("utf-8"); PrintWriter out = response.getWriter(); HttpSession session = request.getSession(); List<String> list = (List<String>) session.getAttribute("list"); printList(out, list); printForm(out); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); response.setCharacterEncoding("utf-8"); request.setCharacterEncoding("utf-8"); PrintWriter out = response.getWriter(); HttpSession session = request.getSession(); List<String> list = (List<String>) session.getAttribute("list"); if (list == null){ list = new ArrayList<>(); session.setAttribute("list", list); } list.add(request.getParameter("item")); printList(out, list); printForm(out); } private void printList(PrintWriter out, List<String> list) { out.println("current items:"); if (list != null){ out.println("<ul>"); for (String item : list){ out.append("<li>").append(item).println("</li>"); } out.println("</ul>"); }else{ out.println("list is empty"); } out.println("<hr/>"); } private void printForm(PrintWriter out){ out.println( "<form action='./SessionDemo' method='post'>" + "<input type='text' name='item'/>" + "<input type='submit' value='add to list'/>" + "</form>" ); } } 。如果要查询纬度,请单独编制索引(可能在@OnSave方法中写入私有字段)。但是,如果您尝试执行地理空间查询(即包含在矩形中),请注意您不能执行两个不等式过滤器(边界中的纬度,边界中的经度)。

好消息是,谷歌最近添加了直接向数据存储区执行地理空间查询的功能:

https://cloud.google.com/appengine/docs/java/datastore/geosearch

GeoPt

请注意,地理空间搜索数据存储区目前是Alpha版本,对于邀请函已关闭。因此,虽然您可以让地理空间搜索在本地工作,但仍然无法在生产中使用。请参阅:Google App Engine › GeoSpatial Alpha Invite