Prince Singh
Prince Singh

Reputation: 5183

Get user current location by envoking his/her phone inbuilt geo location capabilities, not IP

I am building a mobile site in core php for a store which will show its nearest store on the basis of user's current location.

I am Currently getting current location on the basis of user's IP, But it dosnt seems to be accurate. So I want to know if i can envoke inbulit smart phone capabilties to get current location(remember its a mobile site not an app). I tried searching over stackoverflow Didnt get anything related.

Any link/advice in right direction would be great !

Upvotes: 0

Views: 913

Answers (1)

arleslie
arleslie

Reputation: 471

I do not believe you can do this with PHP but HTML5 has built-in features for this.

Use of navigator.geolocation will give you the location of the user/phone with their permission.

navigator.geolocation.getCurentPosition(success, failure[, options]);

function success(location){
    <!-- do stuff here if user gave permission -->
    <!-- Latitude: location.coords.latitude -->
    <!-- Longitude: location.coords.longitude -->
    <!-- Accuracy in metters: location.coords.accuracy -->
}

function failure(){
    <!-- User did not give permission, use failback code -->
}

Options is optional but can be very useful. You can set whether you require a high accuracy or not as well how old the location can be.

Upvotes: 2

Related Questions