搜索没有位置的谷歌地图框
Search Google Maps Box without Places
如果我订阅了Google Maps API,但没有订阅Google Places API,那么是否可以从搜索项中找到位置点?
类似于这个CODEPEN的东西(使用位置)
(function() {
var marker;
var map = new google.maps.Map(document.getElementById('map'), {
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var input = document.getElementById('search');
var searchBox = new google.maps.places.SearchBox(input);
google.maps.event.addListener(searchBox, 'places_changed', updateMap);
function updateMap() {
var places = searchBox.getPlaces();
var place = places[0];
var radius = 1000000;
if (marker) { marker.setMap(null); }
marker = new google.maps.Marker({
map: map, title: place.name,
position: place.geometry.location
});
var circle = new google.maps.Circle({
center: marker.position, radius: radius
});
map.fitBounds(circle.getBounds());
}
})();
<input id="search" type="text" placeholder="Search Here">
<h2/>
<div id="map"></div>
您可以使用地理编码服务来实现此目的:
Google Maps API为地理编码和根据用户输入动态反向地理编码。
示例
function initMap() {
var marker;
var map = new google.maps.Map(document.getElementById('map'), {
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var btnSearch = document.getElementById('btnSearch');
addEvent(btnSearch, 'click', function () {
var input = document.getElementById('search');
updateMap(map, input.value);
});
}
function updateMap(map, address) {
var geocoder = new google.maps.Geocoder();
if (geocoder) {
geocoder.geocode({ 'address': address }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
//console.log(results[0].geometry.location);
var result = results[0];
var radius = 1000000;
var marker = new google.maps.Marker({
map: map,
title: result.formatted_address,
position: result.geometry.location
});
var circle = new google.maps.Circle({
center: marker.position,
radius: radius
});
map.fitBounds(circle.getBounds());
}
else {
console.log("Geocoding failed: " + status);
}
});
}
}
function addEvent(element, evnt, funct) {
if (element.attachEvent)
return element.attachEvent('on' + evnt, funct);
else
return element.addEventListener(evnt, funct, false);
}
#map{
width: 640px;
height: 480px;
}
<input id="search" type="text" placeholder="Search Here">
<button id="btnSearch">Search</button>
<h2 />
<div id="map"></div>
<script src="https://maps.googleapis.com/maps/api/js?callback=initMap"
async defer></script>
相关文章:
- 谷歌地图固定位置覆盖
- 在谷歌地图上绘制位置数据库
- 使用ASP.NET将谷歌地图添加到项目中,并从数据库中检索位置
- PHP谷歌地图-位置红色气球没有显示
- 谷歌地图laravel上圆圈的错误位置
- 点击href在谷歌地图中加载位置
- 使用谷歌地图API和phonegap显示当前位置+方向
- 谷歌地图API标记没有标记我的位置
- 在TWebBrowser(谷歌地图)中显示位置,而无需单击按钮
- 谷歌地图位置自动完成被jQuery Mobile弹出窗口阻止
- 具有常规位置类型的谷歌地图位置构造函数
- 返回我之前指定的谷歌地图位置列表
- Jquery谷歌地图位置-获取选择选项
- 更新基于gps坐标的谷歌地图位置
- 谷歌地图位置搜索框:如何重新计算pac-container的位置
- 谷歌地图位置API地址自动完成和显示选择的结果
- 触发谷歌地图位置信息窗口
- MapsApi -谷歌地图位置服务给出一个奇怪的错误
- 更新谷歌地图位置通过javascript
- 初始化多个谷歌地图位置