I'm developing iPhone web based app - iphone

So when I type something in the search bar of my app, it should send my latitude and longitude to the web server, so it can return me the nearest place where I can get the searched tag. I'm new to Titanium so can anyone help me?

You are basically sending a request via HTTP to your webservice and use the result (Take the function from #Muhammad Zeeshan to get longitude/latitude):
var xhr = Titanium.Network.createHTTPClient();
// write file on success
xhr.onload = function(){
var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,this.apiFile);
f.write(this.responseData);
};
// error handling
xhr.onerror = function(){
Ti.API.error(this.status + ' - ' + this.statusText);
};
// open the client (and test HTTPS)
xhr.open('GET','http://example.com/api/?longitude=' + longitude + '&latitude=' + latitude);
// send the data
xhr.send();
// read file and return json
var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, this.apiFile);
var contents = f.read();
var yourJson = JSON.parse(contents);
On the server Side you need some Webservice to talk to (you didn't specify a language you use on the server), but I presume you get the data via a MySQL database (others should be similar):
SELECT ((ACOS(SIN($lat * PI() / 180) * SIN(lat * PI() / 180) + COS($latitude * PI() / 180) * COS(lat * PI() / 180) * COS(($longitude – lon) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS `distance` FROM `locations` HAVING `distance`<=’10′ ORDER BY `distance` ASC
Add additional WHERE clauses to filter by tag as well.

If you are using TiStudio instead of TiDeveloper you can use the sample project GPS to start with. It's bundled in the download so just fire it up and use that code as a working copy to learn from.

Related

Find location with in 1 mile in postgresql query

I have 2 tables 1 is for facility and 1 is for customer. both contained latitude longitude we want query to fetch customer's available with in 1 miles of facility. We don't want to use postgres function like ST_Distance. Any alternate query to achieve it.
Iterate java list of location and used haversine formula to calculate distance in miles
private double distance(double LatOne, double LonOne, double LatTwo, double LonTwo) {
LonOne = Math.toRadians(LonOne);
LonTwo = Math.toRadians(LonTwo);
LatOne = Math.toRadians(LatOne);
LatTwo = Math.toRadians(LatTwo);
double deltaLon = LonTwo - LonOne;
double deltaLat = LatTwo - LatOne;
double formula = Math.pow(Math.sin(deltaLat / 2), 2)+ Math.cos(LatOne) * Math.cos(LatTwo)* Math.pow(Math.sin(deltaLon / 2),2);
double fOutput = 2 * Math.asin(Math.sqrt(formula));
return (fOutput * 3956) ;
}

How do you make an equation in swift with multiple variables? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am trying to make something and I am trying to use an equation in Swift. This is the equation:
((((((((((calculation.d/100)/2) * Int(0.98) * (sqrt(calculation.vbottle * 1005.967 * (calculation.psi + (calculation.psi * (calculation.vbottle - (calculation.vol/1000))/2)/2) * ((((calculation.d/100)/2) * Int(0.98) * (sqrt(calculation.vbottle * 1005.967 * (calculation.psi + (calculation.psi * (calculation.vbottle - (calculation.vol/1000))/2)/2) /1005.967 * (3.14159 * ((d / 100)/2) * ((d / 100)/2)))) - (((calculation.mempty/1000) + ((calculation.vol * 0.972)/1000))/2) * calculation.g) / (((calculation.mempty/1000) + ((calculation.vol * 0.972)/1000))/2)) * ((1005.967 * (calculation.vol / 1000000) / (((calculation.d/100)/2) * Int(0.98) * (sqrt(calculation.vbottle * 1005.967 * (calculation.psi + (calculation.psi * (calculation.vbottle - (calculation.vol/1000))/2)/2) * 6894.76))))) * 2) sin 2(calculation.theta) / calculation.g) * (1 - calculation.dc))
This might seem complicated but if you understand it, it isn't (for a computer). All the variables have been defined and the error message says:
Missing argument for parameter 'verbatim' in call and another error that says: Unterminated string literal
I am unsure why and whenever I google it up, an actual result never comes up and when one does, it's always about Playgrounds, not the actual Swift that is used to make apps and stuff.
EDIT:
Here is the reproducible example:
'''
//
// ContentView.swift
// MRE File
//
// Created by Go Peter on 2021/06/18.
//
import SwiftUI
struct Calculation {
var vbottle:Int = 2
var g:Int = Int(9.807)
var vol:Int = 250
var d:Int = 2
var theta:Int = 90
var psi:Int = 40
var mempty:Int = 70
}
struct ContentView: View {
var body: some View {
Text("\(((((((((((calculation.d/100)/2) * Int(0.98) * (sqrt(calculation.vbottle * 1005.967 * (calculation.psi + (calculation.psi * (calculation.vbottle - (calculation.vol/1000))/2)/2) * ((((calculation.d/100)/2) * Int(0.98) * (sqrt(calculation.vbottle * 1005.967 * (calculation.psi + (calculation.psi * (calculation.vbottle - (calculation.vol/1000))/2)/2) /1005.967 * (3.14159 * ((d / 100)/2) * ((d / 100)/2)))) - (((calculation.mempty/1000) + ((calculation.vol * 0.972)/1000))/2) * calculation.g) / (((calculation.mempty/1000) + ((calculation.vol * 0.972)/1000))/2)) * ((1005.967 * (calculation.vol / 1000000) / (((calculation.d/100)/2) * Int(0.98) * (sqrt(calculation.vbottle * 1005.967 * (calculation.psi + (calculation.psi * (calculation.vbottle - (calculation.vol/1000))/2)/2) * 6894.76))))) * 2) sin 2(calculation.theta) / calculation.g) * (1 - calculation.dc)))m")
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
'''
There are a few errors I see:
You need a space in /1005.967 after the / operator -- there are likely other spots like this as well.
Near the end of the expression, you have a floating sin that doesn't have an operator before it and isn't followed with parenthesis at all
There are many spots where you refer to just d, but probably mean calculation.d
You have a mismatched number of open and closed parenthesis.
You don't actually have a Calculation property defined on your view, so there's nothing to do the calculations on yet.
Because I don't know the intent of some of this stuff, I can't actually fix it for you. But, I'd recommend trying to clean up the code a little -- at the least, it'll make it easier to debug.
To start, move this out of the interpolated String and into a computed property:
var calc : Int { //Int? -- see comment about this
//your calculation here
}
var body: some View {
Text("\(calc)m")
}
Then, I'd break the calculation up into much smaller expressions that are more readable and would let you find your errors more easily than trying to sift through so many parenthesis, etc. Even a computer can theoretically handle a long, tough-to-read expression, it makes it a challenging debugging issue for us humans.
I'd also be really surprised if you truly want Int for these properties. You have a bunch of spots where you're doing things like Int(0.98), which doesn't make sense, because it'll get rounded to 1. Perhaps you instead want to use Double or Float for everything? You'll see as you start breaking the instructions up and the compiler starts to find more errors once it can parse everything correctly that you're going to end up with type mismatches between things like Int and Double in the existing expressions.

What is the difference between querying by distance and using the earthdistance module in postgres?

In postgres, there are two ways that I know of to query based on distance.
The first is "querying by distance" using a particular algorithm (as seen here http://daynebatten.com/2015/09/latitude-longitude-distance-sql/).
SELECT zcta.*,
3958.755864232 * 2 *
ASIN(SQRT(POWER(SIN((41.318301 - zcta.latitude) *
PI() / 180 / 2), 2) + COS(41.318301 * PI() / 180) *
COS(zcta.latitude * PI() / 180) *
POWER(SIN((-83.6174935 - zcta.longitude) *
PI() / 180 / 2), 2))) AS distance,
MOD(CAST((ATAN2( ((zcta.longitude - -83.6174935) / 57.2957795),
((zcta.latitude - 41.318301) / 57.2957795)) *
57.2957795) + 360 AS decimal), 360) AS bearing
FROM "zcta"
WHERE (zcta.latitude BETWEEN 40.59464208444576
AND 42.04195991555424
AND zcta.longitude BETWEEN -84.58101890178294
AND -82.65396809821705
AND (3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((41.318301 - zcta.latitude) *
PI() / 180 / 2), 2) + COS(41.318301 * PI() / 180) * COS(zcta.latitude *
PI() / 180) * POWER(SIN((-83.6174935 - zcta.longitude) *
PI() / 180 / 2), 2))))
BETWEEN 0.0
AND 50)
ORDER BY distance ASC
The second is the earthdistance module (https://www.postgresql.org/docs/8.3/static/earthdistance.html) for geospatial queries.
select *
from zcta
where earth_box(ll_to_earth(41.318301, -83.6174935), 63067.2) #>
ll_to_earth(zcta.latitude, zcta.longitude)
What is the difference here? Which is better to use? Which is more accurate? How do each work?
A bit late answer, but my addition. The first is a roll-your-own solution, whereas the latter is a Postgresql extension or module called earthdistance. To install, in psql:
CREATE EXTENSION IF NOT EXISTS earthdistance;
You might need to do the same for cube first as they share resources. Or...
CREATE EXTENSION IF NOT EXISTS earthdistance CASCADE;
Here is the documentation.

Distance calculation in query with slick database

I am using slick with scala to find the distance of the user to the closes spot. But I'm not sure how to properly do this with slick. I've tried different ways, but I keep getting errors with symbols or not getting the appropriate answers. The SQL query that I'm looking to convert is:
SELECT *, 3956 * 2 * sin(sqrt( pow(sin((spot_lat -
abs(lat)) * Pi/180 / 2),2) + cos(spot_lat * Pi/180 ) * cos(abs(lat) * Pi/180) *
pow(sin((spot_lon – lon) * Pi/180 / 2), 2) )) as distance
FROM Spots
Where lat and lon have been given as inputs
First, define your database trig functions so that they can be used by slick:
val dbSin = SimpleFunction.unary[Double, Double]("sin")
val dbCos = SimpleFunction.unary[Double, Double]("cos")
val dbSqrt = SimpleFunction.unary[Double, Double]("sqrt")
val dbPow = SimpleFunction.binary[Double, Double, Double]("pow")
val dbAbs = SimpleFunction.unary[Double, Double]("abs")
When you use these functions in a slick query, it generates the SQL query with calls to the database function. Also, make sure that the trig functions are defined in the database you are using. Read more about it at: http://slick.typesafe.com/doc/2.1.0/userdefined.html
Now you can use these functions in your query:
import scala.math._
def distance(lat: Double, lon: Double) = globals.spots map { spot =>
3956 * 2 * dbSin(dbSqrt(dbPow(dbSin((spot.splot_lat - Pi / 180.0 / 2.0), 2) +
dbCos(spot.spot_lat * Pi / 180.0) * dbCos(dbAbs(lat) * Pi / 180.0) *
dbPow(dbSin((spot.spot_lon - lon) * Pi / 180.0 / 2.0), 2)))
}
Please double check that I copied the equation correctly.

Accessing full size (851x315) timeline cover photo via graph api with offset crop

This is a continuous question of https://stackoverflow.com/a/21074207/3269910
the solution seems fine c0.0.851.315
but the c0.0 values are pointing to 0x0 position of the images
what i want is the image with offset_y . if an offset_y: 20,
ie, i want the same size and part of image, what i am seeing in my page timeline in a api call.
Is it possible to do this ?
Note: if i change c0.0 the zero value to some other then it is pointing to pixels but offset_y is % i think.
Thanks for your help.
I had discussion with Facebook Platform Team;
They are not ready to share the manual calculation to us.
and yes, it is possible to calculate value of c0.XXXX manually using your API offset_y value.
Here is my PHP code.
$fb_page_cover_json = json_decode( file_get_contents( 'https://graph.facebook.com/11111111?fields=cover'));
$fb_cover_image_url = $fb_page_cover_json->cover->source;
$image_info = getimagesize($fb_cover_image_url);
$image_width = $image_info[0];
$image_height = $image_info[1];
echo getTop($fb_page_cover_json->cover->offset_y,$image_width,$image_height);
function getTop($offset_y,$image_width,$image_height) {
$cover_w = 851;
$cover_h = 315;
$img_w = $image_width;
$img_h = $image_height;
$real_img_h = ($cover_w * $img_h / $img_w) - $cover_h;
$result = ($real_img_h * $offset_y / 100 * -1);
return floor($result);
}
The method getTop will return the c0.XXX value.
Hope this helps some one.