Two NYPD officers were "assassinated" while sitting in their patrol car in Brooklyn Saturday afternoon by a gunman who had vowed online hours earlier to kill cops.

The gunman traveled to New York to carry out his plot after shoot his ex-girlfriend near Baltimore. His online posting said he was avenging the controversial deaths of two black men at police hands in Ferguson, Missouri, and Staten Island.

The officers, Wenjian Liu and Rafael Ramos, 40, were shot execution-style at point-blank range in the ambush attack near the intersection of Myrtle and Tompkins Avenues in Bedford-Stuyvesant at 2:43 p.m., police said.

The killings happened just as the NYPD received word from police in Baltimore County, Maryland, that the gunman, Ismaaiyl Brinsley, 28, had shot his former girlfriend and might be on his way to New York to harm police.

"Two of New York's finest were shot and killed with no warning, no provocation," NYPD Commissioner William Bratton said during a news conference last night at Woodhull Medical Center, where both officers were pronounced dead. "They were quite simply assassinated, targeted for their uniform and for the responsibility they embraced to keep the people of the city safe."

The officers were working a critical-response detail, aimed at combating a crime surge at the Tompkins Houses, a public housing project, when Brinsley walked up to the passenger side of the car, "took a shooting stance," and opened fire, Bratton said.

Ramos was in the driver's seat and Liu in the passenger seat when they were hit, Bratton said.

"They never had the opportunity to draw their weapons," Bratton said. "They may never even have actually seen their ... murderer."

After the shootings, Brinsley fled into a nearby G train subway station and fatally shot himself on the platform as pursuing officers closed in, police said.

The murder weapon, a silver-colored semiautomatic Taurus pistol, was recovered in the subway, police said.

Bratton said investigators believe Brinsley lived in Atlanta and had traveled to suburban Baltimore, Maryland, where he shot his ex-girlfriend Saturday morning.

The gunman posted a photo of a silver handgun on Instagram about three hours before the Brooklyn shooting, writing "I'm Putting Wings on Pigs Today" and using a #ShootThePolice hashtag, a police source said. In another post, he wrote: "They Take 1 Of Ours ... Let's Take 2 of Theirs. This May Be My Final Post," the source said.

The posts also made reference to Eric Garner and Michael Brown, both of whom were killed during encounters with police this year.

Brinsley, whose criminal history included numerous arrests on charges ranging from robbery to carrying a concealed weapon, had also urged his Instagram followers to "burn the Flag" in protest of police violence a few days before killing the officers, the source said.

Detectives were investigating whether he might have belonged to a street gang, the source said.

The ambush shootings of the two officers turned the busy commercial stretch of Bedford-Stuyvesant -- bustling on the last weekend before Christmas -- into a scene of violence and chaos.

Courtney Felix, 23, said he was at a friend's apartment near the Tompkins Houses when he heard the gunman open fire. He looked out the window and saw the two officers struggling to get out of their car, clutching at their wounds before falling to the ground.

"They looked like they were really hurt, they were struggling to get out of their car," Felix said.

Both officers were assigned to the 84th precinct in downtown Brooklyn, but were working in the 79th precinct as part of the special detail.

Liu, formerly a school safety officer who fulfilled his dream of becoming a cop three years ago, had celebrated a birthday just last week, officials said. He married his wife two months ago, officials said.

Ramoshad a wife and 13-year-old-son "who couldn't comprehend what had happened to his father," New York City Mayor Bill de Blasio said at the news conference last night, adding that he and other officials prayed over the bodies of both men at the hospital.

Afterwards, scores of officers lined up and saluted the fallen officers as their bodies were carried out of the hospital.

The mayor called their killings "a particularly despicable act" that "tears at the foundation of our society."

It is an attack on all of us," he continued. "It is in attack on all we hold dear ... on the very concept of decency. Therefore, every New Yorker should feel they were attacked ... the entire city was attacked, by this heinous individual."

Officials also urged New Yorkers to immediately report any threats made against police on social media.

Brinsley's violent spree began around 5:45 a.m. in Owings Mills, Maryland, when he shot and wounded his ex-girlfriend, who has not been identified. Baltimore County police later received information that he was posting anti-police messages on the woman's Instagram account, Bratton said, and at 2:45 p.m. sent a wanted flier to the NYPD and other agencies warning that Brinsley might be headed their way.

It arrived too late for Ramos and Liu.

"Tragically, this was at the same time they [Liu and Ramos] were being murdered," Bratton said.

The killing of the officers followed several weeks of protests in response to a grand jury's decision not to indict NYPD Officer Daniel Pantaleo in the apparent chokehold death of Eric Garner during an arrest on July 17.

The decision in the Garner case came just days after a grand jury in Missouri declined to indict a white police officer in the Aug. 9 shooting death of Michael Brown. That announcement touched off rioting in the St. Louis suburb.

The killing of two officers was at least the third unprovoked attack on NYPD cops in recent months and the first line-of-duty shooting deaths since 2011.

Two rookie NYPD officers from Long Island were injured when a man struck them with a metal hatchet on a Queens street in October. Police at that scene opened fire, killing the attacker and wounding a female bystander. In a separate attack last week, two police lieutenants were assaulted during protests on the Brooklyn Bridge.

With Anthony M. DeStefano, Nicole Fuller, Ted Phillips and Darran Simon