Four Marines were killed and three people were injured after a gunman opened fire at two military locations.