How can I create a map?

Asked on 2024-07-27

1 search

To create a map using MapKit, you can follow these steps as outlined in the session "Unlock the power of places with MapKit" from WWDC 2024:

  1. Initialize MapKit JS:

    • Add an asynchronous script tag with the MapKit JS source attribute.
    • List your entry point function as the data callback.
    • Use a domain-specific token string for authentication.
    <script async src="https://cdn.apple-mapkit.com/mk/5.x.x/mapkit.js" data-callback="entryPoint" data-token="YOUR_TOKEN_HERE"></script>
    
  2. Create a Map:

    • Define an entry point function that initializes the map.
    • Use the place id to fetch and display the place on the map.
    function entryPoint() {
        const map = new mapkit.Map("map");
        const placeId = "YOUR_PLACE_ID";
        mapkit.search.lookup(placeId, (error, data) => {
            if (error) {
                console.error(error);
                return;
            }
            const place = data.places[0];
            const annotation = new mapkit.MarkerAnnotation(place.coordinate, { title: place.name });
            map.showItems([annotation]);
        });
    }
    
  3. Use Place ID Lookup Tool:

    • Use the Place ID Lookup tool on the Apple Developer website to find the place ID for the location you want to display.
    // Example of using place ID lookup
    const placeId = "YOUR_PLACE_ID";
    
  4. Embed a Map on a Website:

    • Use the Create a Map tool on the developer website to generate HTML for embedding a map.
    <iframe src="https://maps.apple.com/embed?placeId=YOUR_PLACE_ID" width="600" height="450" style="border:0;" allowfullscreen="" loading="lazy"></iframe>
    

For more detailed steps and examples, you can refer to the session chapters:

These chapters provide comprehensive guidance on how to utilize MapKit for creating and displaying maps in your applications.