How to add city markers to a map of Germany?
Entities are not meant to be typed directly. There is no way to guess the correct format. Instead, type Ctrl-=, type the name of the city in any form you like: Nürnberg or Nuremberg will both work, then hit enter. You'll see this:
If the interpretation is not correct, you can choose another one through the ...
menu. If everything if fine, you can confirm with the ✓ button (though this is not compulsory). Now you are ready to use the entity.
If you look at its full form, you will see that it is
Entity["City", {"Nuremberg", "Bavaria", "Germany"}]
The way you entered it was not correct.
The correct form works fine in GeoMarker
.
GeoGraphics[
GeoMarker[Entity["City", {"Nuremberg", "Bavaria", "Germany"}]]
]
Using an external API
ClearAll[GermanZip2GeoPosition];
GermanZip2GeoPosition[zip_String] := Block[
{response = Quiet@Import[URLBuild[{"https://api.zippopotam.us", "DE", zip}], "RawJSON"]},
If[response =!= $Failed,
Query["places", 1,
GeoPosition[ToExpression@{#["latitude"], #["longitude"]}] &][response]
, Null
]
];
GermanZip2GeoPosition[zip_Integer] := GermanZip2GeoPosition[ToString[zip]];
SetAttributes[GermanZip2GeoPosition, Listable]
Example
GeoGraphics@GeoMarker@GermanZip2GeoPosition@{90402, 90491, 90513, 90518, 90522, 90547, 90552, 90579, 90762, 90768
}
Code and plots done in Mathemathica 11.1.1 on Win7
Function Interpreter
can find the positions:
Interpreter["Location"]["Nürnberg, Germany"]
Interpreter["Location"]["90402, Germany"]
Both versions return a GeoPosition
value.
Starting from a list of ZIP codes, the following works:
plzs := {19258, 57399, 90402}
plzsGermany := Map[ToString[#] <> ", Germany" &, plzs]
positions := Map[Interpreter["Location"][#] &, plzsGermany]
GeoGraphics[
{
GeoStyling["OutlineMap"],
Polygon[Entity["Country", "Germany"]],
GeoMarker[positions]
},
GeoBackground -> Transparent
]
The approach works with city names as well, and should generalize to other countries.
Warning: Applied to long lists of zip codes, this can run a while.