Skip to content

Commit 8a44cf0

Browse files
committed
Fixed async issues in coord resolution promise.
1 parent d1c91b2 commit 8a44cf0

File tree

1 file changed

+15
-39
lines changed

1 file changed

+15
-39
lines changed

src/app/passengers/components/smart/trip-planner/trip-planner.component.ts

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,16 @@ export class TripPlannerComponent implements OnInit {
4242
}
4343

4444
ngOnInit() {
45-
//Set Default Map View
46-
this.setInitialLatitude().then(
47-
() => this.setInitialLongitude().then(
48-
() => {
49-
this.latitude = this.initialLat;
50-
this.longitude = this.initialLng;
51-
this.zoom = 14;
52-
console.log("set lat and lng")
53-
},
54-
)).catch((error) => {
55-
console.log("error occurred");
56-
});
45+
// Set Default Map View
46+
this.setInitialCords().then((cords) => {
47+
this.latitude = cords.lat;
48+
this.longitude = cords.lng;
49+
this.zoom = 14;
50+
console.log('set lat and lng')
51+
})
52+
.catch((error) => {
53+
console.log(error);
54+
});
5755

5856
this.destinationInput = new FormControl();
5957
this.destinationOutput = new FormControl();
@@ -114,37 +112,15 @@ export class TripPlannerComponent implements OnInit {
114112
this.estimatedDistance = this.service.estimatedDistance;
115113
}
116114

117-
setInitialLatitude() {
118-
var promise = new Promise((resolve, reject) => {
119-
if ("geolocation" in navigator) {
120-
navigator.geolocation.getCurrentPosition((position) => {
121-
this.initialLat = position.coords.latitude;
122-
//resolve();
123-
});
124-
}
125-
if (error) {
126-
reject();
127-
} else {
128-
resolve();
129-
}
130-
});
131-
return promise;
132-
}
133-
134-
setInitialLongitude() {
135-
var promise = new Promise((resolve, reject) => {
136-
if ("geolocation" in navigator) {
115+
setInitialCords() {
116+
return new Promise((resolve, reject) => {
117+
if ('geolocation' in navigator) {
137118
navigator.geolocation.getCurrentPosition((position) => {
138-
this.initialLng = position.coords.longitude;
119+
resolve({ lat: position.coords.latitude, lng: position.coords.longitude });
139120
});
140-
}
141-
if (error) {
142-
reject();
143121
} else {
144-
resolve();
122+
reject(new Error('No geolocation found in API.'))
145123
}
146124
});
147-
return promise;
148125
}
149-
150126
}

0 commit comments

Comments
 (0)