Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

getUserLocaion() 'attempt to index global...'
Started by curtistaplin Oct 04 2019 01:27 AM

2 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

curtistaplin

[GLOBAL: userInfoPane.html]
curtistaplin
  • Observer

  • 6 posts
  • Corona SDK

Hey,

 

I'm trying to make a simple app which gets the users location. When I run the code I get an error which states it attempts to index global 'map' (a number value). Here is the code below:

 

map = native.newMapView (160,200,50,50)
map=display.contentCenterX
map=display.contentCenterY
map= map:getUserLocation(latitude,longitude)
print(map)
 
-Cheers


[TOPIC: post.html]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,207 posts
  • Enterprise

If you just want to get their location, you don't need to use a mapView. You only need that if you want to display a map. But your code has some pretty major flaws.

map = native.newMapView (160,200,50,50)

will create a new mapView that's 160 points wide, 200 points high and centered on 50, 50 and stores a reference to the map in a variable called "map". This is likely a global variable which may lead to other problems for you later, but for now, map contains the mapView object.

 

Then these three lines:

map=display.contentCenterX
map=display.contentCenterY
map= map:getUserLocation(latitude,longitude)

overwrite your mapView causing it to get lost. map now contains a number value that display.contentCenterX. Then the next line you overwrite the map variable with a different number. Finally you try to reference the original map object, which has now been over written and as such the method getUserLocation() no longer exists.

 

If you want to use a mapView, then this is likely the code you want:

map = native.newMapView (160,200,50,50)
map.x = display.contentCenterX
map.y = display.contentCenterY
currentLocation =  map:getUserLocation()
print(currentLocation.latitude, currentLocation.longitude)

See how we reference members of the map object (.x and .y) to position the mapView to the center of the screen. Then we use the getUserLocation() method of the map object and store the location table in a new variable called currentLocation. That table has many values in it. See: http://docs.coronalabs.com/api/type/Map/getUserLocation.html . 

Finally it prints out the latitude and longitude stored in that table.

 

Buy if you just want to get the user's location, you can use the LocationServices event system to do so.

local latitude = 0
local longitude = 0

local function locationHandler( event )
-- Check for error (user may have turned off location services)
if ( event.errorCode ) then
--native.showAlert( "GPS Location Error", event.errorMessage, {"OK"} )
print( "Location error: " .. tostring( event.errorMessage ) )
else

       latitude = event.latitude        longitude = event.longitude        print( latitude, longitude )
end end Runtime:addEventListener( "location", locationHandler )

See: http://docs.coronalabs.com/api/event/location/index.html

 

Rob      



[TOPIC: post.html]
#3

curtistaplin

[GLOBAL: userInfoPane.html]
curtistaplin
  • Observer

  • 6 posts
  • Corona SDK

Hey Rob, thanks for helping me and explaining everything so clearly and systematically. 




[topic_controls]
[/topic_controls]