Blackberry <location> lat and lon from googlemaps lat and lon - blackberry

I have coordinates like this:
<googlemapsX>32.733611</googlemapsX>
<googlemapsY>-117.189722</googlemapsY>
And i want to show them like this:
String document = "<lbs>
<location lat='LAT' lon='LON'/>
</lbs>";
But LAT and LON must be integers, how to convert them?

To convert them into the lat/lon that BlackBerry uses in its document, you can just multiply them by 100000 to get the correct value.
int lat = 32.733611 * 100000;
int lon = -117.189772 * 100000;
And use those values as your LAT and LON

lat, long can be values like 32.733611, -117.189772 (double values)
int latitude = (int) (lat * 1e6);
int longitude = (int) (long * 1e6);

Related

How to use complex number to plot an Argand Diagram?

I like to create an Argand Diagram from a set of complex number by using Objective-C.
Are there any solution or template that are available that can help me to do this?
Can anyone recommend an approach?
Similar youtube link is provided but it's an android program: https://www.youtube.com/watch?v=s0oTeZ12_ig
- (void)ComputeABCD
{
double designFrequency = 1e9;
double simulateFrequency = 1.5e9;
double pi = 3.14159265359;
double omega = 2*pi*simulateFrequency;
double thetarad=1;
complex double cascadedA=1+0*I;
complex double cascadedB=0+0*I;
complex double cascadedC=0+0*I;
complex double cascadedD=1+0*I;
complex double A=0+0*I;
complex double B=0+0*I;
complex double C=0+0*I;
complex double D=0+0*I;
complex double newA=0+0*I;
complex double newB=0+0*I;
complex double newC=0+0*I;
complex double newD=0+0*I;
int index=0;
for (id element in storeImpedance)
{
id element2 = [storeLength objectAtIndex:index];
id element3 = [storeIndex objectAtIndex:index];
NSNumber *numberImpedance = (NSNumber *)element;
double valueImpedance = [numberImpedance doubleValue];
NSNumber *numberLength = (NSNumber *)element2;
double valueLength = [numberLength doubleValue];
NSNumber *numberIndex = (NSNumber *)element3;
NSInteger valueIndex = [numberIndex integerValue];
++index;
if (valueIndex==0)
{
thetarad=valueLength*pi/180*simulateFrequency/designFrequency;
A=cos(thetarad);
B=I*valueImpedance*sin(thetarad);
C=I*sin(thetarad)/valueImpedance;
D=cos(thetarad);
}
if (valueIndex==1)
{
thetarad=valueLength*pi/180*simulateFrequency/designFrequency;
A=1;
B=0;
C=(I*tan(thetarad))/valueImpedance;
D=1;
}
newA=cascadedA*A+cascadedB*C;
newB=cascadedA*B+cascadedB*D;
newC=cascadedC*A+cascadedD*C;
newD=cascadedC*B+cascadedD*D;
cascadedA=newA;
cascadedB=newB;
cascadedC=newC;
cascadedD=newD;
}
printf("The MAT ABCD is:\n");
printf("%g + %gi\n", creal(cascadedA), cimag(cascadedA));
printf("%g + %gi\n", creal(cascadedB), cimag(cascadedB));
printf("%g + %gi\n", creal(cascadedC), cimag(cascadedC));
printf("%g + %gi\n", creal(cascadedD), cimag(cascadedD));
}

Getting wrong distance between locations in iOS?

Hi aim using following method to find out the distance between my current location and the locations(lat and longitude) stored in NSMutableArray value.
But am getting wrong distance..Please help..
My code
-(void)getDistancetoShow:(NSMutableArray *)newArray{
CLLocationCoordinate2D coordinateUser = [self getLocation];
float _lat,_long;
first_Loc = [[CLLocation alloc] initWithLatitude:coordinateUser.latitude longitude:coordinateUser.longitude];
for(int p=0;p<[[newArray1 valueForKey:#"Name"] count];p++){
_lat=[[[newArray1 valueForKey:#"Latitude"] objectAtIndex:p]floatValue];
_long=[[[newArray1 valueForKey:#"Longitude"] objectAtIndex:p]floatValue];
second_loc=[[CLLocation alloc] initWithLatitude:_lat longitude:_long];
showDistance=[second_loc distanceFromLocation:first_Loc]/1000;
[distanceToDispaly addObject:[NSString stringWithFormat:#"%.2f KM",showDistance]];
}
NSLog(#"first=%#, second=%#", first_Loc, second_loc);
}
Latitudes in array
(
"47.0735010448824",
"47.0564688100431",
" 47.0582514311038",
"47.0587640538326",
"47.0569233603454",
"47.0541853132569",
"47.0542029215138",
"47.0544259594592",
"47.0560264547367",
" 47.0576532159776",
" 47.0550023679218",
"47.0342030007379",
"47.0746263896213",
" 47.0740256635512",
"47.0524765957921",
"47.0606287049051",
"47.0539691521825",
"47.0542799159057",
"47.0651001682846",
"47.0536948902097",
"47.0525973335309",
"47.0389265414812",
"47.0761811267051",
"47.0668801601942",
"47.0614859079241",
"47.0579433468181",
"47.0718998779465"
)
and longitude in array
(
"21.9154175327011",
"21.9312065669748",
"21.9337414545594",
" 21.9346772505188",
" 21.9300587945685",
"21.9363460105132",
"21.9362081709222",
"21.9343042603097",
"21.939485335992",
"21.9320057169724",
"21.9300799002643",
"21.9485373571669",
"21.9310667367526",
"21.9318507902135",
"21.9192195298473",
"21.9195273899529",
"21.9329595191441",
"21.9292015418841",
"21.9219452321208",
"21.9098849252041",
"21.9074768948561",
"21.9424499491422",
"21.9151458954504",
"21.9304346568769",
"21.9305973807911",
"21.9331511189507",
"21.9159872752442"
)
but the real distance in something like staring with 9**** but am getiing now 5***
CLLocation gives you crow(straight) distance between two places. I thinks you are getting crow distance.
First take coordinate of two places and find distance between them.
then search crow distance between those two coordinate.
hope this will help
Once you get the two coordinates you can calculate the distance between them using this piece of code (taken from here):
- (NSNumber*)calculateDistanceInMetersBetweenCoord:(CLLocationCoordinate2D)coord1 coord:(CLLocationCoordinate2D)coord2 {
NSInteger nRadius = 6371; // Earth's radius in Kilometers
double latDiff = (coord2.latitude - coord1.latitude) * (M_PI/180);
double lonDiff = (coord2.longitude - coord1.longitude) * (M_PI/180);
double lat1InRadians = coord1.latitude * (M_PI/180);
double lat2InRadians = coord2.latitude * (M_PI/180);
double nA = pow ( sin(latDiff/2), 2 ) + cos(lat1InRadians) * cos(lat2InRadians) * pow ( sin(lonDiff/2), 2 );
double nC = 2 * atan2( sqrt(nA), sqrt( 1 - nA ));
double nD = nRadius * nC;
// convert to meters
return #(nD*1000);
}
Hope this helps!
To get distance from array of points in Swift use below reduce method.
Here locations is array of type CLLocation.
let calculatedDistance = locations.reduce((0, locations[0])) { ($0.0 + $0.1.distance(from: $1), $1)}.0

Which dataType will return the value (37.961498) as it is ? Double didn't work

Which dataType will return the value (37.961498) as it is?
I am working in MapView. Where, at present I am setting annotation and hence I have to set CLLocationCoordinate2DMake with some lat and lng parsed from googleAPI (Eg:lat = "37.9615000" and lng = "60.5566000") . I have the values in string, hence I converted the sting to double and then assigned But it didn't help. Please some one help me.
double int1=[num1s floatValue];
double int2=[num2s floatValue];
NSLog(#" %f %f ",int1,int2);
annotation.coordinate=CLLocationCoordinate2DMake(int1, int2);
actual values are: 37.9615000,60.5566000
Values I get in NSLog: 37.961498, 60.556599
Thanks in advance.
More explanation and reason why I need exact value:
I need to set annotation for china With its lat and lan ( lat = "53.5609740";
lng = "134.7728099")
But while converting again from string the value changes
When converting a string to a double you should be using doubleValue instead of floatValue
double int1=[num1s doubleValue];
double int2=[num2s doubleValue];
NSLog(#" %f %f ",int1,int2);
annotation.coordinate=CLLocationCoordinate2DMake(int1, int2);
also not critical but I wouldn't recommend naming your doubles as int1 and int2 that is likely to cause you confusion at some point
annotation.coordinate=CLLocationCoordinate2DMake([nums doubleValue], [int2 doubleValue]);
Try this
double int1=[num1s floatValue];
double int2=[num2s floatValue];
NSLog(#" %0.7f %0.7f ",int1,int2);
annotation.coordinate=CLLocationCoordinate2DMake(int1, int2);
Read String Format Specifiers for more info.

collect 2 arrays CLLocationDegree in one array of CLLocationCoordinate2D

I need to draw a polyline on google maps on ios , I have latitudes and longitudes in seperated arrays and I want to collect them in one array of CLLocationCoordinate2D, so plz help me to write the code that make this collection
CLLocationCoordinate2D *coords = malloc(coordinateCount * sizeof(CLLocationCoordinate2D));
for(size_t i = 0; i < coordinateCount; ++i)
{
coords[i].latitude = latitudes[i];
coords[i].longitude = longitudes[i];
}
// ... Use the array ...
free(coords);

Is there any easy way to make GPS coordinates coarse?

I'm working on an iPhone app that is using GPS coordinates for leaderboards. I don't need the coordinates to be exact --- actually I don't ever want the coordinates to be exact, to protect user privacy.
I am specifying kCLLocationAccuracyThreeKilometers for desiredAccuracy, but when the GPS is active it seems it can also pick up the exact location when the device has it.
QUESTION: Is there any easy algorithm I can use to make the GPS data more coarse? Say, make it granular to 3km.
If I just scale the numbers up and remove decimal points and scale them down again it will make it more coarse in some parts of the world than others.
Thanks!
While Mark's answer above was a useful, it still did not yield a formula with consistent results because it relied on a random number generator.
My buddy provided the best answer for this:
Round the lat,lon to the nearest significant figure depending on the granularity, but this would result in all the lat/lons near a certain location, wind up in the same location. This method would use the distance between two points in lat/lon to calculate the rounding of the lat lon. Use the same formula below and set the course to 0, then the distance is your distance granularity. Calculate the resulting new lat/lon subtract the two lat/lons to get the rounding amount for lat. Then set the heading to 90 and recalculate and subtract the new lat/lon from the old to get the rounding amount for lon.
And here's the C++ code:
class LocationUtility
{
public: static Location getLocationNow()
{
Location location;
if(context != null)
{
double latitude = 0;
double longitude = 0;
::of_getCurrentLocation(&latitude, &longitude);
location.setLatitude(latitude);
location.setLongitude(longitude);
location = makeLocationCoarse(location);
}
return location;
}
public: static Location makeLocationCoarse(const Location& location)
{
double granularityInMeters = 3 * 1000;
return makeLocationCoarse(location, granularityInMeters);
}
public: static Location makeLocationCoarse(const Location& location,
double granularityInMeters)
{
Location courseLocation;
if(location.getLatitude() == (double)0 &&
location.getLongitude() == (double)0)
{
// Special marker, don't bother.
}
else
{
double granularityLat = 0;
double granularityLon = 0;
{
// Calculate granularityLat
{
double angleUpInRadians = 0;
Location newLocationUp = getLocationOffsetBy(location,
granularityInMeters, angleUpInRadians);
granularityLat = location.getLatitude() -
newLocationUp.getLatitude();
if(granularityLat < (double)0)
{
granularityLat = -granularityLat;
}
}
// Calculate granularityLon
{
double angleRightInRadians = 1.57079633;
Location newLocationRight = getLocationOffsetBy(location,
granularityInMeters, angleRightInRadians);
granularityLon = location.getLongitude() -
newLocationRight.getLongitude();
if(granularityLon < (double)0)
{
granularityLon = -granularityLon;
}
}
}
double courseLatitude = location.getLatitude();
double courseLongitude = location.getLongitude();
{
if(granularityLon == (double)0 || granularityLat == (double)0)
{
courseLatitude = 0;
courseLongitude = 0;
}
else
{
courseLatitude = (int)(courseLatitude / granularityLat) *
granularityLat;
courseLongitude = (int)(courseLongitude / granularityLon) *
granularityLon;
}
}
courseLocation.setLatitude(courseLatitude);
courseLocation.setLongitude(courseLongitude);
}
return courseLocation;
}
// http://www.movable-type.co.uk/scripts/latlong.html
private: static Location getLocationOffsetBy(const Location& location,
double offsetInMeters, double angleInRadians)
{
Location newLocation;
double lat1 = location.getLatitude();
double lon1 = location.getLongitude();
lat1 = deg2rad(lat1);
lon1 = deg2rad(lon1);
double distanceKm = offsetInMeters / (double)1000;
const double earthRadiusKm = 6371;
double lat2 = asin( sin(lat1)*cos(distanceKm/earthRadiusKm) +
cos(lat1)*sin(distanceKm/earthRadiusKm)*cos(angleInRadians) );
double lon2 = lon1 +
atan2(sin(angleInRadians)*sin(distanceKm/earthRadiusKm)*cos(lat1),
cos(distanceKm/earthRadiusKm)-sin(lat1)*sin(lat2));
lat2 = rad2deg(lat2);
lon2 = rad2deg(lon2);
newLocation.setLatitude(lat2);
newLocation.setLongitude(lon2);
return newLocation;
}
private: static double rad2deg(double radians)
{
static double ratio = (double)(180.0 / 3.141592653589793238);
return radians * ratio;
}
private: static double deg2rad(double radians)
{
static double ratio = (double)(180.0 / 3.141592653589793238);
return radians / ratio;
}
/*
public: static void testCoarse()
{
Location vancouver(49.2445, -123.099146);
Location vancouver2 = makeLocationCoarse(vancouver);
Location korea(37.423938, 126.692488);
Location korea2 = makeLocationCoarse(korea);
Location hiroshima(34.3937, 132.464);
Location hiroshima2 = makeLocationCoarse(hiroshima);
Location zagreb(45.791958, 15.935786);
Location zagreb2 = makeLocationCoarse(zagreb);
Location anchorage(61.367778, -149.900208);
Location anchorage2 = makeLocationCoarse(anchorage);
}*/
};
This is very similar to a previous question
Rounding Lat and Long to Show Approximate Location in Google Maps
If you assume the earth is a sphere (probably adequate for this problem), then you just need to calculate a location which is a certain angular distance from the given latitude and longitude. Pick a distance and a (random) direction, and calculate the new location by using the distance formula.
There's good discussion of the opposite problem (distance between two latitude/longitude points) here: Link
It ought to be relatively straightforward to go from there to finding a point a specified distance away from the given point.
The answer from swinefeaster is ok, but there is no need for such complex maths. If you're rounding to a grid, then the latitude changes by constant amounts at all points on the planet. Longitude changes by different amounts according to how far you are from the equator.
The following code snaps latitude and longitude to an arbitrary grid size
double EARTH_RADIUS_KM = 6371;
double GRID_SIZE_KM = 1.6; // <----- Our grid size in km..
double DEGREES_LAT_GRID = Math.toDegrees(GRID_SIZE_KM / EARTH_RADIUS_KM);
// ^^^^^^ This is constant for a given grid size.
public Location snapToGrid(Location myLoc) {
double cos = Math.cos(Math.toRadians(myLoc.latitude));
double degreesLonGrid = DEGREES_LAT_GRID / cos;
return new Location (
Math.round(myLoc.longitude / degreesLonGrid) * degreesLonGrid,
Math.round(myLoc.latitude / DEGREES_LAT_GRID) * DEGREES_LAT_GRID);
}
Note that this will fail in the case where you are at the Pole (when the cos function approaches zero). Depending on your grid size, the results become unpredictable as you approach a latitude of +/- 90 degrees. Handling this is an exercise left for the reader :)
I try to implemente the solution in Ruby but in my case, the coarse coordinate vs real have a huge difference. the coarse coordinate only change when the lat change but when lat stay the same and long move, coarse remain the same. In case someone can check the code below, perhaps I made a bad coding.
class CoarseLocation
AREA_LIMIT = 1000
class << self
def make_location_coarse(lat, lon)
if lat.nil? && lon.nil?
raise InvalidParamsError
end
location = [lat.to_f, lat.to_f]
new_location_up = get_location_by_offset(location, AREA_LIMIT, 0)
granularityLat = location[0] - new_location_up[0]
if granularityLat < 0
granularityLat = -granularityLat
end
new_location_right = get_location_by_offset(location, AREA_LIMIT, 1.57079633)
granularityLon = location[1] - new_location_right[1]
if(granularityLon < 0)
granularityLon = -granularityLon
end
course_lat = location[0]
course_lon = location[1]
if(granularityLat == 0.0) || (granularityLon == 0.0)
course_lat = 0
course_lon = 0
else
course_lat = (course_lat / granularityLat).to_i * granularityLat
course_lon = (course_lon / granularityLon).to_i * granularityLon
end
[course_lat, course_lon]
end
def get_location_by_offset(location, offset, angle)
lat_radius = location[0] * Math::PI / 180
lon_radius = location[1] * Math::PI / 180
distance = (offset / 1000).to_f
earth_radius = 6371
lat_radius_1 = (Math::asin( Math::sin(lat_radius) * Math::cos(distance/earth_radius) + Math::cos(lat_radius) * Math::sin(distance/earth_radius) * Math::cos(angle))).to_f
lon_radius_1 = (lon_radius + Math::atan2(Math::sin(angle)*Math::sin(distance/earth_radius)*Math::cos(lat_radius), Math::cos(distance/earth_radius) - Math::sin(lat_radius)*Math::sin(lat_radius_1))).to_f
new_lat = lat_radius_1 * 180 / Math::PI
new_lon = lon_radius_1 * 180 / Math::PI
return [new_lat.to_f, new_lon.to_f]
end
end
end
Location field is always ab array of 2 elements in which [0] is lat and [1] is long.

Resources