Determining if a user's location is within the boundaries of a FeatureCollection Polygon

For example

polygonFill

Yeah, I did that and it works. Now, I am just receiving two errors which are part of my own program. I'll see if I can fix them myself otherwise I will post them.

For some reason, all the polygons go away when I open my Mit App Inventor Project again. I have to choose my file again as the source and then all the polygons come back again. However, I need to change the fill color for all of them again. Is there a way to resolve this issue? I believe that this is due to the fact that I keep getting the notification that file not saved, please try again later. My screen currently is frozen once I have changed all the fill colors of the polygons.

Thank you!

Hmm. This has happened here too Jay. I believe there is very heavy use of the MIT App Inventor 2 servers. I think the server is glitching in strange ways. Yes, I get the 'file not saved etc.' notice.
The solution; wait a while. What I did was download the aia, then port it to the http://code.appinventor.mit.edu/ server. That allows error less coding for me; hopefully also for you.

Rather than fixing all the 50 states individually, let me see if I can provide you some Blocks that do it all in one fell swoop... in a minute. Since it is glitching...make sure you save an aia.

Try this snippet, or a variation to change all the Fill in your polygons in one operation

changeAllPolygonColorswithCode

Did it work?

yes, it worked! Thank you for that! I am receiving an error which is related to my own program currently; I will try to fix it otherwise I will post it.

Just one last question: Is there a reason why my Android device is not able to detect my current longitude and latitude? Is there anything that I could possibly do so that my Android device can detect the pair?

Thank you for all your help!

Best,
Jay

I can't see your code @Jay_23 so I certainly don't know 'why my Android device is not able to detect my current longitude and latitude". What do you mean? What do you try and what happens?

  • are you inside? Go outside and test

  • do you have coding errors? I certainly do not know..

  • are you using a tablet that does not have GPS hardware?

  • what is your TimeInterval set to. Try 30000 ms ... faster updates frequently do not achieve a satellite fix. Also, be aware, the initial location information might take as long as a minute to display on some devices.

  • are you testing using an emulator. The stock emulator does not have a GPS receiver.

  • have you turned your device Settings to turn Location on?
    LocationSettings

If you load the aia's at Using the Location Sensor , do the example Projects work in your device? The tutorial lists an number of things that could cause issues.

So I tried all those strategies out and when I loaded the aia file of the "Using the Location Sensor", that tutorial seems to be able to get my latitude and longitude but my own project is not able to. Is the "When LocationSensor.LocationChanged" event required in order for the location sensor feature to work (specifically the latitude and longitude)? Would it be easier if I send the aia file of my project to you?

Thank you!

Perhaps. :slight_smile: Whether the LocationChanged is needed depends on whether you use a LocationSensor in combination with the Map control (which has its own built in LocationSensor code) or not. The LocationSensor works in combination with the Map. Be sure to set Map1.LocationSensor to LocationSensor1 to provide the link. Yes, you need a LocationSensor.LocationChanged Block on your Designer and appropriate code to use the latitude and longitude somewhere in your app. If you do not use a LocationSensor object, you need to use alternative methods to provided the device's the latitude and longitude somewhere in your app.

I would use the Map along with the LocationSensor since it is easy and provides the developer with more options about how to use the GPS hardware.

:cry: Please feel free to post it here in the forum or an image of your Blocks and an explanation about what you want to happen.

is what you said earlier
image

Since you said the above code displays your Marker properly, you probably can capture the latitude and longitude in this awkward way latlonfromMarker

Your device location coordinates probably exist in the Marker1.Latitude and Marker1.Longitude for the Marker1.

Here is a screenshot of all the code in my project. I basically want to get the location of the user, find the three counties in the state of the user which have the most COVID-19 cases, and I want to display this information on a map. The code displays circles around those counties with the most COVID-19 cases in the particular state and when the circle is clicked, it displays the number of COVID-19 cases in that county. Additionally, if the user is located in one of the three counties with the most COVID-19 cases in the state, the app sends a notification to the user. My code works fine when I plug in an actual coordinate for the latitude and longitude but the location sensor doesn't seem to be able to pick up the pair by itself. The set Web1.url block links to a website which is an updated COVID-19 database with number of cases per county for each U.S state. Also, File 2 is a google sheet with longitude and latitude pairs for each county in the U.S. Please let me know what you think.

Screen Shot 2020-07-20 at 12.03.11 PM

Note the last picture is a continuation of the second picture.

jayError

I can barely read any of the Blocks Jay.

Please post a clear image of your Blocks. Is the circled code supposed to post the current user's location in the Marker or what? Have you performed a DoIt debug on the LocationSensor.Latitude/Longitude. What values do they show? How to use DoIt by Ennis Knowing the values indicate whether you are getting GPS information. You might be showing 0,0 if you are not using the LocationChangedEvent or set TimeInterval to short or do not wait until your gps reports a satellite fix (and a LocatonChangedEvent).

Glad to help but your images showing how you use the database are useless. Here is what I do know:

  • want to get the location of the user ---use the LocationSensor

  • find the three counties in the state of the user which have the most COVID-19 cases --you have to determine the state of the user using the coordinates from the LocationSensor then consult the database and search for the state

  • I want to display this information on a map. --- what information? the user's location Marker, the dots ? The code displays circles around those counties with the most COVID-19 cases in the particular state and when the circle is clicked, it displays the number of COVID-19 cases in that county. So, I imagine you want to post three circles and the Marker. -- ok, realize I can't see that or what it is supposed to look like.

  • Additionally, if the user is located in one of the three counties with the most COVID-19 cases in the state -- sorry, you cannot determine that with your present code. Your database probably provides the centroid coordinates of each county. The locationsensor will provide the actual user location. It is highly improbable that the two will ever be identical; it will never happen. To compare them the values for lat/lon must be identical.

  • the app sends a notification to the user. --Ok can't read your blocks so you might be alriight

  • My code works fine when I plug in an actual coordinate for the latitude and longitude --- really? Do you plug in the county centroid coordinates? Try any other coordinates that is within the county and your code will fail

  • but the location sensor doesn't seem to be able to pick up the pair by itself. -- sorry, I do not understand what pair it is supposed to pick up. It will NEVER have the exact coordinates of the centroid. You should be trying to establish, perhaps using a geojson of COUNTIES, whether the user is located in any of those counties. Your states geojson is large and has issues loading; the county geojson for the US is three to four times the states geojson. Will counties even load? I don't know.

  • The set Web1.url block links to a website which is an updated COVID-19 database with number of cases per county for each U.S state. Also, File 2 is a google sheet with longitude and latitude pairs for each county in the U.S. --- both of these are interesting ideas.

Very interesting idea. Part is working. Congratulations. I imagine the part not working includes establishing whether the user is located within one of the high covid plague counties. You cannot do that with the information you have available to you. Sorry. :frowning: You can indicate the person is in the state and let him/her know graphically he/she is near one of the counties by showing the circles and the Marker on the map. You can also determine the distance from each circle to the user and tell that person how far they live from the counties with the highest plague counts. There is a block that calculates distance. You cannot equivocally tell that person a person is located in one of those troublesome counties without a county map outline.

Keep working on your app. Interesting ideas, difficult to implement. Parts do work so feel good and implement what is possible.

-- Steve

Sorry about pictures. I have posted new ones below.

  1. The circled code is just supposed to set the marker to the latitude and longitude of the user.
  2. I have not performed a Dolt debug so I'll try that out right now.
  3. I want to display the three circles and marker on the map (that is what I was referring to when I said 'information on a map'.
  4. I forgot to take a screenshot of a portion of my code which gives a notification to the user if he/she is currently in one of those counties with the most COVID-19 cases. I have pasted that code below (in purple).
  5. I am using the google sheets information (state, county, longitude, latitude) just to place the three circles on the three particular counties (on the map) with the most COVID-19 cases. Nothing else. I am accomplishing the task of finding if the user is in one of the counties using the 'Marker Distance to feature block' (that code is below as well).
  6. My code works when I plug in a longitude and latitude pair for the user's location. My code does not fail even when I try another longitude and latitude pair in a county. I have explained what I am doing with the google sheets in number 5 (I may have not been clear before).
  7. The Location sensor is not able to detect a longitude and latitude pair of my Android device. I will surely try the Dolt debug to see what the problem is. I may need a 'When Location Changed' block.
  8. I do not need a county geojson as the 'Distance to feature' block works for me. Did my state geojson not load for you? I do remember that I have to update the Source of the Feature Collection everytime I run the app and it takes about 20 seconds.

I hope that makes my app a bit more clear. Thank you for all your help!


Please note that after the first two pictures, all the rest of the pictures are one block but are separated so that it is easier to read the code.

I tried the Dolt debugger (very helpful tool) and I am getting 0,0 for both the location sensor latitude and longitude. You suggested that I should use a Location Changed Event; what do you suggest that I put in this event?

One step at a time.

Part of the problem with 0,0 is this

When the Screen2 is initialized, the default coordinates are as you discovered 0,0 because when those Blocks attempt to set the Marker, the GPS has not yet realized a satellite fix to find out where it is located. I do not know what you have on Screen1 but attempting to use the LocationSensor before a fix is achieved causes issues. Simply, the circled code is misplaced and I don't know an easy way to fix your issue.

If you use a single screen (or virtual screens), you could do this

Setting the TimeInterval is important, however it means that you still will not have a Marker on initialization. The initial satellite fix takes about 40 to 60 seconds using most hardware.

A way to get around the issue is to place some of the code into a Clock and allow it to continue checking for valid latitude/longitude until the lat/lon is no longer 0,0 and then, and only then to display your map. The Blocks with orange dots are not necessary but you will have to use them if you display the map on the second screen.

What you might do, to experiment, is to use a start Button to display the marker (an even your map) on your second screen to get the user latitude and longitude like this after waiting for 30 or 40 seconds

Got to run! Sorry, there are more eloquent ways but not time.

Thank you Steve for all this help! You informed me about the problem in my code! I realized that the Location sensor needs some time to read the longitude/latitude of the user as well as the current address of the user. My code previously was not waiting for the location sensor to load all this information and thus, I was getting nothing for the address and the latitude/longitude. What I've done with my code is that I am not allowing my code to run if the location sensor has not yet detected the latitude/longitude. This code is finally working for me and I realized that I don't need the geojson file and the polygons anymore because I can detect the address of the user (which was my previous idea). However, the working code that I have pasted below includes the latitude/longitude feature and the geojson file. I may change my code to incorporate the location sensor address instead of using the latitude/longitude of the user.

Again, thank you for all the help. I truly appreciate it.

Looks reasonable Jay. Yes, you got it. Good luck with your Project.

-- Steve

A post was split to a new topic: Documentation request for the LocationSensor

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.