Nick Patel
Nick Patel

Reputation: 113

Add custom Label and Distance in PanoramaView Google StreetView

I integrated Google StreetView in my iOS application. I also added maker into `PanoramaView'.

But now I want to add custom Label and Distance to marker.

How can I calculate and show distance to each marker in streetView?

My code is as follow:

let panoView = GMSPanoramaView(frame: self.view.bounds)
self.view.addSubview(panoView)

panoView.moveNearCoordinate(CLLocationCoordinate2D(latitude: -33.732, longitude: 150.312))
panoView.camera = GMSPanoramaCamera(heading: 180, pitch: -10, zoom: 1)
panoView.delegate = self

let position = CLLocationCoordinate2D(latitude: -33.732, longitude: 150.312)
let marker = GMSMarker(position: position)
marker.panoramaView = panoView

Upvotes: 0

Views: 52

Answers (1)

K.pen
K.pen

Reputation: 343

Use Core Location

import CoreLocation

func distanceFromPanorama(to marker: GMSMarker, panorama: GMSPanorama) -> CLLocationDistance {
    let panoramaLocation = CLLocation(latitude: panorama.coordinate.latitude, longitude: panorama.coordinate.longitude)
    let markerLocation = CLLocation(latitude: marker.position.latitude, longitude: marker.position.longitude)
    return panoramaLocation.distance(from: markerLocation) // returns distance in meters
}

Update Marker Info

func updateMarkerInfo(for marker: GMSMarker, with panorama: GMSPanorama) {
    let distance = distanceFromPanorama(to: marker, panorama: panorama)
    let formattedDistance = String(format: "%.2f meters", distance)
    marker.snippet = "Distance: \(formattedDistance)"
}

Listen to Panorama Changes

extension YourViewController: GMSPanoramaViewDelegate {
    func panoramaView(_ panoramaView: GMSPanoramaView, didMoveTo panorama: GMSPanorama?) {
        guard let panorama = panorama else { return }
        // Assume you have an array of markers
        for marker in markers {
            updateMarkerInfo(for: marker, with: panorama)
        }
    }
}

Make sure to add markers to your panorama view and set the delegate of the panorama view to the view controller.

Upvotes: 0

Related Questions