MacMost Now 332: iPhone GPS Location With JavaScript

Web developers creating pages specifically for the iPhone can ask for GPS location data on their Web pages with some simply JavaScript. See a basic example that can be used to get the latitude and longitude of the iPhone viewing the Web page.

HTML Source Code:


<script type="text/javascript">

function getGPS() {
	if (navigator.geolocation) {  
		navigator.geolocation.getCurrentPosition(showGPS, gpsError);
	} else {  
		gpsText.innerText = "No GPS Functionality.";  

function gpsError(error) {
	alert("GPS Error: "+error.code+", "+error.message);

function showGPS(position) {
	gpsText.innerText = "Latitude: "+position.coords.latitude+"\nLongitude: "+position.coords.longitude;
	// alternate
	//gpsText.innerHTML = "<a href='"+position.coords.latitude+","+position.coords.longitude+"+(Your+Location)&iwloc=A&z=17'>"+position.coords.latitude+","+position.coords.longitude+"</a>";



<a href=# onclick="getGPS()">Get GPS Data</a>

<div id=gpsText></div>


Comments: 9 Responses to “MacMost Now 332: iPhone GPS Location With JavaScript”

    14 years ago

    Remember to, have you been able to PM me and tell me couple of more thinks about this,
    I am really fan of your blog...gets solved asap.

    david alexander
    14 years ago

    Your blog is really educational �
    keep up the great work!!!!!!

    james casey
    14 years ago

    Hi, I can�t understand how you can include your website in my rss readers.
    Can you Help me, please ...)

      14 years ago

      Not sure what you mean. But RSS is a very different topic than GPS and JavaScript. Perhaps ask your question in more detail at

        13 years ago

        i think he means he wants a rss feed of your site to add on his reader to get the latest updates.

        great posts and info!! especially how to use javascript to connect to the gps to get iphone coordinates

    13 years ago

    Can you please post the longer code showing how to send the GPS location to Google Maps?

      13 years ago

      What do you mean? Different than the code posted under the video?

    Rob Nelson
    13 years ago

    Came across this on Saturday and I thought all my Christmases had come at once. Implemented the link to Google Maps and it works a treat, however (there is always a however!). Via Safari on the iPhone all is well but using an App with a WebView to view the page it goes horribly wrong. Any suggestions?

      13 years ago

      I'd imagine it has to do with permissions. If you are creating an app, the app must ask for permission to get the location. I would consult the SDK documentation for the proper way to get the location in an app.

Comments Closed.