Reputation: 533
As you know, Google maps program for Mobiles has the ability to find users location (My Location service). I want to know how Google Maps can detect users location using internet mobile, GPRS (Not GPS)? and is it possible to write a program in PHP or other web-based languages that can detect users location when they open that page? I can guess that Google doesn't use users IP in order to determine their location because when I tried to guess my location using my Mobile internet IP a wrong location is obtained.
Upvotes: 2
Views: 55793
Reputation: 738
It works like this. Google maps asks the user if they would like to share their location, based on whether or not their browser (or program using the google maps code) supports and implements the geolocation interface, which you can learn more about here. If the user agrees to share their location, information about local wireless access points is passed along so that google maps (or other services) can make an estimation of the user's location. It is possible to implement this in your programs or web pages. I don't know a whole lot about using google maps within programs, but as for a web-page, here is an example of one written in javascript that will do what you are looking for. I'm sure that you can also accomplish this in PHP.
<!DOCTYPE html>
<html>
<head>
<title>Google Maps JavaScript API v3 Example: Map Geolocation</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="UTF-8">
<link href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css"
rel="stylesheet" type="text/css">
<script type="text/javascript"
src="http://maps.googleapis.com/maps/api/js?sensor=true"></script>
<script type="text/javascript">
var map;
function initialize() {
var myOptions = {
zoom: 6,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map_canvas'),
myOptions);
//this is the piece of code you are interested in!!
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude);
var infowindow = new google.maps.InfoWindow({
map: map,
position: pos,
content: 'Location found using HTML5.'
});
map.setCenter(pos);
}, function() {
handleNoGeolocation(true);
});
} else {
handleNoGeolocation(false);
}
}
function handleNoGeolocation(errorFlag) {
if (errorFlag) {
var content = 'Error: The Geolocation service failed.';
} else {
var content = 'Error: Your browser doesn\'t support geolocation.';
}
var options = {
map: map,
position: new google.maps.LatLng(60, 105),
content: content
};
var infowindow = new google.maps.InfoWindow(options);
map.setCenter(options.position);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map_canvas"></div>
</body>
</html>
Consult the google maps api reference here to see the number of ways in which you can use this. Also, not that the source reference ends in "sensor=true". It is important that you set this value to true in your source if you are using geolocation.
Upvotes: 1
Reputation: 448
In the absence of a GPS fix, a common way to do positioning is to do a reverse lookup of the serving cell ID or of a detected Wi-Fi MAC address. A server-based database exists which gives the geolocation of all known cell sites -- which are uniquely identified by their cell ID, or more precisely, their Cell Global ID (CGI), as well the location of all known Wi-Fi APs. Thus, knowing the CGI or MAC, one can determine one's approximate ("coarse") location by doing a dip into the database over a cellular data collection like GPRS. This how commercial services like SkyHook (which Apple used to use, but no longer uses) work.
This coarse position can be made somewhat more precise by using additional inputs such as signal strengths or digital maps.
Some devices -- like iPhones -- cache this database on-device so as to make coarse-resolution positioning faster. Apple got in trouble for this a few months back and has since changed how this works.
In order for a web page to know the device's location, the device would have to call the appropriate (device-specific) positioning API and then submit it to the web server, which obviously requires some device-side code (not just a browser).
Upvotes: 4
Reputation: 3420
Take a look at the Google Geocoding API and W3 Geolocation API Specification. Then this might be helpful for you to find your answers.
Upvotes: 3