Map that shows user location - dropdown component interaction

I am new to python and have very little experience using anvil. I currently have a dropdown box with three names. I am trying to make a map that shows the location of a person in a map when you select their name from the dropdown box. I have the names in the same row as emails and their latitudes and longitudes in a data table. When a name is selected. I want it to search the table for the name’s coordinates and put a pin in the map on that place. Furthermore, I want the latitude and longitude of one of the three people to update based on their actual location. Essentially I am making something similar to snapmap but you chose the person that you are looking for. Any idea how I could do this?

1 Like

Update:
I no have a functioning map that shows the current location of the user. However, I still need to link the dropdown menu to the map and show the coordinates of other users, other than just myself.

Welcome to Anvil.
I’m not much help with Maps but I can probably help you with the general logic.

Are you successfully populating the drop down list from the data tables or have you hard coded it?

You would need to put the code to mark the selected user’s position in the “change” event of the drop down list, then use the “selected_value” to look up the lat/log values for google maps.

2 Likes

Hi, thanks for the help.
At the moment I have the names manually input into the drop down list. If it would be more beneficial to populate it using a data table a can do that. I am having trouble with the general python because I know what I want to do, and a general idea on how I’d do it. I’m am just not sure of how to translate my thoughts to python. Is there a line of code which retrieves a specific value from a data table, if so I could use it to search the tables rows for the row with the correct user and then select the value from the latitude column. Otherwise I’m lost.

This explains how to do all of that :

https://anvil.works/docs/data-tables/data-tables-in-code

You’ll be looking for something like this :

person = app_tables.people.get(name="Zaphod Beeblebrox")

The examples and the docs cover most of what you are looking to do. If you get stuck on something specific whilst trying these things, post back with what you’ve tried and someone will try to help you further.

2 Likes

Thanks for the help.

I have a data table called Users with a column called Name. To populate my dropdown box, which is called dropdown_name, what would I use. I am having trouble comprehending other responses to similar situations. Once done, I have a button called button_1 that needs to search the table for another value in the same row as the name, which is in a column called Latitude. That value needs to be put into a pin on the map, which I think I know how to do, and the same needs to be repeated for Longitude.

This is a pretty good description of what to do :

My example line in my previous post (taken from the docs) shows a search by name, just adjust the table & columns names to fit. In my example, “person” is the whole row, so you can access all properties like this :

person['name']
person['lattitude']

etc.

Does that help?

2 Likes

So I’ve got this so far:
def init(self, **properties):
self.init_components(**properties)
self.dropdown_name.items = [row[‘Name’] for row in app_tables.users.search()]

But I get that same as talked about in the forum that you sent i.e. Exception: ‘items’ must be a list of strings or tuples
at Form2, line 63
I can’t fix it, I’ve tried manipulating it in many ways.

Can you share a clone link of your project, and I’ll take a look?

https://anvil.works/docs/editor/cloning

PM me if you don’t want to share it publicly.

2 Likes

Ok …

  1. you have 3x def __init__declarations. There should only be one per class (form) definition.

  2. move your drop down loader into the "show "event instead of the init method. It’s good form to manipulate the visual elements of a form once you know for certain it is there.

  3. change your drop down loader code to this :

self.dropdown_name.items = [(row['Name'],row) for row in app_tables.users.search() if row['Name'] is not None]

The “label” portion of the items needs to be s string, and some of the results were None which was causing the drop down item allocation to fail. The if statement on the end of the comprehension line filters out the None elements.

I would really recommend working through the tutorials to get an idea of the make up of an Anvil app. Whilst i can see why you’ve done it, you really only need one “master” Material form with all the menu links. The other forms can be subforms. There are plenty of examples in the tutorials and on the forum. These are the kind of things the tutorials will take you through. Once you’ve done that, you’ll see how you can improve the app no end, making everything a lot easier to follow.

Nice looking app by he way :slight_smile:

I’ve been doing a lot of populating dropdowns lately, so can comment on this. Change your list comprehension to something like this:

self.dropdown_name.items = [(row[‘Name’], row) for row in app_tables.users.search()]

The first item in the tuple is what displays in the dropdown, the second item is what gets returned by self.dropdown_name.selected_value. That’s the entire row, so you can then get the latitude and longitude (or any other column values) from it.

1 Like

This is quite an interesting function you are designing. Please keep me updated on it’s progress as I am keen to see where this goes.

What’s the error?

I’m not too up on Maps so i can’t comment on whether that’s the correct way to add a marker to a map or not.