Getting driving distance and inserting into sql server 2008 - javascript

I have a table with lat/long for two points and I need to automatically insert driving distance (not point to point). I know I have to use a 3rd party and I would prefer to do everything in T-SQL - if possible.
I have been working with Google and managed to get the distance in 2 ways (not in SQL):
1) Javascript: This doesnt seem like a viable solution but I found some code online that produces the distance but the address has to be manually entered and I still don't know how to get that number into my table. Is there a way to run JavaScript in SQL and use my lat/long columns as parameters? My guess is no, but I figured I would ask anyway. If there is a way than I can post the code so you can show me how.
2) Found this and it seems like it could work: http://maps.google.com/maps/api/directions/xml?origin={PlaceA}&destination={PlaceB}&sensor=false
which returns results in XML. However, there are multiple distance nodes that arent uniquely identified. Also, is there a way to run this in SQL and use my lat/long columns as parameters for the origin and destination fields, then enter the LAST
<distance>
<value>Number</value>
<text>Number</text>
</distance>
that is returned into my table?
If there is any way to do this through SQL than I am open to any method that works.
The two methods I posted don't really help me get the info into my table and I dont know how to run them from SQL server.

Related

Getting data from JSON (i think)

I hope someone here can help me with this as i am at a loose end. I have a little experience with programming (variables, etc) but not much at all with Javascript, JSON and many others.
Unfortunalty i cannot fully describe what im doing as it invloves sensitive info but i will try my best:
My plan is to collect data from a webhook and enter it into a google sheet which can then be seen by myself and my colleagues (and it needs to be as 'real-time' as possible, hence using webhooks instead of API - i think)
Ive been using google apps script and javascript
At the moment i just want to be able to collect the data from 1 variable, once i know how to do that i can hopefully figure out how to get the rest of the data that i need from the other variables - but there is another problem, the JSON data seems to come in 2 different formats which to be perfectly honest i confusing the **** out of me, so if anyone can could you please explain as best you can what i need to do.
I have looked at w3 schools and a number of other sites and forums (of which this one seemed to make the most sense hence me asking on here) but most of it has fryed my brain.
anyway here is what i need :
I would like to get the data from "title" - in this example "Order #301428\"
here is part of the webhook i have received - the data has made it to the spreadsheet but it is in one massive string which is no good to me, i need to be able to just grab the data i need.
also could someone please tell me how to enter the data i need in a google sheet
Thank you very much in advance for any help
here is the 1st format that i have recieved (not complete as sensitive data) :
{"parameter":{},"postData":{"contents":"{\"id\":12374453,\"title\":\"Order #301428\",\"created_at\":\"2020-04-28T23:43:13.000Z\",\"updated_at\":\"2020-04-30T10:52:27.000Z\",\"customer_id\":10960996,\"user_id\":14426,\"status\":4,\"scheduled_at\":\"2020-04-30T10:00:00.000Z\",\"merchant_id\":149,\"extras\":{\"previous_task_distance\":null,\"distance_to_team\":null},\"
here is the 2nd format :
{"contextPath":"","parameters":{},"contentLength":23356,"parameter":{},"queryString":"","postData":{"contents":"{\"id\":12425150,\"title\":\"Order #354554\",\"created_at\":\"2020-04-30T09:14:01.000Z\",\"updated_at\":\"2020-04-30T10:43:11.000Z\",\"customer_id\":11003352,\"user_id\":14894,\"status\":4,\"scheduled_at\":\"2020-04-30T11:00:00.000Z\",\"merchant_id\":149,\"extras\":{\"previous_task_distance\":null,\"distance_to_team\"
thank you again
Thank you all for your help - i've looked through numerous tutorials and other information but no matter what i do i cant get it to work and i honestly have no idea what im doing wrong - the thing that is really getting me is i can get the long string of data to show up in google sheets so i know ive got the connection there right - but when i follow any of the advice regarding JSON.parse, Javascript objects, JSON.stringify or anything else it just doesnt work - heres my code (my latest test)
var data = JSON.parse(e);
var orderno = data.postData.contents[1];
sheet.getRange(lastRow + 1,1,1,1).setValue([orderno]);
for some reason i cant enter all of my code
Take the json and parse it into an object:
https://www.w3schools.com/js/js_json_parse.asp
You should then be able to access the attributes like so:
obj.postData
PostData appears to be another json string embedded in the first json, so you will need to parse that as well.

How to auto-populate when checked / remove info when unchecked

I am very new to programming and I need some help. I am making a billing/invoice program. I would like to be able to populate and unpopulated an invoice by checking or unchecking a company and either one or multiple services. The company names and services are being stored in a database. I know this needs to be done with JavaScript. I have been looking for examples of how to do this on the internet and I can't find anything that use checkboxes. I'm probably making this way harder than what it should be. Any help on how I can do this would be greatly appreciated.
Google's first result on "javascript checkbox" is an answer to your struggle (w3schools link).
you need to do it in few steps 1st. If data is stored in SQL database use PHP to transfer data for json or use PHP to insert data stright in to columns(checkbox labels)
2nd. Data is readable, so lest go for next step if you using JSON insert data in to your label's.3rd. Write some code in JS, you need to create few functions like get checkbox val, load invoice , and some more relative to your case. For sure you will have some trouble with writing code, but you have community, and we are here to help :D Good luck

Write a auto-fill and submit web form program

I am trying to write a program which can automatically fill in and submit a form in a web in particular time slot.
But i have no idea how and where to start. i searched this in google, but only resulting very general answer like using JavaScript, python. Can anyone tell me which languages should i learn first?
Despite the fact that the generic advice on this thread is quite good, it's pretty broad. I have tackled this problem myself, and despite the fact that I posted a fully functional example, it was deleted by a moderator, despite "theoretically answering the questions".
So, for anyone else looking to solve this problem, you will want to do the following:
Use Selenium and openpyxl, these are two modules that are relatively straight forward and will perform this task perfectly.
You will use selenium to open your web page, and retrieve the relevant html elements that you wish to populate. I suggest finding elements by xPath if you aren't well versed in HTML. The Xpath finder google chrome addon will make this very easy to do.
The driver.get() and driver.find_element_by_xpath() will be the functions that you need.
We will use openpyxl to work with our excel sheet. 'load_workbook()' will load a workbook. We will then use the 'sheet = workbook.active' function to access a sheet from within the workbook.
We now have the functionality to open our website and select an excel sheet.
Now we need to assign cell values to variables, so that we can then populate the HTML form. We assign a variable to each COLUMN in the workbook. So if column A contained first_names we could assign that to by a variable by writing 'FNAME = sheet['A']. Now that we have a way of referring to cells within columns we can begin feeding the data into our HTML form.
We populate our form by using the .send_keys() function in Selenium.
first_name.send_keys(FNAME.value)
the .value makes sure that the correct value is displayed, as sometimes selenium will print the cell function rather than value, we do not want this to happen.
Now that we can print values to our HTML forms from our excel sheet we will need to iterate through each row. We do this with a simply while loop:
i = 1
x = 1
while x <= 50:
first_name.send_keys(FNAME[i].value)
i+=1
x+=1
driver.quit
Once the loop occurs 50 times, the driver will quit, closing the browser and stopping the script.
Some other misc stuff you may find useful when trying to automate this:
driver.back()
time.sleep()
If you would like to see an actual working example feel free to PM me, as apparently posting it here doesn't contribute to the discussion.
The answers you found, as general as they are, are correct. I'll try to explain it to you.
As you are trying to automatize some activity, you have to use a script language to basically
Get stuff references (like getting indexes, forms/fields IDs, etc)
Insert/change stuff (like filling a field, following the field references)
Save stuff (prepare a file, with field references and it's content, that can be injected to the specific system or website)
One example of script language is Python.
So you already have your script. Now you need to inject it on the page. The programming language that can do it for you is Javascript, or JS. It allows you to talk with the webpage, GETting data/references or POSTing data.
I suggest you to write a Python script using a library called Selenium Webdriver.
It will be easy to implement what you have in mind.

Best way in Javascript to check if string is inside an huge txt/csv?

My real world problem is: users of my mobile app type their city and I have to make sure it really exists, and that it is correctly written (caseinsensitive, so these are correct: New York, NEW york, new york. This is not correct: newyork)
There are online apis that work quite well (Google Geocode API for example) but:
After a very little amount of requests, you have to pay (2.500/day right now)
Users must be connected to the internet
That's why I tought that an offline-local solution would be better. There are many websites (like Maxmind) where you can download a list containing every city in the world. I could embed this huge txt/csv right inside my application and do a string search locally (it's a big file, ok, but not that big. It's just a onetime download of something like 30-40MB of uncompressed .txt)
I'm trying to avoid jQuery at all costs and I don't want to use any PHP/MySQL solutions (even if fulltext indexes could be handy), that's why I'm trying to do all this just using javascript.
Given a string as input, let's say "city3", what's the best/fastest way to check if it's inside an (external) huge list like:
city1,
city2,
city3,
city4,
[...]
After solving this (big) problem: if there are no exact matches, is there a way to search for the correct city without freezing the device for 10 minutes?
In the example before, lets say the user types "cit y3" or "cyty3" or "cìty3": can any js function tell him that he might be looking for "city3"? Is this kind of search too slow in this scenario?
Thanks
If speed is an issue then I would recommend loading the data into a JavaScript object and performing an in-memory search rather than repeatedly scanning a big blob of text in a file.
Try formatting the data into JSON with the city names as keys, that will give you good search performance.
A Workaround is creating a Database either SQL either noSQL, and Query this database through your JavaScript Code, using jquery Json functions.
Using a SQL Database ideal would be either MySQL either MariaDB An enhanced, drop-in replacement for MySQL.
In this solution you will probably need a Backend such as PHP to fetch the data from your Database convert them to JSON Format, and then get them through your JavaScript using jQUery Library , with the $.getJSON function
Using a noSQL Database ideal would be MongoDB.
In this solution you can fetch your data directly from javascript, also with the $.getJSON function.
Example for MongoDB Provided Here
if you dont want to use database i think you can do this:
-first , instead use one big file split it into several files. (you can write a script for this and use it just one time for split the big file). in each file put cities that starts with (example) aa , second file cityes that starts with ab.
-then for each city check first letters and then search inside that file.
For example if you need to search for city "Ahmedabad" it will search only in the files with cities that starts with Ah. Probably this is not the best solution ,at the end you got 421 file instead 1 , but reasearch will be faster.

Get Zip Codes within a range in a given Longitude-Latitude

Im working with the google-mapquest mapping API. I need to add some Points of Interest near a certain area from a given latitude-longitude location.
I search the google and mapquest documentation to figure out if there is a method to get all zip-codes in that area but i didnt find anything that does that.
I tried the harvesine implementation to calculate the nearest point given in that radiuos/latitude-longitude and it works fine. The problem with this approach is that i need to update constatntly the database, so i dont like it much.
The question is: Is there a service (google-mapquest or another) that gives you this information?.
No, there is no API for either service that I am aware of. Your best bet is to just get a ZIP Code database and use that. Some really good inexpensive ones are out there. I prefer http://www.zip-codes.com/zip-code-database.asp. Their $40 package is very accurate, updated monthly, and licensed by the USPS (so you know it's quality data).
UPDATE:
I found a WebService that does exactly what i need. You could provide only a simple ZipCode and a country or perhaps a full address and in the response you get the longitude/latitude. Then i send the Latitude and Longitude returned to get a list of all the ZipCodes near the provided coordinates and a given Radius that also can be customized.
You can filter the total rows you want the service to return, or even if you are gonna use miles or kilometers as the measure unit.
So, basically with the returned information, i feed those coordinates for each of the nearest ZipCodes obtained to the google API and render those points of interest into the map that you need to show.
The services are provided by GeoNames. Its an organization dedicated to Geolocalization services. They offer also premium data, but in particular these services are free of use, obviously the information is not as accurate as the premium one, but actually in my tests i found that that is trustful and the response also is very fast.
Another good advantage is that the process to get a Key for use Geonames services it's quite simple.
I left you the link of the services that i'm talking about.
List of available services:
http://www.geonames.org/export/web-services.html
Hope it helps many people.
Regards!

Categories