Cara menggunakan get location javascript

        
            

        
        

Contact us

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Fugit, error amet numquam iure provident voluptate esse quasi, veritatis totam voluptas nostrum quisquam eum porro a pariatur accusamus veniam.

Write to us:

We'll write rarely, but with only the best content.


Submit


San Francisco, CA 94126

United States

+ 01 234 567 89

Mon - Fri, 8:00-22:00

[email protected]

[email protected]

        
            

        .map-container-6{
          overflow:hidden;
          padding-bottom:56.25%;
          position:relative;
          height:0;
        }
        .map-container-6 iframe{
          left:0;
          top:0;
          height:100%;
          width:100%;
          position:absolute;
        }
      
        
    

Yang mungkin sedang merencanakan aplikasi yang mempunyai fasilitas tracking. Berikut ini adalah contoh html untuk mendapatkan posisi GPS.

Aplikasi ini menggunakan javascript. Coding utamanya adalah seperti berikut ini:

    
    
    

Jika menggunakan Android pastikan aktif setting untuk Location Acess yang meliputi:
1. Acess to my Location
2. GPS satellites
3. Wi-fi & mobile network location

Javascriptnya dapat didownload di https://code.google.com/p/geo-location-javascript/downloads/list .

Coding lengkapnya adalah:



  
  Posisi GPS


    
    
    

Informasi lebih lengkap silahkan mengunjungi:

1. https://code.google.com/p/geo-location-javascript/
2. http://diveintohtml5.info/geolocation.html
3. https://developers.google.com/maps/documentation/staticmaps/
4. http://www.w3schools.com/html/tryit.asp?filename=tryhtml5_geolocation_map_script
5. http://www.w3schools.com/html/html5_geolocation.asp


Di tulis oleh: admin

Note: As of Chrome 50, the Geolocation API will only work on secure contexts such as HTTPS. If your site is hosted on an non-secure origin (such as HTTP) the requests to get the users location will no longer function.


Using HTML Geolocation

The getCurrentPosition() method is used to return the user's position.

The example below returns the latitude and longitude of the user's position:

Example

Try it Yourself »

Example explained:

  • Check if Geolocation is supported
  • If supported, run the getCurrentPosition() method. If not, display a message to the user
  • If the getCurrentPosition() method is successful, it returns a coordinates object to the function specified in the parameter (showPosition)
  • The showPosition() function outputs the Latitude and Longitude

The example above is a very basic Geolocation script, with no error handling.



Handling Errors and Rejections

The second parameter of the getCurrentPosition() method is used to handle errors. It specifies a function to run if it fails to get the user's location:

Example

function showError(error) {
  switch(error.code) {
    case error.PERMISSION_DENIED:
      x.innerHTML = "User denied the request for Geolocation."
      break;
    case error.POSITION_UNAVAILABLE:
      x.innerHTML = "Location information is unavailable."
      break;
    case error.TIMEOUT:
      x.innerHTML = "The request to get user location timed out."
      break;
    case error.UNKNOWN_ERROR:
      x.innerHTML = "An unknown error occurred."
      break;
  }
}

Try it Yourself »


Location-specific Information

This page has demonstrated how to show a user's position on a map.

Geolocation is also very useful for location-specific information, like:

  • Up-to-date local information
  • Showing Points-of-interest near the user
  • Turn-by-turn navigation (GPS)

The getCurrentPosition() Method - Return Data

The getCurrentPosition() method returns an object on success. The latitude, longitude and accuracy properties are always returned. The other properties are returned if available:

PropertyReturnscoords.latitudeThe latitude as a decimal number (always returned)coords.longitudeThe longitude as a decimal number (always returned)coords.accuracyThe accuracy of position (always returned)coords.altitudeThe altitude in meters above the mean sea level (returned if available)coords.altitudeAccuracyThe altitude accuracy of position (returned if available)coords.headingThe heading as degrees clockwise from North (returned if available)coords.speedThe speed in meters per second (returned if available)timestampThe date/time of the response (returned if available)

Geolocation Object - Other interesting Methods

The Geolocation object also has other interesting methods:

  • watchPosition() - Returns the current position of the user and continues to return updated position as the user moves (like the GPS in a car).
  • clearWatch() - Stops the watchPosition() method.

The example below shows the watchPosition() method. You need an accurate GPS device to test this (like smartphone):