capacitor-plugins icon indicating copy to clipboard operation
capacitor-plugins copied to clipboard

geolocation: getCurrentPosition options requires JSON stringify on iOS but not on Android and web.

Open phlegx opened this issue 8 months ago • 0 comments

Bug Report

Plugin(s)

@capacitor/geolocation": "6.0.0"

Capacitor Version

@capacitor/core": "6.0.0"

Platform(s)

iOS

Current Behavior

Using GeoLocation.getCurrentPosition(this.options) with this.options as JS Object works fine on web and Android. On iOS it throws the error: DataCloneError: The object can not be cloned..

Using GeoLocation.getCurrentPosition(JSON.stringify(this.options)) solves the error on iOS.

Expected Behavior

It should work always with a JS Object (Web, Android and iOS).

Additional Context

Using Vue 3 with options from Vue data.

...
data() {
  return {
    options: {
      timeout: 1000,
      maximumAge: 1000,
      enableHighAccuracy: true,
    }
  }
}

Because of the reactivity API (Proxy) of Vue 3 for data, I need to get the raw value of the proxy:

  1. Vue 3 Reactivity utils
import { toRaw } from 'vue'
const rawOptions = toRaw(this.options)
  1. Using JSON stringify/parse
const rawOptions = JSON.parse(JSON.stringify(this.options))
  1. Destructuring
const rawOptions = { ...this.options }
  1. With Object.assign
const rawOptions = Object.assign({}, this.options)

Use one of the possible ways above to get the raw value and then:

GeoLocation.getCurrentPosition(rawOptions)

phlegx avatar Jun 11 '24 12:06 phlegx