如果用户在地图范围内,则显示位置;如果没有,请显示地图

时间:2013-04-19 15:40:01

标签: google-maps-api-3 geolocation

我会显示特定位置的地图,并在地图上固定兴趣点。这已经完成了...... 我想要做的是添加以下功能: 如果用户位于此地图的现有边界内,则显示用户当前位置在10处放大并且这些引脚就位。如果用户不在地图的范围内,只需显示带有兴趣点针脚的地图。

我已经包含了一部分代码,它将显示用户位置,无论地图的现有边界是什么,或者只显示没有用户位置的边界。你会看到我一直在玩这个功能而无处可去。

GeoMarker = new GeolocationMarker();
GeoMarker.setCircleOptions({fillColor: '#808080',fillOpacity:0.0,strokeOpacity:0.0});

google.maps.event.addListenerOnce(GeoMarker, 'position_changed', function() {
map.setCenter(this.getPosition());
//map.fitBounds(this.getBounds()); //uncomment to show based on user location regardless of the exsisting bounds of the map
//map.fitBounds(bounds); //uncomment to show ALL the listing Markers within the bounds of the map and not location of the user
//if (map.getZoom() > 1) map.setZoom(13); //this zoom is based on user location
// map.setCenter(this.getPosition());


var listener = google.maps.event.addListener(map, "idle", function() { 
google.maps.event.removeListener(listener); 
});
});

google.maps.event.addListener(GeoMarker, 'geolocation_error', function(e) {
alert('There was an error obtaining your position. Message: ' + e.message);
});
GeoMarker.setMap(map); 
}

1 个答案:

答案 0 :(得分:0)

我不确定,如果我理解你的问题,但是你可以通过google.maps.Map.getBounds()获取当前视口的边界,它返回一个google.maps.LatLngBounds对象。在此调用中,contains() - 方法传递用户的坐标以检查您的用户是否在边界内。