Package: mapsnap
mapsnap - Apple Maps Web Snapshots on Pythonmapsnapgenerates static maps using Apple's MapKit Web Snapshots API>>>frommapsnapimportMapSnap,Annotation,ColorScheme,MarkerStyle>>>map_snapshot=MapSnap(..._key=YOUR_PRIVATE_KEY_PATH,...teamId=YOUR_TEAM_ID,...keyId=YOUR_KEY_ID,...center=[-21.208392,-47.788517],...size="414x200",...colorScheme=ColorScheme.DARK,...)>>>print(map_snapshot.url)https://snapshot.apple-mapkit.com/api/v1/snapshot?teamId=YOUR_TEAM_ID&keyId=YOUR_KEY_ID&center=-21.208392%2C-47.788517&size=414x200&scale=2&colorScheme=dark&lang=en-US&signature=A_DYNAMIC_SIGNATURE_GENERATED_AUTOMATICALLY_BY_THIS_MODULE>>>>>>print(map_snapshot.url)https://snapshot.apple-mapkit.com/api/v1/snapshot?teamId=YOUR_TEAM_ID&keyId=YOUR_KEY_ID&center=-21.208392%2C-47.788517&size=414x200&scale=2&colorScheme=dark&lang=en-US&signature=A_DYNAMIC_SIGNATURE_GENERATED_AUTOMATICALLY_BY_THIS_MODULE>>>Themap_snapshotvariable will now have theurlattribute containing the fully signed path to the static map image that can be accessed over https by any client. The string representation of themap_snapshotreturned instance will always return the map URL as well.If you want to dig deeper on the attributes and parameters of the returned instance you just need to take a look at its representation by either calling it directly on the Python REPL or by calling repr() over it like so:>>>map_snapshotMapSnap(_key=None,teamId=None,keyId=None,center=[-21.208392,-47.788517],z=None,spn=None,size='414x200',scale=2,t=None,colorScheme=<ColorScheme.DARK:'dark'>,poi=None,lang='en-US',annotations=None,overlays=None,referer=None,expires=None,imgs=None)>>>repr(map_snapshot)"MapSnap(_key=None, teamId=None, keyId=None, center=[-21.208392, -47.788517], z=None, spn=None, size='414x200', scale=2, t=None, colorScheme=<ColorScheme.DARK: 'dark'>, poi=None, lang='en-US', annotations=None, overlays=None, referer=None, expires=None, imgs=None)">>>print(repr(map_snapshot))MapSnap(_key=None,teamId=None,keyId=None,center=[-21.208392,-47.788517],z=None,spn=None,size='414x200',scale=2,t=None,colorScheme=<ColorScheme.DARK:'dark'>,poi=None,lang='en-US',annotations=None,overlays=None,referer=None,expires=None,imgs=None)Notice that the_key,teamIdandkeyIdattributed are always nullified by design before returning the instance for security purposes (try to prevent exposure of the key content and IDs).You can also call the.png()method which returns a PNG file object.
The sample below shows how you can fetch the PNG image directly and write it to a file:>>>withopen("test.png","wb")asfp:...fp.write(map_snapshot.png().read())...86737and alas, we have a map image:You can later convert the PNG file to other formats using an image module likePillow.Installing & Updatingmapsnapis available onPyPIand can be installed or updated with:$pipinstall-UmapsnapIf you're having installation problems due to ecdsagmpy2dependency, try installing thelibgmp,libmpfrandlibmpc:(mac)-brew install gmp mpfr libmpc mpc(linux)-sudo apt install -y libgmp-dev libmpfr-dev libmpc-devSetupFirst you need to head to yourApple Developer AccountThen Go toCertificates, Identifiers & ProfilesClick onKeyson the left menuClick the+ Sign next to KeysChoose a name for your new Apple MapKit JS KeyMark theMapKit JScheckbox and Configure itClickContinueClick onRegisterat the top rightDownloadyour key and store itsafely!!! Take Note of your Key IDAfter that youMAYneed to convert your .p8 key to a EC PEM format that python-ecdsa can use.Sometimes the .p8 keys works out of the box. If you're having signing issues, than try the next steps. First try to use the .p8 key you have downloaded directly from apple as the_keyparameter.You can do that using either:$ openssl ec -in __[PATH_TO_ORIGINAL_KEY.p8]__ -out __[PATH_TO_OUTPUT_PEM_KEY.pem]__$ openssl pkcs8 -nocrypt -in __[PATH_TO_ORIGINAL_KEY.p8]__ -out __[PATH_TO_OUTPUT_PEM_KEY.pem]__You need to set the path for the generated .pem as the_keyparameter of theMapSnapclass instanceUsageYou just need to import theMapSnapclass and instance it. you should also import utilities that you will need to use like Annotations, Colors and others.Every time you create a new instance of this class, a signed URL is automatically generated and you can access it directly over the instance.urlattribute or download the PNG from Apple for usage as a regular Python file object by calling the.png()method on the instance itself.Customizing the MapsAdding Annotations (Markers)Default Parameters and AttributesParameterTypeDescription_keystr or bytesThe path for the .p8 or .pem file containing your MapKit JS Key downloaded from Apple Developer Portal.(ex: /opt/myapplekeys/AuthKey_XXXXXXXXXX.p8)This can also be abytesobject containing the raw contents of the key file.teamIdstrYour Apple Developer Team ID (you can view it onhttps://developer.apple.comafter logging in near your company/developer name or by accessing theMembershipoption on the menu)keyIdstrThis ID is shown to you when you create your key and is generally part of the key file name, for instance if the downloaded key file is AuthKey_XYZ712387.p8 your keyId is XYZ712387. (You can double check it by going to your details on the Apple Developer portal)The rest of the parameters are mapped directly fromApple Maps Web Snapshotsdocumentation and can be used as guided there.I have tried to map as much as possible as dataclasses that can be imported from this module itself as shown in examples.If you want to set a value for a parameter, you can check the mapped values currently implemented on/mapsnap/utils.pyRequirementsmapsnaprequires Python 3.7 os higher since it usesdataclasses.Python 3.7 (or higher)ecdsa[gmpy2] (pip install ecdsa[gmpy2])requests (pip install requests)ReferencesOfficial Apple Docs:https://developer.apple.com/documentation/snapshotsAuthorsYgor Lemosygbr@mac.comContributingFollow the PEP8 specificationWrite and Run TestsMake sure to run pre-commit hooksLICENSEThis project is licensed under the MIT License - see theLICENSEfile for details.DISCLAIMERS AND TRADEMARKSApple, Apple Maps, MapKit and iOS are registered trademarks ofApple Inc.The Apple Maps Web Snapshots are currently consideredBeta Softwareand subject to change.MIT LicenseCopyright (c) 2020 Ygor LemosPermission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.