似乎无法将JS参数(变量)传递给Google Map API v3函数

时间:2011-05-29 22:26:34

标签: javascript google-maps variables google-maps-api-3

我正在尝试将一大块Google Maps API脚本移出页面并进入包含的.js文件。

但是!为了做到这一点,我必须调整函数来接受传递的变量。

这是脚本的正常开始......

<script type="text/javascript">
//<![CDATA[
function loadGmap() {
  var map1options = {
    center: new google.maps.LatLng(-12.043333,-77.028333),
    mapTypeId: google.maps.MapTypeId.TERRAIN,
    zoom: 4,
    minZoom: 2,
    streetViewControl: false
  };

这是我尝试传递第一个变量(并失败)......

<script type="text/javascript">
//<![CDATA[
function loadGmaps() {
  loadGmap('-12.043333,-77.028333');
}

function loadGmap(coordinates) {
  var map1options = {
    center: new google.maps.LatLng(coordinates),
    mapTypeId: google.maps.MapTypeId.TERRAIN,
    zoom: 4,
    minZoom: 2,
    streetViewControl: false
  };

想法?也许这不应该是一个字符串...或者什么?

4 个答案:

答案 0 :(得分:3)

center: new google.maps.LatLng(-12.043333,-77.028333),

你将两个参数传递给LatLng。在新函数中,您仍需要将两个参数传递给LatLng。最简单的方法是接受函数的两个参数:

function loadGmaps() {
    loadGmap(-12.043333,-77.028333);
}

function loadGmap(lat, lng) {
    var map1options = {
        center: new google.maps.LatLng(lat, lng),
        mapTypeId: google.maps.MapTypeId.TERRAIN,
        zoom: 4,
        minZoom: 2,
        streetViewControl: false
    };
}

答案 1 :(得分:1)

你需要传递相同的参数......该方法接受2个参数:纬度和经度,而不是单个字符串参数。

function loadGmaps() {
    loadGmap(-12.043333, -77.028333);
}

function loadGmap(lat, long) {
    var map1options = {
        center: new google.maps.LatLng(lat, long),
        ...

答案 2 :(得分:1)

更改为此行:

center: new google.maps.LatLng(
  parseFloat(coordinates.split(",")[0]), 
  parseFloat(coordinates.split(",")[1])
),   

答案 3 :(得分:0)

截至最近,您需要按如下方式对LatLng变量进行CAST:

buildinglatlng = new google.maps.LatLng(Number(maplatitude),Number(maplongitude));