TypeError:这是未定义的

时间:2013-06-22 08:15:36

标签: jquery html5 google-maps

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

“TypeError:这是未定义的”

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

 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);

1 个答案:

答案 0 :(得分:-1)

当然,这是错误的。您应该使用jQuery()代替$()

See more information