Populate + Aggregate in Mongoose

I have two Mongoose models: one for transactions and the other one for the tags associated with them. In order to implement some reports, I need aggregate code like this:

Transaction.aggregate([
  { $unwind: '$tags' },
  {
    $group: {
      _id: '$tags',
      amount: {
        $sum: '$amount'
      }
    }
  }
])

This produces output containing _id and amount. Now, I’d like to populate the other fields (e.g. name) from the model, keeping the calculated amount column. Can I do that within a simple populate?

read more

Unable to add multiple markers in a Google Map

I am trying to have a simpler implementation of Google Map in my Android App. I am able to launch Google Map but I am unable to show multiple markers there. If I keep the latitude and longitude values and name of the places hardcoded then things work, but dynamically with variables and String values I am unable to show them.
I have also gone through this link suggested by many over here. But what I actually want is a simpler implementation of it.

Below is the code that I have used in my Main Activity:

public class Map_Activity_With_Fragment extends AppCompatActivity implements OnMapReadyCallback {

private GoogleMap googleMap;
String Origin, Destination, Start_Date, Num_Adult, Num_Infant, Origin_Map, Destination_Map;
Context context;
List<Address> Addr_Origin, Addr_Dest;
double latitude_origin, longitude_origin, latitude_destination, longitude_destination;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    Bundle extras = intent.getExtras();
    Origin = extras.getString(MainActivity.ORIGIN);
    Destination = extras.getString(MainActivity.DESTINATION);
    Start_Date = extras.getString(MainActivity.START_DATE);
    Num_Adult = extras.getString(MainActivity.ADULT);
    Num_Infant = extras.getString(MainActivity.INFANT);
    Origin_Map = extras.getString(MainActivity.ORIGIN_MAP);
    Destination_Map = extras.getString(MainActivity.DESTINATION_MAP);
    context = Map_Activity_With_Fragment.this;
    setTitle("Location Map");
    setContentView(R.layout.activity_map__with__fragment);

    try
    {
        initializeMap();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_map__with__fragment, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onMapReady(GoogleMap googleMap)
{
    AddMarkers(googleMap);
}


@Override
protected void onResume() {
    super.onResume();
    initializeMap();
}

private void initializeMap()
{
    if (googleMap == null) {
        MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }
}

private void AddMarkers(GoogleMap googleMap)
{
    try {
        Geocoder geocoder = new Geocoder(context);
        Addr_Origin = geocoder.getFromLocationName(Origin_Map, 1);
        Addr_Dest = geocoder.getFromLocationName(Destination_Map, 1);
        if (Addr_Origin.size() > 0) {
            latitude_origin = Addr_Origin.get(0).getLatitude();
            longitude_origin = Addr_Origin.get(0).getLongitude();
        }
        if (Addr_Dest.size() > 0) {
            latitude_destination = Addr_Dest.get(0).getLatitude();
            longitude_destination = Addr_Dest.get(0).getLongitude();
        }
        Marker m1 = googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude_origin, longitude_origin)).title(Origin_Map));
        Marker m2 = googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude_destination, longitude_destination)).title(Destination_Map));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

read more

How can I disable a thread with an onClickListener?

I am working on this splash screen test and implemented a click listener to the layout so it can be skipped by clicking on the screen. The problem is that the intent is called twice, because the Loading thread is not properly interrupted when the screen is clicked. What am I doing wrong and how to avoid this error?

public class SplashScreen extends Activity {
    final Thread Loading = new Thread();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    setContentView(R.layout.splashscreen);
    ImageView SplashScreenView = (ImageView) findViewById(R.id.SplashScreenAnimation);
    SplashScreenView.setBackgroundResource(R.drawable.flashscreenanimation);
    AnimationDrawable SplashScreenAnimation = (AnimationDrawable) SplashScreenView.getBackground();
    SplashScreenAnimation.start();
    RelativeLayout OnTouchSkipScreen = (RelativeLayout)findViewById(R.id.SplashScreenView);
    OnTouchSkipScreen.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Loading.interrupt();
            Loading.isInterrupted();
            Intent SplashScreen = new Intent(SplashScreen.this, HomeScreen.class);
            startActivity(SplashScreen);
            overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
            finish();
        }
    });
    Thread Loading = new Thread() {
        public void run() {
            try {
                sleep(2573);
                Intent SplashScreen = new Intent(SplashScreen.this, HomeScreen.class);
                startActivity(SplashScreen);
                overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                finish();
            }
        }
    };
    Loading.start();
}
}

read more

max-width doesn’t work on img on firefox

I’m making a website (http://emosistemas.com/web/) but I have problem with “max-width” in the images.

In google chrome it works fine, but, in firefox (in responsive mode) it doesn’t resize it.

The images that fails are:

  • the image under “Inicio”
  • the 3 images under “Inicio” image

In responsive mobile (less than 500px aprox), the last 3 images works fine because have “width:100%”.

I try to transform into block, inline-block, position relative, specify widht:auto… but any works.

I’m using Firefox 41.0 and the last nightly version.

read more