i created and finished an App almost similar to Uber, but a little bit different…
it consists of 2 apps:
with first app you place an order through a menu
with second app the nearest driver gets your order and delivers it
the app is very smooth and very quick…
i have 2 problems though!!
Problem1:
i’m not sure i’m using the LocationHandler event for mapview correctly, as everytime i’m removing the current mapview, and loading a new one to get an updated GPS location, is that correct? or can the same eventHandler refresh automatically
Problem2:
how can i get more info from the returned GPS Coordinates like city or country name
For more information, use Google services to look up the info on the GPS coordinates. It has been a long time since I did this, but I think you want to use the ‘Google Places’ API: https://developers.google.com/places/
I dug through an old project of mine. You will have to research this yourself, but back when I wrote this project (2015-ish) you could build a request for nearby parks (what I was doing in the app) like this:
-- -- Create Places Query -- headers = {} headers["Content-Type"] = "application/json" headers["Accept-Language"] = "en-US" body = "" local params = {} params.headers = headers params.body = body local requestString -- 1. Configure search type requestString = "https://maps.googleapis.com/maps/api/place/nearbysearch/json" -- 2. Add lat/long requestString = requestString .. "?location=" .. public.latitude .. "," .. public.longitude --requestString = requestString .. "?location=" .. 45.324226 .. "," .. -122.582864 -- 3. Add radius requestString = requestString .. "&radius=" .. searchRadius -- 4. Add 'park' to the types search (Tip: | is separator for multiple types) requestString = requestString .. "&types=park" -- Multi-Types example =\> requestString = requestString .. "&types=type1|type2|type3|etc" -- 5. Add sensor requestString = requestString .. "&sensor=false" -- 6. Add key requestString = requestString .. "&key=" .. GooglePlacesAPIKey --print("\n\n" .. requestString) -- Sample searches that produce results -- DEBUG ONLY - Known working 'nearby' request string to test in case you have issues. -- UNCOMMENT THIS TO OVERRIDE BUILT-UP REQUEST STRING -- requestString = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=45.324226,-122.582864&radius=5000&types=park&sensor=false&key=" .. GooglePlacesAPIKey print("Request string:\n") print("requestString\n" ) network.request( requestString, "POST", placesNetworkListener, params)
Clearly, your request would be different, but it would be similar in format.
The listener looked kinda like this:
-- == -- placesNetworkListener - Callback that handles Google Places Search Results. -- == local function placesNetworkListener( event ) local foundPark = false local foundDistance = tonumber(searchRadius)/1000 local nearestPark = math.huge local nearestParkName = "" local message if ( event.isError ) then print( "Network error!") return else --print ( "RESPONSE: " .. event.response ) local response = json.decode( event.response ) if(response) then print("Responses --\> " ..#response.results ) if(response.results) then for i = 1, #response.results do local res = response.results[1] if( res.name ) then --print( res.name .. "\n" ) end if( res.geometry and res.geometry.location ) then local lat = tostring(res.geometry.location.lat) local lng = tostring(res.geometry.location.lng) lat = lat:lpad(#lng) local deltaLat = tostring(round(tonumber(res.geometry.location.lat) - tonumber(public.latitude), 6)) local deltaLng = tostring(round(tonumber(res.geometry.location.lng) - tonumber(public.longitude), 6)) local deltaLen = tostring(ssk.math2d.length( tonumber(res.geometry.location.lat) - tonumber(public.latitude), tonumber(res.geometry.location.lng) - tonumber(public.longitude) )) deltaLat = deltaLat:lpad(9) deltaLng = deltaLng:lpad(9) deltaLen = deltaLen:lpad(9) local latStr = lat .. " (delta: " .. deltaLat .. ")" local lngStr = lng .. " (delta: " .. deltaLng .. ")" --print( " public.latitude: " .. latStr .. "\n" ) --print( " public.longitude: " .. lngStr .. "\n" ) --print( " delta len: " .. deltaLen .. "\n\n" ) -- Now, check to see if local lat1 = tonumber(public.latitude) local lng1 = tonumber(public.longitude) local lat2 = tonumber(res.geometry.location.lat) local lng2 = tonumber(res.geometry.location.lng) local dist = math.haversine\_dist( lat1, lng1, lat2, lng2 ) if( dist \<= foundDistance ) then foundPark = true end if( foundPark and dist \< nearestPark) then nearestPark = dist nearestParkName = res.name print('i:', i, dist, foundDistance, nearestParkName, foundDistance ) end end end end end end end
For more information, use Google services to look up the info on the GPS coordinates. It has been a long time since I did this, but I think you want to use the ‘Google Places’ API: https://developers.google.com/places/
I dug through an old project of mine. You will have to research this yourself, but back when I wrote this project (2015-ish) you could build a request for nearby parks (what I was doing in the app) like this:
-- -- Create Places Query -- headers = {} headers["Content-Type"] = "application/json" headers["Accept-Language"] = "en-US" body = "" local params = {} params.headers = headers params.body = body local requestString -- 1. Configure search type requestString = "https://maps.googleapis.com/maps/api/place/nearbysearch/json" -- 2. Add lat/long requestString = requestString .. "?location=" .. public.latitude .. "," .. public.longitude --requestString = requestString .. "?location=" .. 45.324226 .. "," .. -122.582864 -- 3. Add radius requestString = requestString .. "&radius=" .. searchRadius -- 4. Add 'park' to the types search (Tip: | is separator for multiple types) requestString = requestString .. "&types=park" -- Multi-Types example =\> requestString = requestString .. "&types=type1|type2|type3|etc" -- 5. Add sensor requestString = requestString .. "&sensor=false" -- 6. Add key requestString = requestString .. "&key=" .. GooglePlacesAPIKey --print("\n\n" .. requestString) -- Sample searches that produce results -- DEBUG ONLY - Known working 'nearby' request string to test in case you have issues. -- UNCOMMENT THIS TO OVERRIDE BUILT-UP REQUEST STRING -- requestString = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=45.324226,-122.582864&radius=5000&types=park&sensor=false&key=" .. GooglePlacesAPIKey print("Request string:\n") print("requestString\n" ) network.request( requestString, "POST", placesNetworkListener, params)
Clearly, your request would be different, but it would be similar in format.
The listener looked kinda like this:
-- == -- placesNetworkListener - Callback that handles Google Places Search Results. -- == local function placesNetworkListener( event ) local foundPark = false local foundDistance = tonumber(searchRadius)/1000 local nearestPark = math.huge local nearestParkName = "" local message if ( event.isError ) then print( "Network error!") return else --print ( "RESPONSE: " .. event.response ) local response = json.decode( event.response ) if(response) then print("Responses --\> " ..#response.results ) if(response.results) then for i = 1, #response.results do local res = response.results[1] if( res.name ) then --print( res.name .. "\n" ) end if( res.geometry and res.geometry.location ) then local lat = tostring(res.geometry.location.lat) local lng = tostring(res.geometry.location.lng) lat = lat:lpad(#lng) local deltaLat = tostring(round(tonumber(res.geometry.location.lat) - tonumber(public.latitude), 6)) local deltaLng = tostring(round(tonumber(res.geometry.location.lng) - tonumber(public.longitude), 6)) local deltaLen = tostring(ssk.math2d.length( tonumber(res.geometry.location.lat) - tonumber(public.latitude), tonumber(res.geometry.location.lng) - tonumber(public.longitude) )) deltaLat = deltaLat:lpad(9) deltaLng = deltaLng:lpad(9) deltaLen = deltaLen:lpad(9) local latStr = lat .. " (delta: " .. deltaLat .. ")" local lngStr = lng .. " (delta: " .. deltaLng .. ")" --print( " public.latitude: " .. latStr .. "\n" ) --print( " public.longitude: " .. lngStr .. "\n" ) --print( " delta len: " .. deltaLen .. "\n\n" ) -- Now, check to see if local lat1 = tonumber(public.latitude) local lng1 = tonumber(public.longitude) local lat2 = tonumber(res.geometry.location.lat) local lng2 = tonumber(res.geometry.location.lng) local dist = math.haversine\_dist( lat1, lng1, lat2, lng2 ) if( dist \<= foundDistance ) then foundPark = true end if( foundPark and dist \< nearestPark) then nearestPark = dist nearestParkName = res.name print('i:', i, dist, foundDistance, nearestParkName, foundDistance ) end end end end end end end