谷歌地图API v3:如何以2个动态地址为中心
Google Maps API v3: How to center on 2 dynamic addresses?
在过去的8个小时里,我一直在尝试做一些非常简单的事情。
问题是我不太熟悉JavaScript,也不太熟悉谷歌地图API。
我想做的就是找到两个不同位置的中心并相应地缩放
2地址通常是普通的,因为用户会输入信息。
我的所有搜索都在API v2或固定位置上结束。
有人能帮帮我吗?
我真的很感激!
提前感谢!!!
<script src="https://maps.googleapis.com/maps/api/js?sensor=false®ion=BR"></script>
<script>
var geocoder;
var map;
var query = "<?php echo $endCercompleto; ?>";
var query2 = "<?php echo $endFescompleto; ?>";
function initialize() {
geocoder = new google.maps.Geocoder();
var mapOptions = {
zoom:8,
center: new google.maps.LatLng(0, 0),
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
codeAddress();
}
function codeAddress() {
var address = query;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
});
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
var address2 = query2;
geocoder.geocode( { 'address': address2}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var marker2 = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
});
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
</script>
这应该可以工作。请注意,每个地理编码器回调例程中的bounds.extend和map.fitBounds。你不知道哪个会先完成。
概念验证小提琴
<script src="https://maps.googleapis.com/maps/api/js?region=BR"></script>
<script>
var geocoder;
var bounds = new google.maps.LatLngBounds();
var map;
var query = "<?php echo $endCercompleto; ?>";
var query2 = "<?php echo $endFescompleto; ?>";
function initialize() {
geocoder = new google.maps.Geocoder();
var mapOptions = {
zoom:8,
center: new google.maps.LatLng(0, 0),
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
codeAddress();
}
function codeAddress() {
var address = query;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
bounds.extend(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
});
map.fitBounds(bounds);
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
var address2 = query2;
geocoder.geocode( { 'address': address2}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
bounds.extend(results[0].geometry.location);
var marker2 = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
});
map.fitBounds(bounds);
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
</script>
相关文章:
- 谷歌地图API v3:如何以2个动态地址为中心
- 如何将活动类添加到第一个动态选项卡而不是角度的静态选项卡
- 在页面中滑动多个动态部分时出现问题
- 最好有一个大画布还是最多 100 个动态生成的小画布
- 在AJAX中获取两个动态输入的值
- 只删除javascript中的1个动态元素
- 如何从php中第一个动态生成的第二个下拉列表将数据存储到数据库中
- 创建从单个模板继承的多个动态视图
- Ember.js中单个资源中的多个动态段
- Extjs 4,如何使用单个Json文件为多个动态网格发送多个元数据
- 如何使用jquery/javascript处理来自多个动态创建的锚标记的onclick事件
- 在绑定多个动态 JQGard 时覆盖参数
- 使用 jQuery 添加多个动态值并输出结果
- jQuery 将多个动态图像集中在不同大小的容器中
- 发布多个动态文本框值
- jQuery UI - 多个动态生成的滑块和函数,每个滑块和函数都会发生变化
- 在多个动态添加的表单上初始化 jQuery validate() 函数
- 无法从第二个动态依赖下拉框中获取值
- 比较多个动态生成的下拉列表的选定值
- 使用唯一但不友好的名称更新 10 个动态文本字段