从JavaScript变量映射Google地图坐标 [英] Mapping google maps coordinate from a javascript variable

查看:169
本文介绍了从JavaScript变量映射Google地图坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  var myLatlng = new google.maps.LatLng(37.77493) ,-122.419415); 
var myOptions = {
zoom:15,
center:myLatlng,
mapTypeId:google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById(map_canvas),myOptions);

但是,当我尝试使用下面的代码块(将坐标存储在变量中) 。

  var coordinate =37.77493,-122.419415;这个地图显示为全蓝,带或不带替换功能。 
coordinate = coordinate.replace(''','');
var myLatlng = new google.maps.LatLng(coordinate);
var myOptions = {
zoom:15 ,
center:myLatlng,
mapTypeId:google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById(map_canvas),

任何人都可以告诉我发生了什么问题吗?

LatLng 构造函数带两个数字而不是字符串

  var coordinate =37.77493,-122.419415.split(','); 
var myLatlng = new google.maps.LatLng(coordinates [0],coordinates [1]);


I can successfully generate a google map with the code below:

var myLatlng = new google.maps.LatLng(37.77493, -122.419415);
var myOptions = {
    zoom: 15,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

However when I try to do it with the block of code below (storing the coordinate in a variable). The map comes up as all blue, with or with out the replace function.

var coordinate = "37.77493,-122.419415";
coordinate = coordinate.replace('"','');
var myLatlng = new google.maps.LatLng(coordinate);
var myOptions = {
    zoom: 15,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

Can anyone tell me what's going wrong?

解决方案

It looks like LatLng constructor takes two numbers not a string

var coordinates = "37.77493,-122.419415".split(',');
var myLatlng = new google.maps.LatLng(coordinates[0], coordinates[1]);

这篇关于从JavaScript变量映射Google地图坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆