ChatGPT said:
Short answer: yes. You can share a visitor’s current location with (and on) your website in two common ways:
1) Quick & simple: link or open Google Maps at the user’s coordinates
Use the Browser Geolocation API to get
lat,lng, then send/store it or link to Google Maps:https://maps.google.com/?q=<lat>,<lng>(orhttps://www.google.com/maps/search/?api=1&query=<lat>,<lng>).Permissions needed: the browser will show a location-access prompt; your page must be HTTPS (localhost is OK).
No Google API key required.
<button id="share">Share my location</button>
<script>
document.getElementById('share').onclick = () => {
if (!navigator.geolocation) return alert('Geolocation not supported.');
navigator.geolocation.getCurrentPosition(
({ coords:{ latitude, longitude }}) => {
// Example: send to your server
fetch('/api/loc', {
method:'POST', headers:{'Content-Type':'application/json'},
body: JSON.stringify({ lat: latitude, lng: longitude })
});
// Or open Google Maps at that point
window.open(`https://www.google.com/maps/search/?api=1&query=${latitude},${longitude}`, '_blank');
},
err => alert('Location failed: ' + err.message),
{ enableHighAccuracy:true, timeout:10000 }
);
};
</script>
2) Show it on your site with a Google Map + marker
Use the Google Maps JavaScript API to render a map and drop a marker at the user’s location (from the Geolocation API).
Permissions needed:
Browser location prompt (you can’t bypass it).
A Google Cloud project with Maps JavaScript API enabled, billing on, and a restricted API key (HTTP referrers).
Comply with Google Maps Platform Terms and attribution rules.
Difficulty: still straightforward for a basic marker; moderate if you add real-time tracking, geofencing, or storing user trails.
<div id="map" style="height:400px"></div>
<script>
function initMap() {
const map = new google.maps.Map(document.getElementById('map'), { zoom: 14, center: {lat:0,lng:0} });
if (!navigator.geolocation) return alert('Geolocation not supported.');
navigator.geolocation.getCurrentPosition(pos => {
const loc = { lat: pos.coords.latitude, lng: pos.coords.longitude };
map.setCenter(loc);
new google.maps.Marker({ position: loc, map });
}, err => alert('Location failed: ' + err.message));
}
</script>
<script async src="https://maps.googleapis.com/maps/api/js?key=YOUR_KEY&callback=initMap"></script>
What you can’t do
Use Google’s personal “Location Sharing” (the feature inside the Google Maps app) via an API—Google doesn’t expose that for websites.
Get location without a user prompt (browsers require explicit consent on HTTPS).
Privacy & compliance checklist
Tell users why you need their location and what you’ll do with it; provide an opt-out.
If you store location, add it to your privacy policy, and consider consent banners (GDPR/CPRA) for precise geolocation.
Secure it in transit (HTTPS) and at rest; avoid over-collecting (e.g., store city/ZIP if precise lat/lng isn’t necessary).
Fallbacks
If the user denies/blocks geolocation, fall back to manual address/ZIP input or IP-based geolocation (coarse, no prompt).
If you tell me which site you want to add this to (WordPress, custom HTML, React, etc.), I’ll drop in the exact code and setup steps for your stack.
