private void initBaiduMap() {
    mapView = (MapView) findViewById(R.id.bmapView);
    baiduMap = mapView.getMap();
    baiduMap.setMaxAndMinZoomLevel(18, 13);
    IntentFilter iFilter = new IntentFilter();
    iFilter.addAction(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_ERROR);
    iFilter.addAction(SDKInitializer.SDK_BROADCAST_ACTION_STRING_NETWORK_ERROR);
    receiver = new BaiduReceiver();
    registerReceiver(receiver, iFilter);

    geoCoder = GeoCoder.newInstance();
    geoCoder.setOnGetGeoCodeResultListener(this);

    Intent intent = getIntent();
    intentType = intent.getStringExtra(TYPE);
    initActionBar(R.string.chat_position);
    if (intentType.equals(TYPE_SELECT)) {
      // 选择发送位置
      // 开启定位图层
      baiduMap.setMyLocationEnabled(true);
      baiduMap.setMyLocationConfigeration(
          new MyLocationConfigeration(MyLocationConfigeration.LocationMode.NORMAL, true, null));
      // 定位初始化
      locClient = new LocationClient(this);
      locClient.registerLocationListener(myListener);
      LocationClientOption option = new LocationClientOption();
      option.setProdName("avosim");
      option.setOpenGps(true);
      option.setCoorType("bd09ll");
      option.setScanSpan(1000);
      option.setOpenGps(true);
      option.setIsNeedAddress(true);
      option.setIgnoreKillProcess(true);
      locClient.setLocOption(option);
      locClient.start();
      if (locClient != null && locClient.isStarted()) {
        locClient.requestLocation();
      }
      if (lastLocation != null) {
        // 显示在地图上
        LatLng ll = new LatLng(lastLocation.getLatitude(), lastLocation.getLongitude());
        MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
        baiduMap.animateMapStatus(u);
      }
    } else {
      Bundle b = intent.getExtras();
      LatLng latlng = new LatLng(b.getDouble(LATITUDE), b.getDouble(LONGITUDE)); // 维度在前,经度在后
      baiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(latlng));
      OverlayOptions ooA = new MarkerOptions().position(latlng).icon(descriptor).zIndex(9);
      baiduMap.addOverlay(ooA);
    }
  }
Exemplo n.º 2
0
  /**
   * Retrieves the favorite places of a user
   *
   * <p>POST object must be the user_id as integer
   *
   * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
   *     javax.servlet.http.HttpServletResponse)
   *     <p>TODO : thumbnail-pictures
   */
  @SuppressWarnings("unchecked")
  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    // establish database connection
    Connection conn = dao.connect();

    Statement stmt = null;

    String user_id = null;
    String lat_user = null;
    String lng_user = null;

    // retrieve data from request
    if (request.getParameter("user_id") != null && !request.getParameter("user_id").isEmpty())
      user_id = request.getParameter("user_id");
    if (request.getParameter("lat") != null && !request.getParameter("lat").isEmpty())
      lat_user = request.getParameter("lat");
    if (request.getParameter("lng") != null && !request.getParameter("lng").isEmpty())
      lng_user = request.getParameter("lng");

    // create query
    String search_query =
        " SELECT m.meal_id, m.user_id, m.meal_name, m.meal_time, m.meal_price, u.latitude, "
            + "m.meal_servings, m.meal_bookings, u.longitude, u.street_no, u.postal_code, u.city, u.country, "
            + "m.category, m.rating_count, m.thumbnail "
            + "FROM T_MEALS m "
            + "JOIN T_USERS u on m.USER_ID = u.USER_ID "
            + "JOIN T_FAVORITES F on m.MEAL_ID = F.MEAL_ID "
            + "WHERE f.user_ID = "
            + user_id;

    JSONArray meals = new JSONArray();

    try {
      // query database
      stmt = conn.createStatement();
      ResultSet rs_meals = stmt.executeQuery(search_query);

      // convert result into JSON
      while (rs_meals.next()) {
        JSONObject o = new JSONObject();

        o.put("meal_id", rs_meals.getString("MEAL_ID"));
        o.put("user_id", rs_meals.getString("USER_ID"));
        o.put("name", rs_meals.getString("MEAL_NAME"));
        o.put("price", rs_meals.getString("MEAL_PRICE"));
        o.put("servings", rs_meals.getString("MEAL_SERVINGS"));
        o.put("bookings", rs_meals.getString("MEAL_BOOKINGS"));
        o.put("city", rs_meals.getString("CITY"));
        o.put("street", rs_meals.getString("STREET_NO"));
        o.put("postal_code", rs_meals.getString("POSTAL_CODE"));
        o.put("lat", rs_meals.getString("LATITUDE"));
        o.put("long", rs_meals.getString("LONGITUDE"));
        o.put("country", rs_meals.getString("COUNTRY"));
        o.put("rating", rs_meals.getString("RATING_COUNT"));
        o.put("category", rs_meals.getString("CATEGORY"));
        o.put("time", rs_meals.getString("MEAL_TIME"));

        // convert thumbnail-image to JSON
        String image = null;
        if (rs_meals.getBinaryStream("THUMBNAIL") != null) {
          try {
            InputStream in = rs_meals.getBinaryStream("THUMBNAIL");
            byte[] byteimage = IOUtils.toByteArray(in);
            BASE64Encoder encoder = new BASE64Encoder();
            image = encoder.encodeBuffer(byteimage);
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
        o.put("thumbnail", image);

        Double lat_place = null;
        Double lng_place = null;

        // add distance to user in ##.## km
        if (rs_meals.getString("LATITUDE") != null
            && lat_user != null
            && lng_user != null
            && rs_meals.getString("LONGITUDE") != null) {
          lat_place = Double.valueOf(rs_meals.getString("LATITUDE"));
          lng_place = Double.valueOf(rs_meals.getString("LONGITUDE"));

          float distance =
              gc.distFrom(
                  new Float(Double.valueOf(lat_user)).floatValue(),
                  new Float(Double.valueOf(lng_user)).floatValue(),
                  new Float(lat_place).floatValue(),
                  new Float(lng_place).floatValue());

          o.put("distance", String.valueOf(distance));
        } else {
          o.put("distance", "null");
        }
        meals.add(o);
      }

      response.setStatus(HttpServletResponse.SC_OK);
    } catch (SQLException e) {
      e.printStackTrace();
      response.setStatus(HttpServletResponse.SC_CONFLICT);
    } finally {
      // close connection
      dao.close(conn);
    }

    // send result to client
    String json = meals.toJSONString();
    response.setContentType("text/javascript");
    response.getOutputStream().print(json);
    response.flushBuffer();
  }