Subercaseaux and Heule showed in https://arxiv.org/abs/2301.09757 (The Packing Chromatic Number of the Infinite Square Grid is 15) that $n=15$ is the smallest positive integer for which there is a map $\varphi:\mathbb Z^2\longrightarrow\lbrace 1,2,\ldots,n\rbrace$ such that $\varphi(u)=\varphi(v)$ implies for $u\not= v$ that $u$ and $v$ are at distance (given by the $l_1$-norm $\parallel (x,y)\parallel_1=\vert x\vert+\vert y\vert$) strictly larger than $\varphi(u)=\varphi(w)$. (I learned this from the Quanta-magazine article https://www.quantamagazine.org/the-number-15-describes-the-secret-limit-of-an-infinite-grid-20230420/ )
On the other hand, no such function (with values in a finite set of positive integers) exists on $\mathbb Z^3$. (Short proof: Since every edge joining two elements of $\mathbb Z^3$ at distance $1$ contains at most a unique vertex in $\varphi^{-1}(\lbrace 1\rbrace)$, the proportion of points in the preimage of $1$ under such a map is bounded above by $1/2$. Distinct elements in the preimage of $2n$ or $2n+1$ are at distance at least $2n+1$. The density of those two preimages is thus bounded above by $1/E(n)$ where $E(n)=1/2(2n+1)(2n^2+2n+3)$ is the value of the Ehrhart polynomial counting the number of integral points in closed balls of radius $n$ for the $l_1$-norm on $\mathbb Z^3$, see entry A1845 of the OEIS. But $2\sum_{n\geq 1}1/E(n)<1/2$.)
In order to get finite values for $\mathbb Z^d$ we can relax the rules leading to the following question:
Given a positive integer $d$, what is the smallest positive integer $n_d$ such that there exists a map $\varphi:\mathbb Z^d\longrightarrow \lbrace 1,2,\ldots,n_d\rbrace$ such that $\varphi(u)=\varphi(w)$ implies that $u$ and $v$ belong either to a common connected monochromatic component of diameter strictly less than $\varphi(u)=\varphi(v)$ or they belong to different connected monochromatic components at distance strictly larger than $\varphi(u)=\varphi(v)$?
Explanations: Connected monochromatic components are maximal subgraphs (with edges given by elements at distance $1$ in $\mathbb Z^d$) in preimages $\varphi^{-1}(\lbrace c\rbrace)$ of constant values.
Remarks:
All numbers $n_d$ are finite. Short proof: We denote by $C_n=\lbrace 0,1,\ldots,n-1\rbrace^d$ the discrete cube of side-lenghth $n-1$ and diameter $d(n-1)$. It can be translated by $k^d$ elements in $n\mathbb Z^d$ in order to cover all vertices of the discrete cube $C_{nk}=\lbrace 0,\ldots,nk-1\rbrace^d$. Colour all these $k^d$ disjoint translates by the set of all $k^d$ consective integers in $\lbrace d(n-1)+1,\ldots,d(n-1)+k^d\rbrace$ and repeat this colouring rule by translating the resulting coloured $C_{kn}$-cube by all elements of $kn\mathbb Z^d$. Two points of the same colour belong then either to a monochromatic connected component (of diameter $d(n-1)$) or they are at distance at least $(k-1)n$. We chose now first an integer $k>d+1$ and then an integer $n>(k^d-d)/(k-d-1)$. We have then $(k-1)n>d(n-1)+k^d$.
An example showing $n_2\leq 8$ (without error on my behalf) is given by the $13\mathbb Z(1,0)+12\mathbb Z(0,1)$ invariant map given by $$ \begin{array}{ccccccccccccc|c} 8&8&8&8&8&6&6&1&7&7&3&3&3&8\\ 8&8&8&8&8&2&1&7&7&7&7&3&1&8\\ 8&8&8&8&8&2&7&7&7&7&7&7&2&8\\ 8&8&8&8&8&1&7&7&7&7&7&7&6&8\\ 6&6&6&5&5&5&1&7&7&7&7&6&6&6\\ 6&6&5&5&5&5&5&4&7&7&1&6&6&6\\ 6&1&3&5&5&5&4&4&4&4&2&2&6&6\\ 2&2&3&3&5&2&2&4&3&3&3&5&1&2\\ 1&4&3&2&1&6&6&1&2&2&5&5&5&1\\ 5&4&4&1&6&6&6&6&1&5&5&5&5&5\\ 4&4&4&6&6&6&6&6&6&1&5&5&5&4\\ 2&4&1&2&6&6&6&6&4&4&1&5&2&2\\ \hline 8&8&8&8&8&6&6&1&7&7&3&3&3&8\\ \end{array}$$ (The first row and column are repeated for easing verification.) Connected islands labelled $a$ have diameter $<a$ and are at mutual distance $>a$. I ignore if there is such a map with values in $\lbrace 1,2,3,4,5,6,7\rbrace$.
The definition can be extended to continuous metric spaces. The resulting optimal numbers for Euclidean spaces $\mathbb R^d$ might be slightly different. I ignore if the resulting numbers remain finite in strongly curved hyperbolic spaces.