0

gem gmaps4rails の使用に問題があります。javascript と gmaps4rails を組み合わせて使用​​しています。これが私のコードのサンプルです:

       function handle_locations(position) {

        alert("Length = " + Gmaps.map.markers.length);
        var yourStartLatLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);

        var marker = new google.maps.Marker({position: yourStartLatLng});
        alert("Ola 2");

        if(document.getElementById("map") != null)
          alert("diff");

        Gmaps.map.markers[0] = marker;

        alert("Ola 333");
        //var map = $document.filter('#map');
        //var map = document.getElementById("map");
        //Gmaps.map.add_marker(marker);  
    }

navigator.geolocation.getCurrentPosition(handle_locations);

handle_locations 関数は正常に機能し、現在地を取得できます。問題は、gmaps を使用して作成されたマップにマーカーを追加することです。この関数で地理位置情報マーカーを地図に追加するにはどうすればよいですか?

4

1 に答える 1

1

私は同様の問題を抱えていましたが、これは私にとってはうまくいきました。

lat = position.coords.latitude;
lng = position.coords.longitude;

Gmaps.map.callback = function() {
   Gmaps.map.createMarker({
      Lat: lat,
      Lng: lng, 
      rich_marker: null, 
      marker_picture: ""
   });
}

navigator.geolocation.getCurrentPosition(function(position) { ユーザーからの位置の取得は非同期であり、時間がかかるため、リクエストが成功した後にのみ position.coords.latitude/longitude を使用するようにしてください(おそらくそうしています)。

緯度と経度が正しいことを確認するには、

alert(lat+" "+lng);
于 2012-07-13T12:46:44.760 に答える