If you are going to calculate your own shipping in this way, you would have to do some custom coding to get the distance and then calculate the cost by writing your own rule for it, which is something that is not supported, so I can't help you with it.
If there are any of the shipping providers that you can use, we just plug in to their shipping api which takes into consideration the weight and distance.