0

我正在尝试从 html5 定位函数中获取用户的纬度和经度。之后使用谷歌地图 API 我想获取用户的当前位置名称。这是我在 jquery 中编写的代码。但我遇到了一个错误

“类型错误:这是未定义的”

这里出了什么问题?我错过了什么吗?

 function getLocation()    //getting the users location 
       {
                  alert("inside show getlocation");
                  var locationerror = document.getElementById('locationerror'); 

                  if(navigator.geolocation)
                  {
                    navigator.geolocation.getCurrentPosition(showPosition);
                  }
                  else
                  {
                     locationerror.innerHTML = "Please enable current location in your browser to get best results. Click <a href = 'https://www.google.co.in/search?q=enable+current+location+in+browser'>here<a> for help.";
                  }
       };

       getLocation();         

       function showPosition(position)  //get user's  current location name
           {
                 alert("inside show position");
                 mylat = position.coords.latitude;
                 mylong = position.coords.longitude; 

                 var latlng2 = new google.maps.LatLng(mylat,mylong);

                 alert(latlng2);
                  geocoder = new google.maps.Geocoder();
                  if (geocoder) 

                  {
                     var apilink2 = "http://maps.googleapis.com/maps/api/geocode/json?";

                    $.ajax(
                    {
                      type: "GET",
                      url: apilink2,
                      data: {
                         latlng: latlng2,
                         sensor: true           
                            },
                        dataType: "jsonp"
                        }).done(function(response)
                        {
                             alert("done");
                              var currentlocationname = response[0].formatted_address;

                               if( typeof response[0].currentlocationname == 'undefined' )
                               {
                                   $('#locationerror').innerHTML = "Can't get your current location.";
                               }
                       });



             alert(currentlocationname);
4

1 回答 1

-2

绝对,这是错误的。你应该使用jQuery()而不是$()

查看更多信息

于 2013-06-22T08:26:32.047 回答