0

次のスクリプトを使用していますが、ユーザーの場所で作成したlat変数とlon変数を渡すのに問題があります。作成したlat変数とlon変数を削除し、手動で0,0を入力すると、デバッグのために正常に機能します...マップは、idがmap_canvasのdivに表示されるはずです。

$(document).ready(function() {
navigator.geolocation.getCurrentPosition(handle_geolocation_query,handle_errors);  
var lat = position.coords.latitude;
var lon = position.coords.longitude;
var latlng = new google.maps.LatLng(lat, lon);
    var myOptions = {
    zoom: 1,
    center: latlng,
    disableDefaultUI: true,
    mapTypeId: google.maps.MapTypeId.TERRAIN
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
    myOptions); map.setTilt(45); 
});  
4

2 に答える 2

1

あなたの問題はおそらくここにあります:

navigator.geolocation.getCurrentPosition(handle_geolocation_query, handle_errors);

おそらく、handle_geolocation_query次のようになります。

var position;
function handle_geolocation_query(pos) {
    position = pos;
}

しかしnavigator.geolocation.getCurrentPosition、非同期関数呼び出しです:

このgetCurrentPosition()メソッドは、1 つ、2 つ、または 3 つの引数を取ります。呼び出されると、すぐに戻り、デバイスの現在の場所を非同期に取得しようとする必要があります。試行が成功した場合、successCallbackを呼び出す必要があります [...]

鉱山を強調します。

したがって、handle_geolocation_queryコールバックは、 を呼び出したgetCurrentPositionときではなく、位置を取得したときに呼び出されますgetCurrentPosition。だから、あなたがここに着いたら:

var lat = position.coords.latitude;
var lon = position.coords.longitude;

必ずしも有用なものが含まれているとは限りませpositionん。おそらく、それposition.coordsが未定義であるか、オブジェクトではないか、または同様のものであるというエラーが表示されます。

便利な値を持つコールバック内にあなたnew google.maps.LatLngnew google.maps.Mapものを移動する必要があります。handle_geolocation_queryposition

于 2011-07-28T07:18:18.000 に答える
1

http://j.maxmind.com/app/geoip.js

http://maps.google.com/maps/api/js?sensor=true

 var ulat;
 var ulon;
 var map;

         if (navigator.geolocation)  {



       navigator.geolocation.getCurrentPosition(handle_geolocation_query,handle_errors);

            }

            else if (!navigator.geolocation)

            {  
             max();  
            } 
             else
            { 
             alert("Sorry user location can't be detected");
              initialize();   
            }   



//maxmind api for fallback
        function max()  
        { 
              ulat=geoip_latitude();
              ulon=geoip_longitude();

            initialize();
        }  


        function handle_errors(error)  
        {  
            switch(error.code)  
            {  
                case error.PERMISSION_DENIED: alert("user did not share geolocation data");  
                break;  

                case error.POSITION_UNAVAILABLE: alert("could not detect current position");  
                break;  

                case error.TIMEOUT: alert("retrieving position timed out");  
                break;  

                default: alert("unknown error");  
                break;  
            } 
        initialize();    
        } 

        function handle_geolocation_query(position){ 

        ulat=position.coords.latitude;
        ulon=position.coords.longitude;
      /* alert('Lat: ' + position.coords.latitude +  
                  ' Lon: ' + position.coords.longitude); */ 

        initialize();
        } 


     function initialize(){
     loc = new Array();
     geocoder = new google.maps.Geocoder();
      var latlng = new google.maps.LatLng(ulat,ulon);
       var myOptions = {
       zoom: 3,
       center: latlng,
       mapTypeId: google.maps.MapTypeId.ROADMAP
        };
     map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);



}//end of initialize

こんにちは、地理位置情報は本質的に非同期であるため、同期関数で使用しないでください。そうしないと、新しい google.maps.LatLng(lat, lon) 関数で null 値が取得される可能性があります。あなたの要件に応じて上記のコードを試してみてください.jqueryでも同様に作業できます.

于 2011-08-02T16:01:15.223 に答える