You need a preposition there to join "a few kilometers" with "the sea", it wouldn't make any sense to say
Your house is within a few kilometers the sea
If that doesn't immediately look wrong to you, imagine it's written "Your house is within a few kilometers, the sea". That last part of the sentence, 'the sea', isn't joined to the rest of the sentence. Now there are two prepositions you could use in this case, of and from.
What's the difference between the two? Well I use from when I'm talking about where I started traveling from, and I use of when I'm talking about the place I arrive at (in this, the house). Here are a few examples:
- I walked three miles from his house (Correct, I know where I started walking)
- I walked three miles of his house (Wrong, I don't know where I arrived at [SEE NOTE])
- I walked three miles north of his house (Correct, I know the general direction of where I arrived at)
- I walked to the road which was three miles of his house (Correct, I know where I ended up)
- I walked to the road which was three miles from his house (Correct, I know where I started travelling from)
NOTE: Number 2 is a correct sentence, but it doesn't mean what you think it means. Of in this case is used as a possessive, it means I walked inside his house for three miles. The miles are owned by the house. If your example read "I swam a few kilometers of the sea" it would mean that you were in the sea and you moved three miles. this is a completely different use of of, so please don't get confused!