マーカーと場所のジオロケーションにアクセスする際に問題があります。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の何も変えない。
https://developers.google.com/maps/documentation/javascript/reference#LatLng見られるように。あなたはそれを返すためにメソッドを呼び出さなければなりません。静的ではなく....助けてくれてありがとう! – tear728