EasyMapLibrary is a Swift library designed to simplify the use of maps in iOS applications. It provides a range of tools and components for handling user location, performing local searches, and displaying maps with annotations and circles.
- User location permission management.
- Continuous user location updates.
- Address and place search using
MKLocalSearchCompleter
. - Map display with annotations and circles.
- Map camera interaction to focus on specific locations.
- iOS 17.0 or higher.
- Xcode 14 or higher.
To integrate EasyMapLibrary
into your project using Swift Package Manager, add the following dependency to your Package.swift
:
dependencies: [
.package(url: "https://github.com/yourusername/EasyMapLibrary.git", from: "1.0.0")
]
Then add EasyMapLibrary to your target's dependencies:
.target(name: "YourTarget", dependencies: ["EasyMapLibrary"])
You can also go to XCode - Files - Add Package Dependencies and search the library with this link https://github.com/silviaespanagil/EasyMapLibrary
EasyMapLibrary is not yet compatible with Carthage. If you are interested in this option, consider contributing with a Pull Request.
EasyMapLibrary
has three main funcionalities that can be used independently depending on the needs of your project. Remember that in all cases you will have to import EasyMapLibrary
The LocationManager
class is responsible for managing and updating the user's location. It provides methods to request location permissions, start and stop location updates, and handle location updates through a delegate. It can be used independently to get the user's current location and track any changes.
To use the location manager, you must always call .requestLocationPermission()
to be compliant with Apple Guidelines. Remember that you will need to update your project with NSLocationWhenInUseUsageDescription
and NSLocationAlwaysUsageDescription
with an explanation of why you need the user's location.
import EasyMapLibrary
@StateObject private var locationManager = LocationManager()
// Request permission and start updating location
locationManager.requestLocationPermission()
locationManager.startUpdatingLocation()
Here is a list of all available methods in the LocationManager class:
Method | Description |
---|---|
requestLocationPermission() |
Requests permission to access the user's location. |
startUpdatingLocation() |
Starts updating the user's location. |
stopUpdatingLocation() |
Stops updating the user's location. |
onLocationUpdate(handler: @escaping (CLLocationCoordinate2D) -> Void) |
Provides a closure to handle location updates. |
The MapManager class handles map-related functionalities, including updating the map camera position, searching for addresses, suggesting addresses, and managing search results. It integrates with LocationManager to get the user's current location. The MapManager also takes a defaultLocation where your map will have the initial focus.
import EasyMapLibrary
let mapManager = MapManager(defaultLocation: CLLocationCoordinate2D(latitude: 37.7749, longitude: -122.4194))
// Request permission
mapManager.requestLocationPermission()
// Update search query
mapManager.updateSearchQuery("San Francisco")
Here is a list of all available methods in the MapManager class:
Method | Description |
---|---|
updateUserLocation(_ location: CLLocationCoordinate2D?) |
Updates the user's current location. |
updateSelectedCoordinate(_ coordinate: CLLocationCoordinate2D?) |
Updates the selected coordinate on the map. |
updateMapCameraPosition(_ position: MapCameraPosition) |
Updates the camera position of the map. |
setDefaultLocation(to coordinate: CLLocationCoordinate2D) |
Sets a default location for the map. |
requestLocationPermission() |
Requests permission to access the user's location. |
updateSearchQuery(_ query: String) |
Updates the search query for local search. |
search(_ completion: MKLocalSearchCompletion) |
Initiates a search based on the provided completion result. |
searchAddress(for coordinate: CLLocationCoordinate2D) |
Searches for an address based on a coordinate. |
The MapView struct provides a SwiftUI view for displaying maps and integrating map-related functionalities such as location selection and address search. It wraps around MapKit's Map and other components to offer a seamless map viewing experience.
If showSearchField
is set to true, users can input an address, and a sheet with address suggestions will appear at the bottom of the view. Users can click to confirm their selection.
import SwiftUI
import EasyMapLibrary
struct ContentView: View {
@StateObject private var mapManager = MapManager(defaultLocation: CLLocationCoordinate2D(latitude: 37.7749, longitude: -122.4194))
var body: some View {
MapView(mapManager: mapManager, showSearchField: true)
}
}
Here's how the map looks.
![](https://private-user-images.githubusercontent.com/81619759/341354410-010ddb8d-5ff2-4dc5-9bdf-9fb6ffb47622.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjM5OTc0MTcsIm5iZiI6MTcyMzk5NzExNywicGF0aCI6Ii84MTYxOTc1OS8zNDEzNTQ0MTAtMDEwZGRiOGQtNWZmMi00ZGM1LTliZGYtOWZiNmZmYjQ3NjIyLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MTglMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODE4VDE2MDUxN1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTM4NzQ4YWQyMDM0ZmZlNWRmM2E4NWQ2NDhjYjA4MDEzOTY0MGI2MjAxNjhjNzRkZGM1NjdjNWM4ZjVjNDkwNTImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.DCvHI1xfNQAV1mTlMfp1v5agjnWvQtHLdt0ecnuAg30)
Simulator.Screen.Recording.-.iPhone.SE.3rd.generation.-.2024-06-20.at.11.27.42.mp4
- Colour customization
Do you have any idea or request? Catch me in Twitter: @fischesil
EasyMapLibrary is available under the MIT license. See the LICENSE file for more information.