0

嘿,我是谷歌地图 api 的新手,我有一个嵌入式地图,我正在使用地理定位来获取用户的经纬度,然后我正在填补地图 api 中的空白。但是,当我使用生成的纬度和经度时,该地图不起作用,但如果我只输入一个,它就起作用了

非工作代码:

     var map;
  var infowindow;

  function initialize() {

    if (navigator.geolocation)
   {
    navigator.geolocation.getCurrentPosition(showPosition);
    }
    function showPosition(position)
    {

    var latlon = new google.maps.LatLng( position.coords.latitude + "," + position.coords.longitude );

    map = new google.maps.Map(document.getElementById('map'), {
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      center: latlon,
      zoom: 15,
      disableDefaultUI: true
    });

    var request = {
      location: latlon,
      radius: 555,
      types: ['bar']
    };
    infowindow = new google.maps.InfoWindow();
    var service = new google.maps.places.PlacesService(map);
    service.search(request, callback);
  }

  function callback(results, status) {
    if (status == google.maps.places.PlacesServiceStatus.OK) {
      for (var i = 0; i < results.length; i++) {
        createMarker(results[i]);
     }
    }
   }
  }

  function createMarker(place) {
    var placeLoc = place.geometry.location;
    var marker = new google.maps.Marker({
      map: map,
      position: place.geometry.location
    });

    google.maps.event.addListener(marker, 'click', function() {
      alert(place.name);
    });
  }

  google.maps.event.addDomListener(window, 'load', initialize);

工作代码

 var map;
  var infowindow;

  function initialize() {
    var latlon = new google.maps.LatLng(-33.8665433, 151.1956316);

    map = new google.maps.Map(document.getElementById('map'), {
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      center: latlon,
      zoom: 15,
      disableDefaultUI: true
    });

    var request = {
      location: latlon,
      radius: 555,
      types: ['bar']
    };
    infowindow = new google.maps.InfoWindow();
    var service = new google.maps.places.PlacesService(map);
    service.search(request, callback);
  }

  function callback(results, status) {
    if (status == google.maps.places.PlacesServiceStatus.OK) {
      for (var i = 0; i < results.length; i++) {
        createMarker(results[i]);
      }
    }
  }

  function createMarker(place) {
    var placeLoc = place.geometry.location;
    var marker = new google.maps.Marker({
      map: map,
      position: place.geometry.location
    });

    google.maps.event.addListener(marker, 'click', function() {
      alert(place.name);
    });
  }

  google.maps.event.addDomListener(window, 'load', initialize);

如果感谢任何帮助:)

4

1 回答 1

2

替换你的行:

var latlon = new google.maps.LatLng
      (position.coords.latitude + "," + position.coords.longitude);

var latlon = new google.maps.LatLng
      (position.coords.latitude, position.coords.longitude);

问题在于将两个值连接成一个,其中构造函数需要两个参数。

于 2012-10-10T08:35:35.683 回答