js代码的框架如下: 
代码如下: 
 
var map; 
var browserSupport = false; 
var attempts = 0; 
$(document).ready(function () { 
//初始化地图 
    InitMap(); 
//定位 
getLocation(); 
    //定位跟踪 
watchLocation(); 
}); 
function InitMap() { 
/* Set all of the options for the map */ 
var options = { 
}; 
/* Create a new Map for the application */ 
map = new google.maps.Map($('#map')[0], options); 
} 
/* 
* If the W3C Geolocation object is available then get the current 
* location, otherwise report the problem 
*/ 
function getLocation() { 
} 
function watchLocation() { 
} 
/* Plot the location on the map and zoom to it */ 
function plotLocation(position) { 
} 
/* Report any errors using this function */ 
function reportProblem(e) { 
} 
 script> 
InitMap方法就是调用google maps api初始化地图,他需要设置options对象,在调用地图初始化的时候使用。 
代码如下: 
function InitMap() { 
/* Set all of the options for the map */ 
var options = { 
zoom: 4, 
center: new google.maps.LatLng(38.6201, -90.2003), 
mapTypeId: google.maps.MapTypeId.ROADMAP, 
mapTypeControl: true, 
mapTypeControlOptions: { 
style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR, 
position: google.maps.ControlPosition.BOTTOM_CENTER 
}, 
panControl: true, 
panControlOptions: { 
position: google.maps.ControlPosition.TOP_RIGHT 
}, 
zoomControl: true, 
zoomControlOptions: { 
style: google.maps.ZoomControlStyle.LARGE, 
position: google.maps.ControlPosition.LEFT_CENTER 
}, 
scaleControl: true, 
scaleControlOptions: { 
position: google.maps.ControlPosition.BOTTOM_LEFT 
}, 
streetViewControl: true, 
streetViewControlOptions: { 
position: google.maps.ControlPosition.LEFT_TOP 
} 
}; 
/* Create a new Map for the application */ 
map = new google.maps.Map($('#map')[0], options); 
} 
getLocation和watchLocation方法获取定位信息。 
代码如下: 
function getLocation() { 
/* Check if the browser supports the W3C Geolocation API */ 
if (navigator.geolocation) { 
browserSupport = true; 
navigator.geolocation.getCurrentPosition(plotLocation, reportProblem, { timeout: 45000 }); 
} else { 
reportProblem(); 
} 
} 
function watchLocation() { 
/* Check if the browser supports the W3C Geolocation API */ 
if (navigator.geolocation) { 
browserSupport = true; 
navigator.geolocation.watchPosition(plotLocation, reportProblem, { timeout: 45000 }); 
} else { 
reportProblem(); 
} 
} 
成功获取位置信息后,调用plotLocation方法把位置显示在google maps上。 
代码如下: 
function plotLocation(position) { 
attempts = 0; 
var point = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); 
var marker = new google.maps.Marker({ 
position: point 
}); 
marker.setMap(map); 
map.setCenter(point); 
map.setZoom(15); 
} 
demo下载地址:googleMapGeolocation.rar
下载本文