簡體   English   中英

我如何在 IOS SwiftUI 上更新谷歌地圖相機 position

[英]How can i update google maps camera position on IOS SwiftUI

我在 SwiftUI 項目中實施谷歌地圖 SDK,在這個項目中,我根據我從服務器請求的訂單位置列表綁定標記。

我已經在 map 上綁定了標記,但是現在我想通過標記上的正確縮放來集中地圖,我已經在 android 應用程序版本上使用邊界執行此操作,現在我想在 Z818056DBD7E7CD88848.C 上執行相同操作但是mapView.moveCamera()mapView.animate()都不起作用。

在綁定標記之后,我請求在updateUIView(_ mapView: GMSMapView, context: Context)中更改相機。

編碼:

import SwiftUI
import GoogleMaps
import shared

struct GoogleMapsView: UIViewRepresentable {
    private let MARKER_SIZE = 40
    private let zoom: Float = 15.0
    private let bounds: GMSCoordinateBounds = GMSCoordinateBounds()
    @Binding var mappedOrders: Dictionary<Int32, [OrderDTO]>
    @Binding var filteredOrders: [OrderItem]
    @Binding var showAlert: Bool
    @Binding var wichAlert: Int
    
    func makeUIView(context: Self.Context) -> GMSMapView {
        let camera = GMSCameraPosition.camera(withLatitude: 40.4637, longitude: 3.7492, zoom: 6.0)
        let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
        mapView.delegate = context.coordinator
        return mapView
    }
    
    func updateUIView(_ mapView: GMSMapView, context: Context) {
        
        mapView.clear()
        for (key) in mappedOrders.keys {
            let orderList: [OrderDTO] =  mappedOrders[key]!
            orderList.filter { order in
                return order.deliveryStateString == "rider_assigned" ||
                    order.deliveryStateString == "delivery_in_progress" ||
                    order.deliveryStateString == "pending_rider"
            }.forEach{ order in
                switch order.deliveryState {
                case .deliveryInProgress:
                    if (order.deliveryLocation != nil) {
                        createDeliveryPointMarker(order: order).map = mapView
                    }
                    break
                case .pendingRider:
                    if (order.deliveryLocation != nil) {
                        createDeliveryPointMarker(order: order).map = mapView
                    }
                    if(order.restaurantLocation != nil) {
                        createRestaurantMarker(order: order).map = mapView
                    }
                    break
                case .riderAssigned:
                    if (order.deliveryLocation != nil) {
                        createDeliveryPointMarker(order: order).map = mapView
                    }
                    if(order.restaurantLocation != nil) {
                        createRestaurantMarker(order: order).map = mapView
                    }
                    break
                default:
                    break
                }
                
            }
        }
        let update = GMSCameraUpdate.fit(bounds)
        mapView.animate(with: update)
    }
    private func createDeliveryPointMarker(order: OrderDTO) -> GMSMarker {
        let location = CLLocationCoordinate2DMake(
            order.restaurantLocation!.latitude,
            order.restaurantLocation!.longitude
        )
        
        let marker = GMSMarker(
            position: location
        )
        bounds.includingCoordinate(location)
        marker.title = order.user
        marker.userData = MarkerDataStored(
            order: order,
            isClientMarker: true
        )
        
        let iconName:String
        
        switch order.deliveryState {
        case .pendingRider:
            iconName = "flag_red"
            break
        case .riderAssigned:
            iconName = "flag_blue"
            break
        default:
            iconName = "flag_green"
            break
        }
        
        marker.isTappable = true
        marker.icon = resizeImage(
            image: UIImage(named: iconName)!,
            scaledToSize: CGSize(width: MARKER_SIZE, height: MARKER_SIZE)
        )
        
        return marker
    }
    private func createRestaurantMarker(order: OrderDTO) -> GMSMarker {
        
        let marker = GMSMarker(
            position: CLLocationCoordinate2DMake(
                order.deliveryLocation!.latitude,
                order.deliveryLocation!.longitude
            )
        )
        
        marker.title = order.user
        marker.userData = MarkerDataStored(
            order: order,
            isClientMarker: false
        )
        
        let iconName:String
        
        switch order.deliveryState {
        case .pendingRider:
            iconName = "restaurant_red"
            break
        default:
            iconName = "restaurant_blue"
            break
        }
        
        marker.isTappable = true
        marker.icon = resizeImage(
            image: UIImage(named: iconName)!,
            scaledToSize: CGSize(width: MARKER_SIZE, height: MARKER_SIZE)
        )
        return marker
    }
    
    func makeCoordinator() -> MapsDelegate {
        let delegate = MapsDelegate()
        delegate.onClickMarker = { marker in
            filterByRestaurant(marker: marker)
            wichAlert = OPEN_RESTAURANT_ORDERS
            showAlert = true
        }
        return delegate
    }
    
    private func filterByRestaurant(marker: GMSMarker) {
        let data: MarkerDataStored = marker.userData as! MarkerDataStored
        filteredOrders.removeAll()
        if (data.isClientMarker) {
            filteredOrders.append(OrderItem(order: data.order))
        } else {
            self.mappedOrders[data.order.commercialPremiseId]?.forEach() { order in
                filteredOrders.append(OrderItem(order: order))
            }
        }
    }
}

struct MarkerDataStored {
    let order: OrderDTO
    let isClientMarker: Bool
}

正確的方法是:

更改此行

bounds.includingCoordinate(location)

有了這個

bounds = bounds.includingCoordinate(location)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM