Mapping google maps coordinate from a javascript variable

巧了我就是萌 提交于 2020-01-03 03:13:11

问题


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?


回答1:


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]);


来源:https://stackoverflow.com/questions/16491113/mapping-google-maps-coordinate-from-a-javascript-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!