Skip to content

Commit

Permalink
Merge pull request #938 from undertakerme/main
Browse files Browse the repository at this point in the history
user location is being fetched properly
  • Loading branch information
vimistify authored Oct 30, 2024
2 parents b04d615 + 15a97b0 commit 3d52b87
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 38 deletions.
5 changes: 2 additions & 3 deletions user-map.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ <h1>Hospitals</h1>

service = new google.maps.places.PlacesService(map);
searchNearby(userLocation);

}, error => {
console.error('Error getting user location: ' + error.message);
}, () => {
console.error('Error getting user location.');
});
} else {
console.error('Geolocation is not supported by this browser.');
Expand Down
76 changes: 41 additions & 35 deletions user.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,9 @@
<div class="container">
<h1 class="text-4xl font-extrabold text-stone-900">Nearby Hospitals</h1>
<div id="hospitalList">
<div class="hospital-item">
<h3>Hospital Name</h3>
<p><strong>Address:</strong> Address</p>
<p><strong>Rating:</strong> 4.5</p>
</div>
<!-- Hospitals will be populated here -->
</div>
<button id="viewMapBtn" class="">View on Map</button>
<button id="viewMapBtn">View on Map</button>
</div>

<script>
Expand All @@ -29,40 +25,50 @@ <h3>Hospital Name</h3>
lat: position.coords.latitude,
lng: position.coords.longitude
};

const service = new google.maps.places.PlacesService(document.createElement('div'));
const request = {
location: userLocation,
radius: '5000',
type: ['hospital', 'pharmacy']
};

service.nearbySearch(request, (results, status) => {
if (status === google.maps.places.PlacesServiceStatus.OK) {
const hospitalList = document.getElementById('hospitalList');
hospitalList.innerHTML = '';
results.forEach(place => {
const hospitalItem = document.createElement('div');
hospitalItem.className = 'hospital-item';
hospitalItem.innerHTML = `
<h3>${place.name}</h3>
<p><strong>Address:</strong> ${place.vicinity}</p>
<p><strong>Rating:</strong> ${place.rating ? place.rating : 'No rating'}</p>
`;
hospitalList.appendChild(hospitalItem);
});
} else {
console.error('Places request failed due to: ' + status);
}
});
}, error => {
console.error('Error getting user location: ' + error.message);
searchHospitals(userLocation);
}, () => {
console.error('Error getting user location.');
});
} else {
console.error('Geolocation is not supported by this browser.');
}
}

function searchHospitals(location) {
const service = new google.maps.places.PlacesService(document.createElement('div'));
const request = {
location: location,
radius: '5000',
type: ['hospital', 'pharmacy']
};

service.nearbySearch(request, (results, status) => {
const hospitalList = document.getElementById('hospitalList');
hospitalList.innerHTML = ''; // Clear previous list

if (status === google.maps.places.PlacesServiceStatus.OK) {
results.forEach(place => {
const hospitalItem = document.createElement('div');
hospitalItem.className = 'hospital-item';
hospitalItem.innerHTML = `
<h3>${place.name}</h3>
<p><strong>Address:</strong> ${place.vicinity}</p>
<p><strong>Rating:</strong> ${place.rating ? place.rating : 'No rating'}</p>
`;
hospitalList.appendChild(hospitalItem);
});
} else {
hospitalList.innerHTML = `
<div class="hospital-item">
<h3>No hospitals found.</h3>
<p>Please try again later.</p>
</div>
`;
console.error('Places request failed due to: ' + status);
}
});
}

window.onload = fetchHospitalList;

document.getElementById('viewMapBtn').addEventListener('click', () => {
Expand All @@ -71,4 +77,4 @@ <h3>${place.name}</h3>
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCKR3agIMLtauzDhz4fCu3heww0BV_81H4&libraries=places"></script>
</body>
</html>
</html>

0 comments on commit 3d52b87

Please sign in to comment.