ios 在swiftUI 2中向MapKit添加Map标记

dojqjjoe  于 2022-12-20  发布在  iOS
关注(0)|答案(3)|浏览(205)

我正在尝试使用新的Mapkit SwiftUI view,我可以显示某个地区的Map,但我不知道如何显示Map Marker
这是我的

import SwiftUI
import MapKit

struct ContentView: View {
    @State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 38.8977, longitude: -77.0365), span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5))
    var location1 =  MapMarker(coordinate: CLLocationCoordinate2D(latitude: 38.8977, longitude: -77.0365), tint: .red)
    
    
    var body: some View {
        Map(coordinateRegion: $region, showsUserLocation: true).edgesIgnoringSafeArea(.all)
    }
}

有人知道如何将location1添加到Map吗?我找到了this,但一直无法使其工作

nuypyhwy

nuypyhwy1#

可以按如下方式使用MapPin()或MapMarker():

import SwiftUI
import MapKit

struct Place: Identifiable {
let id = UUID()
let name: String
let latitude: Double
let longitude: Double
var coordinate: CLLocationCoordinate2D {
    CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
  }
}

struct MapView: View {

let places = [
    Place(name: "Position 1", latitude: 31.21, longitude: 120.50),
    Place(name: "Position 2", latitude: 31.210205, longitude: 120.52301),
    Place(name: "Position 3", latitude: 31.230006, longitude: 120.54002)
]

@State private var region = MKCoordinateRegion(
    center: CLLocationCoordinate2D(latitude: 31.21, longitude: 120.50),
    span: MKCoordinateSpan(latitudeDelta: 0.2, longitudeDelta: 0.2)
)

var body: some View {
    Map(coordinateRegion: $region, showsUserLocation: false,  annotationItems: places){ place in
        MapPin(coordinate: place.coordinate)
        //MapMarker(coordinate: place.coordinate)

    }
  }
}
z5btuh9x

z5btuh9x2#

iOS 16.0已弃用Map识别码":使用Map标记Map注记

import Foundation
import MapKit

struct NationalParkLocation: Codable, Identifiable {
    var id = UUID()
    var latitude, longitude: Double
    //Computed Property
    var location: CLLocationCoordinate2D {
        CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
    }
}

import SwiftUI
import MapKit

struct MapView: View {
    
    @State private var region: MKCoordinateRegion = {
        var mapCoordinate = CLLocationCoordinate2D.init(latitude: 6.60, longitude: 16.43)
        var mapZoomLevel = MKCoordinateSpan.init(latitudeDelta: 70.0, longitudeDelta: 70.0)
        var mapRegion = MKCoordinateRegion.init(center: mapCoordinate, span: mapZoomLevel)
        return mapRegion
    }()
    
    let locations: [NationalParkLocation] = [
        NationalParkLocation.init(latitude: -2.3333333, longitude: 34.8333333),
        NationalParkLocation.init(latitude: -23.9883848, longitude: 31.5525515)
    ]
    var body: some View {
        Map(coordinateRegion: $region,showsUserLocation: true ,annotationItems: locations) { item in
            // (A) MARKER: (New Style) (always static)
             MapMarker(coordinate: item.location)
            
            // (B) Custom basic annotation (it could be iteractive)
//            MapAnnotation(coordinate: item.location) {
//                Button {
//                    print("Location is", item.location)
//                } label: {
//                    Image("job")
//                        .resizable()
//                        .scaledToFit()
//                        .frame(width: 50, height: 50)
//                }
//            } // Annotation
        }
    }
}

(A)标记:

(B)自定义基本注解

k4ymrczo

k4ymrczo3#

这是一个简单的演示。在Xcode 12 / iOS 14上测试

import CoreLocation
import MapKit

struct Marker: Identifiable {
    let id = UUID()
    var location: MapMarker
}

struct DemoView: View {

    @State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 38.8977, longitude: -77.0365), span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5))

    let markers = [Marker(location: MapMarker(coordinate: CLLocationCoordinate2D(latitude: 38.8977, longitude: -77.0365), tint: .red))]

    var body: some View {
        Map(coordinateRegion: $region, showsUserLocation: true, 
          annotationItems: markers) { marker in
            marker.location
        }.edgesIgnoringSafeArea(.all)
    }
}

相关问题