2016-04-23 22 views
-1

これは私がgeometry.locationのパラメータ形式は何ですか?

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 
を見ることができます

しかし、私は "placeloc ="の文章を見ることはできませんし、alspはマップ内のマーカーです。x、y、zはgeomtery.locationが動作する特定のフォーマットであるべきですか? google.maps.LatLngLiteralも動作するはずgoogle.maps.LatLng

ことが

place.geometry.locationニーズ:MarkerOptionsオブジェクトにpositionプロパティとして使用する

+0

あなたのアドレスアレイのメンバーは文字列です。文字列には 'geometry'プロパティはありません。 – geocodezip

+0

大丈夫ですか?だから私はそれを互換性にするために文字列を変換する必要がありますか? –

+0

'.geometry.location'は' google.maps.LatLng'または 'google.maps.LatLngLiteral'である必要があります。あなたの住所はどこから来ますか?あなたの 'createMarker'関数はどこから来ましたか? (GoogleドキュメントのPlaces APIの例のようです)問題を示す[最小、完全、テスト済み、読みやすい例](http://stackoverflow.com/help/mcve)を入力してください。 – geocodezip

答えて

0

。 (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>

関連する問題