Thank you Steve! It seems like this feature will work! I am not quite sure how I am supposed to use the geojson Feature Collection of the USA states in App Inventor. Could you please explain a bit more?
Determining if a user's location is within the boundaries of a FeatureCollection Polygon
What have you tried Jay?
Maybe. This possible technique also depends on a network or wifi connection and why you need to know which state the devices is in. It will not be full proof with respect to locations near state/state boundaries. Using a CurrentAddress block is much simpler.
This MIT advice shows one way to use a FeatureCollection https://appinventor.mit.edu/explore/ai2/state-geography-quiz . The MIT tutorial uses a geojson file to display a map that shows only a few New England states.The tutorial should help to understand what is involved.
Read the MIT Map documentation (all of it) Map components . Failure to read and understand will probably mean you will experience difficulty building your Project. Yeah, no kidding. This is not simple stuff. Not necessarily difficult but does require varied programming experience with different controls.
You will need to use the Any Component call Polygon.DistanceToFeature Block and a Map Marker and a LocationSensor. Set the mapFeature of the P.DTF block to Marker1; set centroids to false. Poll each item in the FeatureCollection.Feature List using an any item block. Set component to get item. Look for a component in the List that results in a distance of 0 to your design time Marker. Once you find out in which polygon you are located, use the break Block to terminate the search. Your device is located within the 'captured' polygon. Provide code to display the name of the Polygon you 'captured' iterating through the List; the polygon name will be the name of the state if you provide the coding.
Yes, I think that your method is correct for my project and it is almost working for me. The only problem that I am facing is that when I am trying to import the geojson file, the polygons are not automatically showing up as components like they did in the State quiz tutorial. Thus, I do not have any polygons in my project and I am receiving an error which states: "Distance to feature cannot accept the following arguments....". I have pasted portions of my code below just to confirm that I have interpreted you correctly. Here is the link that I am using for my geojson file: https://eric.clst.org/tech/usgeojson/
Thank you for all the help so far!
Yep, that is what happens if a developer uses the FeatureCollection.LoadFromURL. The polygons show on the Map but not listed in the FeatureCollection in the Designer.
May I suggest you load a geojson file of the USA state outlines to a FeatureCollection directly and without a url link. Load the geojson data to your Project by adding the geojson to the Media, then set the FeaturesCollection Source (in Properties) to point to that file.
Other methods that can be used to load a FeatureCollection might fail. Loading using the File component ( larger files often fail) or using FeatureCollection.LoadFromURL (creates run time polygons). The run time polygons require special handling that design time polygons created when setting the geojson in Media/Source do not require .
Using the FeatureCollection.LoadFromURL to load a FeatureCollection results in run time polygons.
Run time polygons are what you created. Run time polygons are difficult to handle from a programming stand point compared to features of the geojson available to the developer when using design time polygons. If you set the Source for the FeatureCollection, state names are automatically assigned to the polygon Title when when design time polygons are created and the polygon 'Names' (Title) will appear in the Designer. The conversion of 'Name' to 'Title') does not automatically happen when you instantiate the FeatureCollection using a url link. Also, you will not see this in the Designer.
The developer has to provide code to assign Titles (Names) to the polygon blocks in order to do useful things in that case. You would require a List of state names corresponding to the run time components generated.
Thank you Steve! The polygons seem to be showing up in my designer now. However, my code is still not able to show me the polygon in which the marker distance is 0. I'm not quite sure why I am getting nothing in text box 1.
So I actually found an alternative way of writing the code above and my program is finally working! My only problem is that all the U.S states are green now and I think that they may be hiding the marker and circles that I have as part of my project. Is there any way to get rid of the color so that my marker and circles show up? Thank you!
Congratulations. There are about a half a dozen ways to do that. Are you going to share your code that worked for you or keep the huge audience in suspense?
Yes. Your marker and circles (you have circles, wow ) should show without any attention depending how you coded. Since you share limited bits of code, we can only guess. MIT set the control up so the default FillColor for the Polygon generated in a FeatureCollection is a Custom hue of green. Why? I have not idea (possibly Irish ancestry). Developers can manually change all these polygons displays by setting FillColor to None or setting the Opacity to something less than 1 (perhaps 0.1) in the Designer. There are lots of other ways including using the Any Component Blocks or setting the FillColor to a custom color with transparency by changing the settings using Blocks. Experiment. Depending on what you want to do you can change 50+ images manually or use a looping code.
The order in which objects are added to the FeatureCollection is usually responsible for things hidden behind other things, to use the proper technical terms. A solution MIT mentioned some time ago is to reverse the order of displaying the objects; however making the polygons themselves transparent or translucent will work as well .
Here is the code that worked:
I will try out your suggestions!
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.
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
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!
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?
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?
Perhaps. 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.
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
Since you said the above code displays your Marker properly, you probably can capture the latitude and longitude in this awkward way
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.
Note the last picture is a continuation of the second picture.