谷歌地图API问题

时间:2013-07-22 16:11:07

标签: google-maps google-maps-api-3 google-maps-api-2

我必须将谷歌地图api代码从v2翻译为v3。我已经尝试过,但它没有用。

这是旧版本代码:

function getQueryVariable(variable){
  var query = window.location.search.substring(1);
  var vars=query.split("&");
  for(var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  }
}

    var map = null;
    var geocoder = null;


    function showAddress(address, year1,year2,year3) {
        map = new GMap2(document.getElementById("map_canvas"));
        geocoder = new google.maps.Geocoder();
            if (geocoder) {
               geocoder.getLatLng(address,function(point) {
                        if (!point) {
                           alert(address + " not found");
                           } else {
                             map.setCenter(point, 11);
                             var marker = new GMarker(point);
                             map.addOverlay(marker);

                             }
                          }
                    );
            }
    }

这就是我写的:

function getQueryVariable(variable) {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i=0;i<vars.length;i++) {
            var pair = vars[i].split("=");
            if(decodeURIComponent(pair[0]) { 
                return pair[1];
            }
        }
    }

var map = null;
var geocoder = null;
function showAddress(address) {
        var map=new google.maps.Map(document.getElementById("map_canvas"));

        geocoder = new google.maps.Geocoder();
        geocoder.geocode(address, function(results, status) {
                           if (status == google.maps.GeocoderStatus.OK) {
                              var center = results[0].geometry.location;
                              map.setCenter(center); 
                              var marker = new google.maps.Marker({
                              map: map,
                              position: center 
                              });
                            } 
                        }
                    }

有人可以告诉我我做错了什么吗?

非常感谢。

马塞罗

1 个答案:

答案 0 :(得分:0)

您的代码中存在语法错误。但主要问题是您需要初始化google.maps.Map对象。目前,google.maps.MapOptions对象中有3个必需选项:

https://developers.google.com/maps/documentation/javascript/reference#MapOptions

center | LatLng | The initial Map center. Required.
mapTypeId | MapTypeId | The initial Map mapTypeId. Required.
zoom | number | The initial Map zoom level. Required.

这对我有用:

function getQueryVariable(variable) {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i=0;i<vars.length;i++) {
            var pair = vars[i].split("=");
            if(decodeURIComponent(pair[0])) { 
                return pair[1];
            }
        }
    }

var map = null;
var geocoder = null;
function showAddress(address) {
  var map=new google.maps.Map(document.getElementById("map_canvas"),{center:new google.maps.LatLng(0,0), zoom:2, mapTypeId : google.maps.MapTypeId.ROADMAP});

  geocoder = new google.maps.Geocoder();
  geocoder.geocode({address:address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      var center = results[0].geometry.location;
      map.setCenter(center);
      if (results[0].geometry.viewport) map.fitBounds(results[0].geometry.viewport)
      else if (results[0].geometry.bounds) map.fitBounds(results[0].geometry.bounds)
      var marker = new google.maps.Marker({
                         map: map,
                         position: center 
                         });
    } else alert("Geocoder returns: " + status); 
  });
}

working example