Geolocation with Mobile Data

Asked

Viewed 111 times

4

Well, I’m trying to make a project to be used in smartphones, more specifically in Android, but not this Developed on Android !

I’m just using HTML5 and CSS,all Responsive, and I want to put a feature that only worked in Angular(I think)

What I Want:

  1. I wanted to know how I can get Cell Phones through the Mobile data can give the location of the same

    1. or even if you can tell which of the phones is physically closest to a certain designated location (Google Maps or so )

I didn’t put the Code in because I only have the Front End of Application .... Regarding the location, the idea would even be to use the GPS of the mobile phone ... Web page is only made to be used on your phone and not via your computer! To help even more .... Imagine that we connect the two mobile phones in which a route and a final destination are defined, and through the Location of both, one can tell which of the Devices is closest to the Final Destination !

I wish I could-without explaining myself in detail or anyone who does not want me to indicate some kind of website/s that can help me !

1 answer

5


So, since you didn’t give details of your code, I’ll post an explanation at HTML5 which until recently read in the W3C documentation on geolocation.

There are three popular ways to get your position:

IP geolocation It is the method used by most Web browsers on computers. Through whois queries and localization services of IP, will determine the city or region in which you are.

GPRS triangulation Devices connected to a cellular network and without a GPS, or with the GPS turned off, can determine their position by Triangulation of GPRS antennas close. It is much more accurate than the IP-based method, will show in which part of the neighborhood you are.

GPS It is the most accurate method. Under ideal conditions, the margin of error is only 5 meters. Although these are the three most popular ways to solve the problem, they may not be the only ones. Some user agents may use a combination of these methods, or even a new method that will be invented. Therefore, the Geolocation API is agnostic in relation to the method used. There is only one way to turn on and off the "high precision mode", which will have different meaning in each user agent.

Let’s take an example:

function getPosition() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(position, erro);
    }
}

function position(posicao) {
    console.log(posicao.coords.latitude, posicao.coords.longitude);
}

function erro(positionError) {
    console.log(positionError.code);
    if (positionError.code == 1)
        document.getElementById('msg').innerHtml = 'Usuário bloqueou a localização';
}

Where postion is a callback function, which will receive a positioning object.

The method getCurrentPosition receives two other parameters. The first is a function for error handling the second, a configuration object.

Error will receive an object with error details, which will be explained below.

Running the above code, the browser will display:

inserir a descrição da imagem aqui

The user can then choose whether or not to share their position with the site. In addition to the user being able to say no, a lot can go wrong when it comes to getting geolocation. To handle this, you can pass the second parameter to getCurrentPosition, in our case the callback erro.

If something goes wrong, the error function will receive an object positionError, that has the attribute code, which may have one of the following values:

1- Permission denied

The user clicked not to share.

2- Position not available

User agent is disconnected, GPS satellites could not be accessed or some similar error.

3- Timeout

Time out when getting a position. You can set the maximum time when calling getCurrentPosition.

0- Unknown error

Something else prevented the user agent from getting a position. Do not treat the user’s response as an error, in its error handling function, if you get error code 1, please do not bother the user with error messages. He chose not to share his position with the site. Perhaps the best attitude is to do nothing at this time.

Configuration object:

The third parameter of getCurrentPosition is a configuration object, which can have the following properties:

enableHighAccuracy

If true, turn on high-precision mode. Num cellular this can instruct the browser, for example, to use GPS instead of GPRS triangulation.

timeout

The time in milliseconds that the user’s agent will wait for the position before firing a type 3 error.

maximumAge The time, in milliseconds, that the browser can cache the position.

  • It helped a lot... Thank you very much ! Regarding the Code I did not put because I only have the Front End of the Application .... Regarding location, the idea would even use the GPS of the phone ... Since the Web page is made only to be used in Mobile and not through the Computer ! To help even more .... Imagine that we connect the two mobile phones in which a route is set and a final destination, and through the Location of both, we can tell which Device is closest to the Final Destination !

  • @Maniacsaw Yes, you can do this on Mobile phone too!

  • @Maniacsaw, I will edit by adding the third configuration parameter, which speaks of mobile devices.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.