2017-01-13 22 views
0

マーカーと場所のジオロケーションにアクセスする際に問題があります。Google Maps API JSでマーカーと場所の位置にアクセス

基本的に地図の中央に位置するユーザーのマーカーを作成しています。次に、geolocation.watchPosition関数を実行しています。ここではuserMarkerの位置を更新しています。したがって、watchPositionで行われるすべてのティックで、userMarkerの位置を現在の位置に更新しています。私はまた、特定の半径内で検索しているplacesと非常によく似たことをやっています。検索は、それぞれの目盛りがwatchPositionで実行され、マーカーはそれぞれplaceに与えられます。

視覚的には、すべてのマーカーが正しい位置にあり、正しい位置に更新されているため、正しい位置にあります。問題は、console.log(userMarker.getPosition())を使用してマーカーの位置をコンソールに表示しようとすると、マーカーのLatLngオブジェクトが返されますが、空の場合は_.E {}のようになります。

私は基本的にちょうど私がマーカーの位置にアクセスするために間違っていることを知りたいのですが、なぜですか?これは、オブジェクトの仕方だけではありません。私が間違っていることに間違いありません。

function updateMap(){ 
    //navigator is passed a callback to handle return position 
    navigator.geolocation.watchPosition(watchPositionCallback); 
} 


/* HELPER FUNCTIONS AND CALLBACKS */ 

// callback for watchPosition() used to handle the returned position data 
// as well as run searches 
function watchPositionCallback(position){ 
    var pos = { 
    lat: position.coords.latitude, 
    lng: position.coords.longitude 
    } 

    //new google maps LatLng object 
    var currentLocation = new google.maps.LatLng(pos.lat, pos.lng); 

    //update request object to be sent to nearBySearch() 
    //with the currentLocation object as the specified location 
    nearByChurchSearchRequest.location = currentLocation; 
    nearByStoreSearchRequest.location = currentLocation; 

    // center the map on the currentLocation object 
    // and update userMarker position 
    map.setCenter(currentLocation); 
    userMarker.setPosition(currentLocation); 

    console.log(userMarker.getPosition()); // returns _.E {} 

    // initialize new PlacesServices 
    placesChurchService = new google.maps.places.PlacesService(map); 
    placesStoreService = new google.maps.places.PlacesService(map); 

    // service methods used to search nearby queries based on the requests 
    placesChurchService.nearbySearch(nearByChurchSearchRequest, searchCallback); 
    placesStoreService.nearbySearch(nearByStoreSearchRequest, searchCallback); 
} 

フルjsのコードはmap.js

PS下github.com/tear727/newMapである:ここで

は、コードの一部である私もgetCurrentLocation()を試してみました、それは」doesnの何も変えない。

答えて

1

userMarker.getPosition(へ

お電話LatLongブログオブジェクトのメソッドを呼び出すには)のLatLngオブジェクトを返します。このオブジェクトの

使用.lat()と.lng()ここでは、私が今見る

var pos = userMarker.getPosition(); 
 
console.log(pos.lat(), pos.lng());

+0

https://developers.google.com/maps/documentation/javascript/reference#LatLng見られるように。あなたはそれを返すためにメソッドを呼び出さなければなりません。静的ではなく....助けてくれてありがとう! – tear728

関連する問題