Keep your dog under control and not on their lawns.
There, I solved the problem!
If your dog is so out of control that you can’t keep it from going to yards, then fix the training before you take it out in public again.
If the road/sidewalk is too narrow to allow your dog to walk next to you safely and out of traffic, then you need to find another place to walk.
If the dog is not on the lawn (which is legally considered trespassing even if not always enforced by owners or police) then it can’t pee on the lawn.
Keep a shorter leash, get a better “heel” command, or work on boundary training until the dog understands that they aren’t allowed to leave the sidewalk.