You should post a minimal and complete code example so we can pin down exactly what the problem is.
Solely based on what you posted, my blind guess would be that you're sending your map data without any size parameter, and trying to compensate for this on the receiver with a heuristic (aka, a hack) that isn't accurate enough. If so, the solution is to send a size parameter.