谷歌地图API第3版:如何在2动态地址中心? [英] Google Maps API v3: How to center on 2 dynamic addresses?
问题描述
我已经花了过去8小时内试图做一些事情extremelly容易。
问题是,我不是很熟悉JavaScript没有谷歌地图API。
所有我想要做的就是找到2个不同地点的中心和accordly放大。
2.地址是dinamically,因为用户输入的信息。
我所有的搜查API v2或为固定的位置结束了。
可能有人请帮助我吗?
我真的AP preciate呢!
非常感谢事先!
<脚本SRC =https://maps.googleapis.com/maps/api/js?sensor=false®ion=BR>< / SCRIPT>
<脚本>
VAR地理$ C $铬;
VAR地图;
VAR的查询=< PHP的echo $ endCercompleto;>中;
VAR QUERY2 =< PHP的echo $ endFescompleto;>中;
函数初始化(){
地理codeR =新google.maps.Geo codeR();
VAR的MapOptions = {
变焦:8,
中心:新google.maps.LatLng(0,0),
mapTypeId设为:google.maps.MapTypeId.ROADMAP
}
地图=新google.maps.Map(的document.getElementById('map_canvas提供')的MapOptions);
codeAddress();
}
功能codeAddress(){
VAR地址=查询;
地理coder.geo code({'地址':地址},功能(结果状态){
如果(状态== google.maps.Geo coderStatus.OK){
VAR的标记=新google.maps.Marker({
地图:地图,
位置:结果[0] .geometry.location,
});
}其他{
警报('地理code不成功,原因如下:'+状态);
}
});
VAR地址2 = QUERY2;
地理coder.geo code({'地址':地址2},功能(结果状态){
如果(状态== google.maps.Geo coderStatus.OK){
VAR MARKER2 =新google.maps.Marker({
地图:地图,
位置:结果[0] .geometry.location,
});
}其他{
警报('地理code不成功,原因如下:'+状态);
}
});
< / SCRIPT>
这应该工作。注意,bounds.extend并在每个地理codeR回调例程的map.fitBounds。你不知道这将首先完成。
概念小提琴证明
<脚本SRC =https://maps.googleapis.com/maps/api/js?region=BR>< / SCRIPT>
<脚本>
VAR地理$ C $铬;
VAR边界=新的google.maps.LatLngBounds();
VAR地图;
VAR的查询=< PHP的echo $ endCercompleto;>中;
VAR QUERY2 =< PHP的echo $ endFescompleto;>中;
函数初始化(){
地理codeR =新google.maps.Geo codeR();
VAR的MapOptions = {
变焦:8,
中心:新google.maps.LatLng(0,0),
mapTypeId设为:google.maps.MapTypeId.ROADMAP
}
地图=新google.maps.Map(的document.getElementById('map_canvas提供')的MapOptions);
codeAddress();
}
功能codeAddress(){
VAR地址=查询;
地理coder.geo code({'地址':地址},功能(结果状态){
如果(状态== google.maps.Geo coderStatus.OK){
bounds.extend(结果[0] .geometry.location);
VAR的标记=新google.maps.Marker({
地图:地图,
位置:结果[0] .geometry.location,
});
map.fitBounds(边界);
}其他{
警报('地理code不成功,原因如下:'+状态);
}
});
VAR地址2 = QUERY2;
地理coder.geo code({'地址':地址2},功能(结果状态){
如果(状态== google.maps.Geo coderStatus.OK){
bounds.extend(结果[0] .geometry.location);
VAR MARKER2 =新google.maps.Marker({
地图:地图,
位置:结果[0] .geometry.location,
});
map.fitBounds(边界);
}其他{
警报('地理code不成功,原因如下:'+状态);
}
});
}
< / SCRIPT>
I've spent the last 8 hours trying to do something extremelly easy.
The problem is that I'm not very familiar with JavaScript neither Google Maps API.
All I want to do is to find the center of 2 different locations and zoom accordly.
The 2 address are dinamically, since the user will input the info.
All my searched ended on API v2 or for fixed locations.
Could someone please help me out?
I really appreciate it!
Thanks a lot in advance!!!
<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>
This should work. Note that the bounds.extend and the map.fitBounds in each of the geocoder callback routines. You don't know which will complete first.
<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第3版:如何在2动态地址中心?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!