Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9268633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:54:23+00:00 2026-06-18T14:54:23+00:00

I am trying to get the users geolocation via the html5 geolcation api, and

  • 0

I am trying to get the users geolocation via the html5 geolcation api, and i use the following snippet for it:

if (navigator.geolocation) {
    var timeoutVal = 10 * 1000 * 1000;
    navigator.geolocation.getCurrentPosition(
      displayPosition, 
      displayError,
      { enableHighAccuracy: true, timeout: timeoutVal, maximumAge: 0 }
    );
  }
  else {
     // DO SOME STUFF HERE
  }


  function displayPosition(position) {

    // configuration
    var myZoom = 12;
    var myMarkerIsDraggable = true;
    var myCoordsLenght = 6;
    var defaultLat = position.coords.latitude;
    var defaultLng = position.coords.longitude;
    document.getElementById('latitude').value = defaultLat;
    document.getElementById('longitude').value = defaultLng;
    /*
      1. creates the map
      2. zooms
      3. centers the map
      4. sets the map’s type
    */
    var map = new google.maps.Map(document.getElementById('canvas'), {
      zoom: myZoom,
      center: new google.maps.LatLng(defaultLat, defaultLng),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });


    });
    // centers the map on markers coords
    map.setCenter(myMarker.position);
    // adds the marker on the map
    myMarker.setMap(map);
  }

  function displayError(error) {
    var errors = { 
      1: 'Permission denied',
      2: 'Position unavailable',
      3: 'Request timeout'
    };
    alert("Error: " + errors[error.code]);
  }
});

The trouble with the above approach is that, few of the users have found it difficult to use. Few of the times, they have clicked Deny instead of Allow and keep staring on the screen. So from usability point of view, I think a good approach would be:

  1. Ask them for permission.

  2. Wait for 3 seconds, if they click deny or don’t respond, use IP to show the geolcation on the map.

How can I accomplish the second step in my above snippets.
Please let me know, thanks!
However, what would be a better way to handle

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T14:54:24+00:00Added an answer on June 18, 2026 at 2:54 pm

    Here is a script (geolocator.js) I wrote some time ago and updated recently.

    Update: Geolocator v2 is released.

    Features:

    • HTML5 geolocation (by user permission)
    • Location by IP
    • Geocoding (coordinates from address)
    • Reverse Geocoding (address lookup from coordinates)
    • Full address information (street, town, neighborhood, region,
      country, country code, postal code, etc…)
    • Fallback mechanism (from HTML5-geolocation to IP-geo lookup)
    • Watch geographic position
    • Get distance matrix and duration
    • Calculate distance between two geographic points
    • Get timezone information
    • Get client IP
    • Supports Google Loader (loads Google Maps dynamically)
    • Dynamically creates Google Maps (with marker, info window, auto-adjusted zoom)
    • Non-blocking script loading (external sources are loaded on the fly without interrupting page load)

    See a live demo.
    See API documentation.

    Geolocator Example Screenshot

    Usage:

    var options = {
        enableHighAccuracy: true,
        timeout: 6000,
        maximumAge: 0,
        desiredAccuracy: 30, // meters
        fallbackToIP: true, // get location by IP if geolocation fails or rejected
        addressLookup: true, // requires Google API key
        timezone: true, // requires Google API key
        map: "my-map" // creates a Google map. requires Google API key
    };
    geolocator.locate(options, function (err, location) {
        console.log(err || location);
    });
    

    Example Output:

    {
        coords: {
            latitude: 37.4224764,
            longitude: -122.0842499,
            accuracy: 30,
            altitude: null,
            altitudeAccuracy: null,
            heading: null,
            speed: null
        },
        address: {
            commonName: "",
            street: "Amphitheatre Pkwy",
            route: "Amphitheatre Pkwy",
            streetNumber: "1600",
            neighborhood: "",
            town: "",
            city: "Mountain View",
            region: "Santa Clara County",
            state: "California",
            stateCode: "CA",
            postalCode: "94043",
            country: "United States",
            countryCode: "US"
        },
        formattedAddress: "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
        type: "ROOFTOP",
        placeId: "ChIJ2eUgeAK6j4ARbn5u_wAGqWA",
        timezone: {
            id: "America/Los_Angeles",
            name: "Pacific Standard Time",
            abbr: "PST",
            dstOffset: 0,
            rawOffset: -28800
        },
        flag: "//cdnjs.cloudflare.com/ajax/libs/flag-icon-css/2.3.1/flags/4x3/us.svg",
        map: {
            element: HTMLElement,
            instance: Object, // google.maps.Map
            marker: Object, // google.maps.Marker
            infoWindow: Object, // google.maps.InfoWindow
            options: Object // map options
        },
        timestamp: 1456795956380
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use an external site's API to get users IP info (country,
I'm trying to figure out how to get the users information after validating via
I'm currently trying to get users to submit data via PHP/HTML forms and into
I am trying to use javascript to get a user's geolocation so i can
I'm trying to get a users facebook profile pic using the FB api. My
I am trying to get all users that are updated maximum 90 seconds ago:
Ok I am trying to get the users First Name the form gets their
I'm trying to get a count of users who have paid for a service
I'm trying to get a list of users who have authorized my Twitter application.
I am trying to get the zip code of the users current location.I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.