如何使用变量来定义新的google.maps.LatLng()
How do I use variable to define new google.maps.LatLng()
我已经读了,读了,读了,但我不能弄清楚这个。我有一个lat数组和一个lng数组。我需要能够将值从数组传递到新的google.maps.LatLng()。看起来这是可行的:
new google.maps.LatLng(lats[0], lngs[0])
但它没有。我想不出该怎样做这件事。帮助吗?
下面是创建数组的方法: <script type="text/javascript">
var lats = [];
var lngs = [];
function initialize() {
var geo = new google.maps.Geocoder;
var address = "<?php echo $arrayAddress; ?>";
geo.geocode({'address':address},function(results, status){
if (status == google.maps.GeocoderStatus.OK) {
lats.push(results[0].geometry.location.lat());
lngs.push(results[0].geometry.location.lng());
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
如果我console.log(lat[0]),我得到预期的结果。
这里我要创建new。google。mapslatlng ():
<script type="text/javascript">
var map;
function initialize() {
var mapOptions = {
zoom: 8,
center: new google.maps.LatLng(lats[0], lngs[0])
};
map = new google.maps.Map(document.getElementById('map-canvas'),mapOptions);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
这是在wordpress的循环之外。地理编码器部分在循环内。我正在对循环中所有帖子的地址进行地理编码,并将坐标移动到该数组中,然后将坐标数组作为标记放置在循环之外的地图上。
var locat = new google.maps.LatLng(34,74);
geocoder = new google.maps.Geocoder();
var mapOptions = {
zoom: 11,
center: locat,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById(divID), mapOptions);
相关文章:
- 单击超链接时,如何使用Google Maps API v3缩放地图
- 使用Google Maps API向标记添加多个字符
- Google Maps API OverlayView()在AngularJS指令中不起作用
- 使用Google Maps API驱动时间多边形
- 使用Google Maps JavaScript API v3和Geocoding API映射多个位置
- 在for循环中使用多维数组设置google.maps.Marker图标
- 可扩展的画布作为Google Maps Javascript API中的覆盖
- 在自动完成中使用Google Maps Places API;API不工作
- google.maps.event.addDomListener(window, 'load', fun
- 根据PHP中数组的长度在Google Maps API中循环遍历标记
- 加载Google MAPS API v3的JSON数据
- 如何使用Dojo引用Google Maps事件中的包含类
- Javascript:Google Maps API:如果地图加载在隐藏容器中,则为空白地图
- 如何在Ionic/Codova应用程序中保护我的Google Maps Javascript V3 API密钥
- 来自JSON数组的Google Maps API生成器
- URL http://maps.google.com/maps?saddr=Start&daddr=end 继续
- Google Maps/Google Places:getPlace 和 getDetails 方法之间有区别吗?
- 如何在像maps.google.com这样的标记上右键添加基本方向
- 谷歌地图:放大/缩小与平移到特定点,如maps.google.com上下文菜单
- maps.google 地理编码 Jquery 调用与国家/地区代码