How old is your dog? If he's young it might be something you have to wait for them to outgrow. Either way, if it's excited peeing, then try to keep him calm while playing with him and avoid getting him too worked up. Him peeing when he's in trouble sounds like submissive peeing, in this case make sure you're not being to rough with him and making him scared. If you're sure that's not the problem then you need to look for ways to build the confidence in your dog so he doesn't feel he needs to pee to show submission.
Hope this helps and I'm sure others will have ideas too!