Converting text to numbers that can be assigned to Marker SetLocation

Hello,

So I am trying to get the route to the nearest location.

I tried to combine the National Parks project and the route project to get shortest route from a csv list.

I tried putting the the latitude and longitude of the nearest location (which were successfully obtained and displayed on two labels (17 & 18) at the very end.

I tried putting these labels on the web component, such that the labels would assign the values to the latitude and longitude of our marker (named KIMS; else if condition). call KIMS.SetLocation method perfectly works if the latitude and latitude are numbers and shows route but not the assigned values.

How can I convert those labels or marker latitude and longitude into numbers that can be accepted by call KIMS.SetLocation?

Kindly help.

Regards.

  1. What do the lat and lon numbers look like ?
  2. If they truly look like lat and lon numbers e.g. lat 51.501070 lon -0.141494, you could try multiplying them by 1 to make them a number....

image

Thank you for the response, Tim.

The labels showing Lat and Long values are numbers.

I tried your formula but it doesn't work.

blocks (10)

Kindly help.

Regards.

use Do it to debug your blocks, see also tip 4 here App Inventor: How to Learn | Pura Vida Apps
see also Live Development, Testing, and Debugging Tools

Unbenannt

Taifun


Trying to push the limits! Snippets, Tutorials and Extensions from Pura Vida Apps by Taifun.

    • Why are you multiplying a Label and why are you using the call Marker.SetLocation block? The call Marker.SetLocation block is used with design time Markers. You seem to be using run time Markers, so I expect you should be using image or possibly using both types in your app.

When you use destNumber
you use your selectDestinationNumber Procedure identify the latitude and longitude, whether you actually get a latitude or longitude depends on whether latitude has an index of 3 and longitude an index of 4. Perhaps the index of 1 is a name you provide to identify the location? What does selectDestinationNumber do? What it does is not obvious from the Blocks you shared. It might be a csv file like 1, anothervalue, latitude, longitude; but you do not tell us.

The component should be a value a marker 'name' from a ListofRunTime Markers you create from your locationList I guess.

setLocation

You said "perfectly works if the latitude and latitude are numbers and shows route but not the assigned values." Sorry, this statement is not clear. What are the 'assigned values' ... your name for the location? Are you attempting to post these 'assigned values' in a Marker bubble? If so, get the values from the csv you may have constructed to identify the location at post it in set KIMS.Title perhaps (a guess). What you do depends on whether KIMS is a design time or run time Marker and the construction of your csv.

You might post a piece of your csv to help us understand what you are trying to do and why you need to do it.

Regards,
Steve

Thank you for the response Steve, Taifun.

From the National Parks example, I obtained the nearest location.

From that example, one can see where the latitude and longitude are found. selectDestinationNumber 3 and selectDestinationNumber 4are where they are received. What I did was assign the obtained coordinates to labels 17 & 18.

Now...

Else condition of the block:

I tried to use the labels to assign coordinates (which I had earlier referred to as values) to the marker. Now , if I used [number] block and replaced 0 with actual coordinate numbers, marker and route to the marker is visible. But when I tried to use label to set location, route doesn't appear.

blocks (10)

CSV is: Name,number,latitude,longitude - just like in National Parks example.

Kindly help.

Regards.

It does not appear you called the route Procedure
routeOSM

To display a route on your Map , you cannot use the Marker.SetLocation Block. That Block only places a Marker at that location. Instead, call the route Procedure (that is part of the one example you are following) Place the Label17Text and The Label18.Text into the empty puzzle pieces routeCalling into endLat and endLon slots respectively. Is that all you need to do? I do not know. It depends on the code in your Web1.Get. It appears you are trying to display the route from within the code that displays it. You can not do that. Perhaps add a Button that contains the call route and the label coordinates and eliminate the entire else if block...it does not do what you think it does.

Thank you for the response, Steve

I have been using the route call procedure which you have mentioned since the beginning.

I modified the codes as follows:


(I have left the endLat and endLon while downloading block image; I DID assign labels to the bu no route is visible)

I took in the national parks example to get nearest location. Location's coordinates are successfully shown in two labels 17 & 18. THIS is the set running markers from National Parks.

I tried to assign label numbers/string to endLat & endLon BUT it doesn't work.

This is a combo of both route and National Parks examples.

Kindly help.

Regards

The errorMap first If is possibly totally inappropriate. The conditional probably prevents the route from displaying. Only you can test whether eliminating the IF helps. Fill in the empty puzzle pieces too or you will generate an error.

You have other mistakes you can probably fix after you fix this Block.

Of course, what happens depends on the values here:

Your attempt won't work if

depends on what selectDestinationNumber provides. You have to test this. Are you sorting the locations based on the straight line distance from the Android ? The method used in NationalParks uses a straight-line distance calculation (not the driven route). You might want use the straight-line distance The distance determined by using route Procedure will be different but it probably is not an issue. Sorting many locations to get the driving distance is very complex, might cause issues using OSR and probably is not necessary for your app.

Thank you for the repsonse

Tim, I DID NOT put the endLat and endLon as blanks. I did put up the labels next to it, which still doesn't work. I erroneously added the wrong image of the block from a previous condition. Apologies for that.

What I need to do:

  1. If status says available,route to A particular location is to be shown. The main, primary location. This works perfectly. The coordinates are fed at design portion itself.
  2. If status says Unavailable, route to nearest center/hospital/location needs to be shown. THIS is not happening.

I merged the National Parks and route examples.

Yes, I need to get driving route to the nearest location if status is unavailable.

The primary route is shown perfectly. The route for unavailable condition is not working. If I manually assign coordinates to the endLat and endLon from blocks section, the route is visible in unavailable condition . What I need to show is the driving route to nearest location USING the csv file data - while employing National Parks example. CSV data is in the format: name,number,latitude,longitude

I'll attach all the current major blocks so as to avoid any confusion.

c1 c2

Capturea5 Capturea9

Also...

Even with this block section first if condition, the route is shown perfectly for Available condition.

If the nearest location can be found and coordinates can be assigned to labels 17 & 18, it should be able to show route as well. But for some reason, route is not being shown.

Kindly help. Please.

Regards.

Once you display all the Markers in a csv (from the National Park tutorial), it is impossible to later display a route on the Map component. If you try, you either elicit this RunTime Error Attempt to invoke virtual method 'void org.osmdroid.views.overlay.OverlayWithIW.showInfoWindow()' on a null object reference Note: You will not see another error reported for 5 seconds.

or the Map component will refuse to display the route on the map.

The behavior may be an issue with the OSM api and the ability to render the LineString when runtime Markers are present. This is a guess. The National Parks tutorial creates a number of runtime markers; when you attempt to post the route path, posting of the route fails Why? I certainly do not know.

You can plot only the closest driving route from a csv of many destinations:

  • use a locations csv as in National Parks and populate a List of destinations
  • calculate the straight-line distance to each location in the List without posting runtime markers.
  • determine the location closest to the Android. Sort a List of distances and use it to determine which location in the List is the shortest distance away. Do not use the Map1.CreateMarker Block in your solution. The CreateMarker Block creates a run time marker. Runtime markers on the Map seem to cause issues with the route Procedure. Instead use a different method to determine distances. I suggest a Great Circle algorithm.
  • post the route and only display the route to the location that is nearest.
  • shortest route displays

I tried several methods to get a display of all location markers and the route LineString. All potential ways to merge the route and National Park tutorial are complex. The attempts have issues relating to the methods that do not allow rendering of the driving route or Markers for all the locations.
I could not find a simple way to take existing code from the two tutorials, merge those and
successfully post a driving route on the Map. The best solution involves rewriting the sorting routines to find the 'closest' location by straight-line distance. Use the route Procedure to plot the driving route without posting the positions of all the other locations in the csv file.

If you create the display on the right, I found it impossible to display the shortest driving route on the same map object. Displaying the route on the left works fine as long as you avoid run time markers. If you find a solution that allows both to display; be sure to post it here.

Developers cannot add a route display and have it have it render if they also display all locations with markers in red and the nearest in green as in National Parks. The problem is the possible bug in the OSM api or elsewhere that does not allow this. I sort the locations without displaying all locations and only show the shortest solution on the map. Displaying the route first, then posting all the other Markers fails too. The route displays but run time Markers do not plot. Someone else might have a better solution that allows merging the two tutorials that sustains the main features of both tutorials in the same app.

Keep trying. It is possible to sort all locations and display the driving route. At the moment, I believe no one is able to plot a Marker for all locations and expect to plot the route on the same map.

@SteveJG Do you have an AIA that exhibits the error. That error sounds like a bug in App Inventor and I'd like to figure out what the root cause is.

@ewpatton Sure Evan. I'll email you an example later today or tomorrow. I need to write something to describe behavior so you can understand what I did and what happens.

-- Steve

1 Like

Thank you for the response, Steve.

I tried using two map components. The first map component was used for finding the closest location. It does. Then, I tried using the marker as a reference point for the marker in the second map component. I tried to find the route using a button. No change. The route is not visible. I tried two web components - Web1 for National Parks example and Web2 for route. Doesn't work. I used the same web component for both. Doesn't work.
All these in same screen.

Kindly help, if you got the solution.

Regards.

Evan indicated

I expect anyone will have issues merely copying and pasting from the Routes and National Park tutorials to combine the features of the apps. It will be that way until Evan finds a solution to allow the route track to display after posting runtime markers on the map. He does not have a fix for the bug yet so no one can post the route if he/she posts all the Markers.

Can you build a similar app despite the bug? There are several possibilities

  • do not display all the destination locations using run time markers. Calculate the distances from your Android to those destinations without using the CreateMarker block; sort the distances and capture the location that is closest. Use the coordinates of the closest destination to post only the route on the map ( the shortest route. ) I showed a solution on 22 June. Posting all the location markers creates or contributes to the issue where the route track does not render on the map in my opinion. I believe runtime markers and LineString components might interfere with one another. If you post only the shortest distance route (without the other markers), there is no issue displaying the route. The route posts.

  • I made major revisions in the code of both tutorials and developed a 'solution' that seems to allow posting both all markers and the route track on the same Map. Create a distance List of the distances to each location and merge the distance List with the original destinations list (created from the csv of destinations). Determine the closest location using a Great Circle distance algorithm. Use the coordinates from merged list that correspond to the nearest location and post the 'shortest' route. After posting the route, post the location markers of all the other locations on to the Map that displays the route. I get a map something like this:

    with both the locations and the shortest driving route. That is probably what you want to do.

I expect the second way to build an app was successful because of the order I created map objects. Post the shortest route, then post the Markers of all the destinations. Try that object creation order (construct the route LineString then post the runtime Markers) or wait for Evan to fix the suspected bug (your existing code probably will work once Evan fixes Map component issue).

What I do to build an app using the features of both the route tutorial and National Parks tutorial is not necessarily a

that will work in your circumstances.

Thank you for the response, Steve.

Could you kindly provide the aia or the blocks for the new solution you said, including major changes?

or how this worked?

No need of all the markers to be posted. Just the nearest one and route to it.

When I did try to put assign the coordinates of nearest location to labels and then employ these coordinates to call route procedure (where endLat and endLon were fed with the labels, route did not appear.

Please. Kindly help.

Regards.

The critical Blocks needed to sort a List of coordinates by distance from an Android device in a way that does not trip the LineString bug are shown @rohitcr2k .

Sufficient information is posted to determine the nearest location. Then you can use the coordinates provided to post a route to that location without tripping the bug. The original, 'simple' method to sort destinations based on distance from your Android works fine in the National Parks tutorial. When a developer uses the simple method, users cannot later post a LineString (the route) on the Map as you discovered. Why is this alternate method necessary? Using runtime Markers along with a LineString contribute to the bug that prevents developers from posting the route LineString after working with runtime Markers Evan mentioned.

The Blocks in the Button3 event handler determine the nearest of all the destinations stored in the Destinations.csv, the csv file used in the National Parks tutorial. Both methods find the the closest location. These Block determine the nearest destination without using the Marker blocks (used in the National Parks tutorial). This more complex method should allow you to calculate distances and sort them without using runtime Markers.

The Button3 Blocks calculate distances and determine the nearest without using runtime Markers. The algorithm

  • iterates through the csv/list of coordinates to obtain the required information.

  • calculates a distance from the Android to each of the locations. The calculation requires coordinates from a LocationSensor or hard coded to use in the route Procedure Origin coordinates.

  • saves each distance calculated in a List of distances. The distances are calculated by iterating through fulldestinationList. The distanceBetweenLocationsMiles Procedure makes the actual calculation.

  • creates a List of the distances to all the destinations (newDestinationDistances). Simultaneously the newDestinationDistancesandLocation List (the original destination list with the calculated distance as a new item) is created. Both Lists are needed later to identify the nearest location.

  • uses the getMinimumValueInList2 Procedure to determine how close the Android is to each stored location from the lists.

  • searches the newDestinationDistancesandLocation List for the 'shortest' straight-line distance. In the example it searches for 243.9 miles (the calcMinDistance). The distance you determine to the nearest location will be very different.

  • then finds the calcMinDistance in the newDestinationDistancesaandLocation List, the code extracts the latitude,longitude and Location Name corresponding to what the code determined as the closest location (shortest distance) from the newDestinationDistancesandLocation List items.

  • the corresponding coordinates (closestDestination) display in Label3 and theLocationName in Label4.

  • some blocks Blocks you need are in the National Parks tutorial and the Blocks are not reproduced here. Also missing are the Blocks required to initialize the Lists. You need to provide the 'missing' Blocks and the List initialization Blocks.

Reproduce the Blocks, add the missing pieces and you can determine the closestDestination coordinates.

Use the lat,lon coordinates of the nearest location in your route Procedure destination puzzle pieces. Parse the lat,lon two element csv to obtain the destination lat and the destination lon. Display the route on your map.

Calculating the nearest destination without using the runtime Marker information will allow you to post just the nearest marker and the driving route to it on your map.

The route is calculated but does not display when you use the route Procedure because of the bug. The bug means you cannot use the CreateMarker command prior to drawing the LineString route (or something related). The LineString displays the route graphics.

Passing information using Labels can create execution issues; much better to provide the information you want to use for endLat etc. in a variable.

4 posts were split to a new topic: Convert the Text of a Button to a Number