【问题标题】:If the user is within the bounds of the map, show location; if not, show map如果用户在地图范围内,则显示位置;如果没有,请显示地图
【发布时间】:2013-04-19 15:40:00
【问题描述】:

我展示了一个特定位置的地图,地图上固定了兴趣点。这已经实现了…… 我要做的是添加以下功能: 如果用户位于此地图的现有范围内,则显示用户当前位置(放大 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); 
}

【问题讨论】:

    标签: google-maps-api-3 geolocation


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多