Closed hayuki closed 7 years ago
There is another way to get the marker's position - you need to call marker.get('position') which will retrieve the position you set on the marker through MarkerOptions. However, that value does not change as the marker is dragged around, so it serves no purpose if you want to capture the marker's new position after it's been dragged somewhere.
marker.getPosition()
is an async method that returns a promise that resolves with a GoogleMapLatLng
object.
@ihadeed I understand that, but what happens is, if I use it as expected - marker.getPosition().then((latlng) => { ... });
I get an error that says "could not find method 'then' of undefined". I think the promise never resolves.
@hayuki I will try to reproduce this issue now and figure out what the problem is.
Ok, now I see what's wrong here.
This is wrong:
marker.on(GoogleMapsEvent.MARKER_DRAG_END).subscribe((marker: Marker) => { ... })
When the marker emits the event, it doesn't return back an object typeof Marker
. It returns back the original Marker object from the actual plugin and not from Ionic Native. So instead of the above, you need to do this:
marker.on(GoogleMapsEvent.MARKER_DRAG_END).subscribe(() => { ... })
This will make marker
refer to the original marker
variable that you had defined earlier (which was produced by Ionic Native.
The signature for marker.getPosition
on the original object is getPosition(callback)
and it doesn't return anything (it returns the LatLng object through the callback).
For reference, the code was tested here: https://github.com/ihadeed/ionic-native-playground/blob/ionic-native-1577/src/pages/google-maps/google-maps.ts#L17-L29
I did what you said and now it works perfectly, thank you! :)
Final working code:
private setUpDraggableMarker(marker: Marker) {
marker.on(GoogleMapsEvent.MARKER_DRAG_END).subscribe(() => {
marker.getPosition().then((pos: LatLng) => {
this.onLocationSelected.emit(pos); /* changes as expected */
this.map.animateCamera({target: pos, zoom: this.defaultZoom, duration: 8});
});
})
}
I got same error.I did what u said.But not solved this type of error.My code is like this.
this.map.on(GoogleMapsEvent.MY_LOCATION_BUTTON_CLICK).subscribe((map: GoogleMap) => {
if (this.isViewLoaded) {
this.map.getCameraPosition().then((camera: CameraPosition) => {
let target: LatLng = <LatLng> camera.target;
this.marker.setPosition(target);
});
}
});
On my case error is : Property 'then' does not exist on type 'CameraPosition
my code :
this.map.on(GoogleMapsEvent.MY_LOCATION_BUTTON_CLICK).subscribe(() => {
if (this.isViewLoaded) {
this.map.getCameraPosition().then((camera) => {
let target: LatLng = <LatLng>camera.target;
this.marker.setPosition(target);
});
}
});
camera = this.map.getCameraPosition();
I'm submitting a ... (check one with "x") [ x ] bug report [ ] feature request
Current behavior: When a marker is added to the map, then dragged to a different position, and the
marker.getPosition()
method is called, instead of returning aPromise<LatLng>
it returns undefined. And I cannot see any other way to get the moved marker's new location. I looked through the codebase here on GitHub and I see thatgetPosition()
isn't really implemented, it only hasreturn;
in the method body. Am I missing something?Expected behavior:
marker.getPosition()
should always return aPromise<LatLng>
Steps to reproduce: Create a map, add a draggable marker. Add a
MARKER_DRAG_END
event on it, and in the success callback of thesubscribe()
callmarker.getPosition()
Related code:
package.json info: