按经度和纬度计算距离

时间:2017-03-30 05:26:04

标签: java android sql

我有一个代码来计算从数据库中选择的纬度和经度的距离,执行结果返回错误,它显示:

[53,190]左表达式不是算术表达式。

[193,256]右表达式不是算术表达式

这是我的代码:

@GET
@Path("returnNearbyFriends/{studentid}/{longitude}/{latitude}")
@Produces({"application/json"})
public JsonArray returnNearbyFriends(@PathParam("studentid") Integer studentid, @PathParam("longitude") Double longitude, @PathParam("latitude") Double latitude) {
    TypedQuery q = em.createQuery("select distinct(l.studentid.studentid), l.date, (ACOS(SIN((:latitude * 3.1415) / 180 ) *SIN((l.latitude * 3.1415) / 180 ) +COS((:latitude * 3.1415) / 180 ) * COS((l.latitude) * 3.1415) / 180 ) *COS((:longitude * 3.1415) / 180 - (l.longitude * 3.1415) / 180) * 6380) as Distance "
            + "from Location l "
            + "where l.date in (select max(l.date) FROM Location l group by l.studentid) "
            + "order by l.date;", Location.class);
    q.setParameter("studentid", studentid);
    q.setParameter("longitude", longitude);
    q.setParameter("latitude", latitude);
    List<Object[]> list = q.getResultList();
    JsonArrayBuilder builder = Json.createArrayBuilder();
    for (Object[] obj : list){
        String sid = (String) obj[0];
        String ldate = (String) obj[1];
        String distance = (String) obj[2];
        JsonObject jsObject = Json.createObjectBuilder()
                .add("StudentID", sid)
                .add("LocationDate", ldate)
                .add("Distance", distance)
                .build();
        builder.add(jsObject);             
    }
    JsonArray result = builder.build();
    return result;
}

谁能帮我解决问题?

1 个答案:

答案 0 :(得分:1)

获得当前lat,lon的位置后,您可以检查原点(lat,Lon)的距离

void *