geometry.location的参数格式是什么?

时间:2016-04-23 14:28:45

标签: javascript google-maps

这是我的功能,其中包含我需要访问的地点的位置

 function callback() {
  document.getElementById("demo").innerHTML="entered into callback function";
    var addresses = ['x','y','z'];
      for (var i = 0; i < addresses.length; i++) {
        createMarker(addresses[i]);
    }
  }

这是我创建标记的功能

function createMarker(place) {   
document.getElementById("demo").innerHTML+=" entered into createmarker function, location=";
  document.getElementById("demo").innerHTML+=place;
    var placeLoc = place.geometry.location;
    document.getElementById("demo").innerHTML+="placeloc="+placeLoc;
    var marker = new google.maps.Marker({
      map: map,
      position: place.geometry.location
    });
页面中的

,我可以看到

entered into callback function entered into create marker function, location=x

但是我看不到&#34; placeloc =&#34;如果x,y,z应该是geomtery.location工作的特定格式吗?

1 个答案:

答案 0 :(得分:0)

用作MarkerOptions对象中的position属性:

place.geometry.location必须是google.maps.LatLng

google.maps.LatLngLiteral也应该有用。

代码段(使用google.maps.LatLngLiteral):

var addresses = [
  {address: "New York, NY",geometry: {location: {lat: 40.7127837,lng: -74.0059413}}}, 
  {address: "Newark, NJ, USA",geometry: {location: {lat: 40.735657,lng: -74.1723667}}}, 
  {address: "Baltimore, MD, USA",geometry: {location: {lat: 39.2903848,lng: -76.6121893}}}, 
  {address: "Philadelphia, PA, USA",geometry: {location: {lat: 39.9525839,lng: -75.1652215}}}
];
var map;
var bounds = new google.maps.LatLngBounds();

function initialize() {
  map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: new google.maps.LatLng(37.4419, -122.1419),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  callback();

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

function callback() {
  document.getElementById("demo").innerHTML = "entered into callback function<br>";

  for (var i = 0; i < addresses.length; i++) {
    createMarker(addresses[i]);
  }
  map.fitBounds(bounds);
}

function createMarker(place) {
  document.getElementById("demo").innerHTML += " entered into createmarker function, location=";
  document.getElementById("demo").innerHTML += place + "<br>";
  var placeLoc = place.geometry.location;
  document.getElementById("demo").innerHTML += "placeloc=" + placeLoc.lat + "," + placeLoc.lng + "<br>";
  var marker = new google.maps.Marker({
    map: map,
    position: place.geometry.location
  });
  bounds.extend(marker.getPosition());
}
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="demo"></div>
<div id="map_canvas"></div>