Automatically store coordinates and also populate result based on coordinates in php

Hi EE.

Is there anyone know how to get the location of the device accessing my website at that time and save the coordinates to MySQL and also when retrieving data from the database only show results that are around the current searcher's location using php, html, MySQL.

What I want is that I want when someone is searching for something on my site the site will first get the current location of the user ad then search on the Mysql what the user is searching and then only display data that is around that particular location.

Thank you.
LVL 2
Mlungisi NdlelaFounder of MCSIDevelopers also a software developerAsked:
Who is Participating?
 
Scott Fell, EE MVEConnect With a Mentor Developer & EE ModeratorCommented:
While it may not be 100% reliable, you can still do this and have pretty good idea of the general location.  What you will want to do is design your page where you are displaying the location you have detected and allow the user to easily update their own location.

This is not a quick process so I will outline what you need to do and I suggest from there you break down your question into different parts that I will outline.

1) Get the current location.
To do this, you can use any number of API's
https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition
http://ip-api.com/  with example http://ip-api.com/docs/api:json
Some good information on this subject from Google https://developers.google.com/web/fundamentals/native-hardware/user-location/

2) Create a table in your database to capture the IP, session and current geolocation and optionally username.
Make sure to get permission.
This will allow you to track user's locations across sessions if you capture username.

3) Update your table in the background.

4) Display the data and allow the user to update their location by zip code or address etc.

5) Allow your users to "log in" where you capture contact information and link to the table where you capture the session information.

6) Match the users the session table to look up the table of data for your shops, events or whatever data you are capturing.
You can match on postal code or radius.

If you are not familiar how any of these individual steps can be achieved, then best to ask a question for each individual step.
0
 
David FavorConnect With a Mentor Linux/LXD/WordPress/Hosting SavantCommented:
There's no reliable way to do this.

Anyone using a VPN or cell users, which may be passed through various in house VPNs, mean you never have a clue of the geo of any visitor.

This is why Google allows a person to set their geo or zipcode.

Likely best to follow Google's lead + just allow people to flag their geo in your site's search box.
0
 
Mlungisi NdlelaFounder of MCSIDevelopers also a software developerAuthor Commented:
Hi and sorry for the late reply I was out of town.

@David well getting the exact location is not that important and also even if a person uses proxy or VPN it doesn't matter as there will be people who will visit purely whom come to get what they need. But what is important is to get the location of the browser/computer so that the result will be filtered based to that location.

@Scott Thats seems as what I'm looking for but not all off it. The users wont be logging in so I don't have to capture IP's, usernames. There is an app called UBER you can google it, it does something like this when you need a cab you login and it get your current location then find UBER cabs which are around you so I want to have something like that but in my case its the search that I want to filter based on the searchers location. Making google as another example.

When you access google it get your location(State) that it will search or filter search around your state and also down at the end its gives you options to change language to the languages mostly used in that particular state.
0
 
Scott Fell, EE MVEConnect With a Mentor Developer & EE ModeratorCommented:
> There is an app called UBER you can google it,

Being from the U.S. Uber and Lyft are very common and you would have be living in a remote cave to not know about those.

We all may be mixing things up here. Your question has the tag web development but you did mention, "location of the device accessing my website".   There is a distinct difference between using an app on a mobile device and using the web browser on any device be it a desktop, laptop, tablet or phone.   When you are using an app like Uber, you may not have realized that you gave permission for the app to access your phone/tablet's location which is gathered by a combination of GPS and triangulation of Cell towers.  Also, when you fire up your Uber app, you have already logged in. Until you log out of Uber,  you are always logged in. So indeed you have logged in to that app.

When you are accessing your site from a browser, there is an ip that you can access and use a 3rd party service to get an idea of the geo location of that IP.  At least that is the way you can obtain the information without getting permission.  It is not going to be as accurate as what a native app like Uber will provide, but in your case you may just want to know a general idea. There is also a geolocation api https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/Using_geolocation.  Note that when you try the example there, you are prompted for permission.  Again, this will not be as accurate as a native app.

> The users wont be logging in so I don't have to capture IP's, usernames.
Where am  now confused is Uber does capture your userrname and you are logged in when you  use it (You just may have forgot that you are logged in).  The reason I described the method I did is because you are using a browser web application and not a native application like Uber.

Just to summarize, when using a web app from your web browser, I have outlined some ways you can obtain a geolocation. You don't have to log in to use the IP, and if you choose to use the geolocation API, your user will be prompted in the browser for permission. This is not as accurate as using a native app like Uber so you will probably want a way for your users to let you know the location the web app thinks they are in is not the actual location and create some way for your user to update their current location either exact lat/lon or some general area such as postal code or state. Once the user gives you that information, you can store it for the session, in a cookie or in your database.

I hope my explanation clears things up.  Please re-read my previous post and if I have parts of it wrong such as logging in, you can ignore that.
0
 
Mlungisi NdlelaFounder of MCSIDevelopers also a software developerAuthor Commented:
I think you said it well. Thank you. I will try it out and see.
1
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.