我移动地图时搜索

时间:2015-02-13 06:36:44

标签: javascript html5 google-maps-api-3

我在许多城市组织了我的数据库中的完整地址。我使用geocoding将标记放在地图上,并在页面中显示组织列表。然后我尝试使用geolocation找到用户。

我的问题是如何在用户位置附近显示组织,并在用户移动地图时自动更新组织列表(当我说移动地图时我的意思是像airbnb一样移动。)

2 个答案:

答案 0 :(得分:8)

Earth3DMap.com工作期间,我回答了你的问题。

Live demo is here.

不要忘记加载Places API。检查演示的HTML代码。

以下是javascript代码:

var map;
var infowindow;
var service;
var request;
var currentLocation;
var newLat;
var newLng;
var newCurrLocation;
var latlngArray;
var marker;
var markers;
function initialize() {
  map = new google.maps.Map(document.getElementById('map-canvas'), {
    center: new google.maps.LatLng(48.1293954,11.556663), // Munich Germany
    zoom: 15
  });
  getMoveData()
    google.maps.event.addListener(map,'dragend',getMoveData) // All events are here https://google-developers.appspot.com/maps/documentation/javascript/examples/full/map-events
}

function getMoveData() {
    clearMarkers()
    currentLocation = map.getCenter()
    newCurrLocation = currentLocation.toString();
    newCurrLocation = newCurrLocation.replace('(', '');
    newCurrLocation = newCurrLocation.replace(')', '');

    latlngArray = new Array();
    latlngArray = newCurrLocation.split(",")
    for (a in latlngArray) {
            latlngArray[a] = parseFloat(latlngArray[a]);
    }
    newLat = latlngArray[0]
    newLng = latlngArray[1]
    map.setCenter({
        lat : newLat,
        lng : newLng
    });
    showPlaces()
}

function showPlaces(){
   request = {
    location: currentLocation,
    radius: 500,
    types: ['store'] // All types are here: https://developers.google.com/places/documentation/supported_types
  };
  infowindow = new google.maps.InfoWindow();
  service = new google.maps.places.PlacesService(map);
  service.nearbySearch(request, callback);
}
function callback(results, status) {
  if (status == google.maps.places.PlacesServiceStatus.OK) {
    for (var i = 0; i < results.length; i++) {
      createMarker(results[i]);
    }
  }
}
markers = [];
function createMarker(place) {
  var placeLoc = place.geometry.location;
  marker = new google.maps.Marker({
    map: map,
    position: place.geometry.location
  });
  markers.push(marker);

  google.maps.event.addListener(marker, 'click', function() {
    infowindow.setContent(place.name);
    infowindow.open(map, this);
  });
}

function clearMarkers() {
  for (var i = 0; i < markers.length; i++) {
    markers[i].setMap(null);
  }
  markers = [];

}

google.maps.event.addDomListener(window, 'load', initialize);

答案 1 :(得分:2)

我正在努力解决这个问题。

有一个功能getBounds()在地方资料库下)。使用此功能,您始终可以获得活动窗口的范围(latlon)。

以下是一个例子 -

var map;
function initialize()
{
var mapOpt = {
  center:new google.maps.LatLng(51.508742,-0.120850),
  zoom:6,
  mapTypeId:google.maps.MapTypeId.ROADMAP
  };
map=new google.maps.Map(document.getElementById("googleMap"),mapOpt);
}

google.maps.event.addDomListener(window, 'load',initialize);
<script src="http://maps.googleapis.com/maps/api/js"></script>

<button onclick="alert(map.getBounds());">Get bounds</button>
<br><br>
<div id="googleMap"style="width:400px;height:300px;"></div>

更新 -

要在缩放后和拖动地图后添加事件,请查看 -

  1. How can I handle map move end using Google Maps for Android V2?
  2. Google Map Api v3 drag event on map
  3. 认为这会对你有所帮助:)