Javascript 如何将lat、lng信息传递到谷歌地图

Javascript 如何将lat、lng信息传递到谷歌地图,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,我只有一个地址,我想在谷歌地图上加亮这个地址 以下是帮助获取lat、lng信息的资源 $location保留位置的地址 function getLatLng(callback){ var geocoder = new google.maps.Geocoder(); var latlng = new Array(2); // get Lat, Lang geocoder.geocode({'address': '<?php echo $location ?&

我只有一个地址,我想在谷歌地图上加亮这个地址

以下是帮助获取lat、lng信息的资源

$location
保留位置的地址

function getLatLng(callback){
    var geocoder = new google.maps.Geocoder();
    var latlng = new Array(2);

    // get Lat, Lang
    geocoder.geocode({'address': '<?php echo $location ?>', 'region': 'UK'}, function(results, status){
         if (status == google.maps.GeocoderStatus.OK) {
               latlng[0] = results[0].geometry.location.lat();
               latlng[1] = results[0].geometry.location.lng();
               callback(latlng); // call the callback function here

          } else {
              console.log( "Unable to find address: " + status );
          }
       });
   }

   var citymap = {};
   var CityCircle;

   citymap['<?php echo $location ?>'] = {
       center: getLatLng(function(latlng){ return latlng; }), 
       population: 2842518
   };

希望这对某人有所帮助

以下是一个我认为你所追求的例子

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">


  <link rel="stylesheet" type="text/css" href="https://developers.google.com/maps/documentation/javascript/examples/default.css">

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>

<script type='text/javascript'>//<![CDATA[ 

var cityCircle;
// Create an object containing LatLng, population.
function loadCities(map) {
  var citymap = {};
  citymap['chicago'] = {
    location: 'chicago',
    population: 2842518
  };
  citymap['london'] = {
    location: 'london',
    population: 4000000
  };


  getLatLng(citymap['chicago'], function(city) { 
      addCityToMap(city, map);
  });

  getLatLng(citymap['london'], function(city) { 
      addCityToMap(city, map);
  });
}

function getLatLng(city, callback){
    var geocoder = new google.maps.Geocoder();
    var latlng = new Array(2);

    // get Lat, Lang
    geocoder.geocode({'address': city.location, 'region': 'UK'}, function(results, status){
        if (status == google.maps.GeocoderStatus.OK) {
            latlng[0] = results[0].geometry.location.lat();
            latlng[1] = results[0].geometry.location.lng();

            city.center = new google.maps.LatLng(latlng[0], latlng[1]);
            callback(city); // call the callback function here
        } else {
            console.log( "Unable to find address: " + status );
        }
   });
}

function addCityToMap(city, map) {
    // Construct the circle for each value in citymap. We scale population by 20.
    var populationOptions = {
      strokeColor: '#FF0000',
      strokeOpacity: 0.8,
      strokeWeight: 2,
      fillColor: '#FF0000',
      fillOpacity: 0.35,
      map: map,
      center: city.center,
      radius: city.population / 20
    };
    cityCircle = new google.maps.Circle(populationOptions);
}

function initialize() {
  var mapOptions = {
    zoom: 4,
    center: new google.maps.LatLng(37.09024, -30.712891),
    mapTypeId: google.maps.MapTypeId.TERRAIN
  };

  var map = new google.maps.Map(document.getElementById('map-canvas'),
      mapOptions);

  loadCities(map);
}

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

</script>


</head>
<body>
  <div id="map-canvas"></div>

</body>


</html>

//  

您也可以传入
位置
作为第一个参数,以便
getLatLng
可以重用。哎呀,很抱歉回调,写这个问题是打字错误,我已经更新了这个问题,正如您所说的
位置
您能再描述一下吗,“那我该怎么办?”jogesh_pi拼凑了一个例子,展示了一种方法。不一定是最好的,只是一个例子
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">


  <link rel="stylesheet" type="text/css" href="https://developers.google.com/maps/documentation/javascript/examples/default.css">

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>

<script type='text/javascript'>//<![CDATA[ 

var cityCircle;
// Create an object containing LatLng, population.
function loadCities(map) {
  var citymap = {};
  citymap['chicago'] = {
    location: 'chicago',
    population: 2842518
  };
  citymap['london'] = {
    location: 'london',
    population: 4000000
  };


  getLatLng(citymap['chicago'], function(city) { 
      addCityToMap(city, map);
  });

  getLatLng(citymap['london'], function(city) { 
      addCityToMap(city, map);
  });
}

function getLatLng(city, callback){
    var geocoder = new google.maps.Geocoder();
    var latlng = new Array(2);

    // get Lat, Lang
    geocoder.geocode({'address': city.location, 'region': 'UK'}, function(results, status){
        if (status == google.maps.GeocoderStatus.OK) {
            latlng[0] = results[0].geometry.location.lat();
            latlng[1] = results[0].geometry.location.lng();

            city.center = new google.maps.LatLng(latlng[0], latlng[1]);
            callback(city); // call the callback function here
        } else {
            console.log( "Unable to find address: " + status );
        }
   });
}

function addCityToMap(city, map) {
    // Construct the circle for each value in citymap. We scale population by 20.
    var populationOptions = {
      strokeColor: '#FF0000',
      strokeOpacity: 0.8,
      strokeWeight: 2,
      fillColor: '#FF0000',
      fillOpacity: 0.35,
      map: map,
      center: city.center,
      radius: city.population / 20
    };
    cityCircle = new google.maps.Circle(populationOptions);
}

function initialize() {
  var mapOptions = {
    zoom: 4,
    center: new google.maps.LatLng(37.09024, -30.712891),
    mapTypeId: google.maps.MapTypeId.TERRAIN
  };

  var map = new google.maps.Map(document.getElementById('map-canvas'),
      mapOptions);

  loadCities(map);
}

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

</script>


</head>
<body>
  <div id="map-canvas"></div>

</body>


</html>