Trouble to get near by ATM list - ios

I am using Google Map API to get the Latitudes and Longitude of near By ATM. I have get Google API key by my Google credentials. I am requesting by my app with Code
NSString *url = [NSString stringWithFormat:#"https://maps.googleapis.com/maps/api/place/search /json?location=%f,%f&radius=%#&types=%#&sensor=true&key=%#", currentCentre.latitude, currentCentre.longitude, [NSString stringWithFormat:#"%i", currenDist], googleType, kGOOGLE_API_KEY];
NSURL *googleRequestURL=[NSURL URLWithString:url];
NSData* data = [NSData dataWithContentsOfURL: googleRequestURL];
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
and I am getting
{
"debug_info" = (
);
"html_attributions" = (
);
results = (
);
status = "REQUEST_DENIED";
} as response.
please let me know what to do??

I think you need to use the Google Place API for getting the ATM location look for the link below
How to get 20+ result from Google Places API?

Related

Google Maps decoded GMSPath incorrect iOS

So, I use the directions API in my app. However, when I decode the polyline from the response, it does not show up correctly. Here is my code:
//Construct request URL
NSString *urlString = [NSString stringWithFormat:
#"%#?origin=%f,%f&destination=%f,%f&sensor=true&key=%#",
#"https://maps.googleapis.com/maps/api/directions/json",
userMarker.position.latitude,
userMarker.position.longitude,
place.coordinate.latitude,
place.coordinate.longitude,
#"AIzaSyDrtHA-AMiVVylUPcp46_Vf1eZJJFBwRCY"];
NSURL *directionsURL = [NSURL URLWithString:urlString];
//Get directions in JSON format
dispatch_async(dispatch_get_main_queue(), ^{
NSData* data = [NSData dataWithContentsOfURL:directionsURL];
NSError* error;
if(data){
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
//Parse JSON and plot route on map
NSDictionary *routes = [json objectForKey:#"routes"][0];
NSDictionary *route = [routes objectForKey:#"overview_polyline"];
NSString *overview_route = [route objectForKey:#"points"];
//Clear map from previous polylines
[self.mapView clear];
//Make polyline
GMSPath *path = [GMSPath pathFromEncodedPath:overview_route];
GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
polyline.strokeWidth = 4;
polyline.strokeColor = [UIColor darkGrayColor];
polyline.map = self.mapView;
});
And here is the polyline:
As you can,see it does not follow the road properly. However, to me it seems as if there are not enough points to have proper bends.
EDIT: This only happens 50% of the time, sometimes it shows up correctly, sometimes it does not.
What could I be doing wrong?
Going by this (which is using the js API instead of the json API that you're using):
Get a polyline from Google maps directions V3
It looks like you should use the lines in each of the steps of the legs in the response, instead of the overview_polyline which is probably meant to be just a rough approximate line.

parse JSON weather object from Open Weather Map API using AFNetworking

I am using AFNetworking to retrieve information about the weather for a specific location, e.g:
http://api.openweathermap.org/data/2.5/weather?q={New%20York%20City}
I am using the AFNetworking framework but I am having the problems parsing some objects of the JSON.
If I have an NSDictionary with the MAIN object information from the JSON:
NSDictionay *main = [responseObject objectForKey:#"main"];
If I log the main NSDictionary I will get the following valid output:
"main":{
"temp":296.78;
"pressure":1011;
"humidity":69;
"temp_min":293.15;
"temp_max":299.82
};
Although if I create a NSDictionary containing the weather object I will get the following information whenever logging it to the console:
NSDictionay *weather = [responseObject objectForKey:#"weather"];
"weather":(
{
"id":801;
"main":"Clouds";
"description":"few clouds";
"icon":"02d"
}
);
The parsed information contains ( brackets instead of [ from the original response. This does not allow me to correctly access the inside attributes of the weather object.
Summing up, I am able to access all the inside variables of the MAIN object, but I cannot access the attributes of the Weather object (e.g access the icon attribute).
Can someone help me with this ?
Thank you,
You calling your service as below.
NSString *query = #"http://api.openweathermap.org/data/2.5/weather?q={New%20York%20City}";
NSLog(#"%#",query);
query = [query stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData *jsonData = [[NSString stringWithContentsOfURL:[NSURL URLWithString:query] encoding:NSUTF8StringEncoding error:nil] dataUsingEncoding:NSUTF8StringEncoding];
NSError *error = nil;
NSDictionary *results = jsonData ? [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error] : nil;
Now print Response :
NSLog(#"weather==%#",[results objectForKey:#"weather"]);
NSLog(#"description==%#",[[[results objectForKey:#"weather"] objectAtIndex:0] objectForKey:#"description"]);
NSLog(#"icon==%#",[[[results objectForKey:#"weather"] objectAtIndex:0] objectForKey:#"icon"]);
NSLog(#"id==%#",[[[results objectForKey:#"weather"] objectAtIndex:0] objectForKey:#"id"]);
NSLog(#"main==%#",[[[results objectForKey:#"weather"] objectAtIndex:0] objectForKey:#"main"]);
Your Response is :
whwather==(
{
description = "sky is clear";
icon = 01d;
id= 800;
main= Clear;
}
)
description== "sky is clear";
icon == 01d;
id == 800;
main == Clear;

Convert NSString to NSData in Chinese case

everybody, I know this question is lots of people to ask and have lots of answer in stack overflow, but In my case, I try to get the JSON format from here, and code like this:
// Get JSON
NSString* path = #"http://opendata.epa.gov.tw/ws/Data/UV/?format=json";
NSURL* url = [NSURL URLWithString:path];
NSString* jsonString = [[NSString alloc]initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:nil];
NSArray* arrayResult = dic;
NSDictionary* resultDic = [arrayResult objectAtIndex:0];
NSLog(#"resultDic:%#", resultDic);
NSString* uv = [resultDic objectForKey:#"UVI"];
NSLog(#"UVI:%#", uv);
NSString* publicshedTime = [resultDic objectForKey:#"PublishTime"];
NSLog(#"PublishTime:%#", publicshedTime);
NSLog(#"中文");
I used :
NSString* jsonString = [[NSString alloc]initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
But it's not effective to this case, I don't know why.
In console, I can see the Chinese words correctly, and in debugger I can see the "jsonString" content is correct, but I don't know why I convert it to NSData, the content will be wrong, and it output like this :
2014-05-27 03:24:09.307 Tab demo[3014:60b] resultDic:{
County = "\U5609\U7fa9\U5e02";
PublishAgency = "\U4e2d\U592e\U6c23\U8c61\U5c40";
PublishTime = "2014-05-27 03:00";
SiteName = "\U5609\U7fa9";
TWD97Lat = "23,29,52";
TWD97Lon = "120,25,28";
UVI = 0;
}
2014-05-27 03:24:09.308 Tab demo[3014:60b] UVI:0
2014-05-27 03:24:09.308 Tab demo[3014:60b] PublishTime:2014-05-27 03:00
2014-05-27 03:24:09.308 Tab demo[3014:60b] 中文
For sure, the other data without Chinese words is display correctly.
This wrong looks like encoding wrong, but I don't know how to fix it correctly, can anyone tell me the way to fix it, Thanks a lot!
Don't compare contents of objects by their appearance in the console when logged using NSLog. That's unreliable because the algorithm to convert objects into strings is not documented, not strict and is for debugging purposes only.
Compare objects by using the compare: method.

Display all JSON data from NSDictionary to UITableview

I am fairly new to iOS development, been at it for 2 years or so. I have developed an app for my Litecoin mining pool that is able to pull data from the API url in JSON format. I am able to get individual variables from JSON, but I want to display all of the data from the "workers" block into a UITableView. Here is an example of the JSON:
{"username":"n00bminer","balance":"0","total_hashrate":"1429","payout_history":"0.71828344","round_shares":"96908","workers":
{
"n00bminer.1":{"hashrate":"998","last_share_timestamp":"1392906308","accepted_shares":"84755","stale_shares":"913"},
"n00bminer.cpu":
{"hashrate":"0","accepted_shares":"7891","stale_shares":"11"},
"n00bminer.2":{"hashrate":"431","last_share_timestamp":"1392906300","accepted_shares":"13285","stale_shares":"118"}
}}
(bolded is the part that I need) Live data: http://www.ielitepool.com/api.php?api_key=9d9be4bd59eb8de59f1cac981099c43e866b0cc07a5555dd922c9efc08ac31a1
I request this using:
NSData* apiData = [NSData dataWithContentsOfURL:
[NSURL URLWithString:[[NSUserDefaults standardUserDefaults] valueForKey:#"api_url"]]
];
NSDictionary* json = nil;
if (apiData) {
json = [NSJSONSerialization
JSONObjectWithData:apiData
options:kNilOptions
error:nil];
}
and get individual variables using:
float total_hashrate = [[json valueForKey:#"total_hashrate"] floatValue];
How can I get all of the workers, as well as their stats in one UITableView. For example, I don't want the worker name to only display, then have it lead to another UITableViewController, but rather the name and then the stats underneath (hashrate, accepted_shares, etc.)
Following code will print worker's information:
[json[#"workers"] enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSDictionary *obj, BOOL *stop) {
NSLog(#"worker = %# : hashrate = %f last_share_timestamp = %f",
key, [obj[#"hashrate"] floatValue], [obj[#"last_share_timestamp"] floatValue] );
}];
If you will have troubles displaying info in table, please create separate question with code sample.

Adding bounds to google maps search string causes nil results

I added a search box to my mapping app a little while back which was doing very simple address searching with minimal options.
http://maps.googleapis.com/maps/api/geocode/json?address=sydney+grove&sensor=true
I've just added bounds parameters using the current screen viewport like this
http://maps.googleapis.com/maps/api/geocode/json?address=sydney+grove&bounds=51.198083,-0.830125|51.799930,0.576125&sensor=true
it returns a result when pasted into a browser but always a nil result if entered in code (jsonResponse always equals nil)
-(void) doGeocodingBasedOnStringUsingGoogle:(NSString*) searchString {
GMSCoordinateBounds* bounds=[[self datasource] searchBounds];
//CREATE LOOKUP STRING
NSString *lookUpString = [NSString
stringWithFormat:#"http://maps.googleapis.com/maps/api/geocode/json?
address=%#&bounds=%f,%f|%f,%f&sensor=true",
searchString,
bounds.southWest.latitude,
bounds.southWest.longitude,
bounds.northEast.latitude,
bounds.northEast.longitude];
lookUpString = [lookUpString stringByReplacingOccurrencesOfString:#" "
withString:#"+"];
//SEARCH FOR RESULTS
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSError *error = nil;
NSData *jsonResponse = [NSData dataWithContentsOfURL:[NSURL URLWithString:lookUpString]];
if (jsonResponse) {
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonResponse options:kNilOptions error:&error];
self.searchResults = [jsonDict valueForKey:#"results"];
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableview reloadData];
});
});
}
This code was fine before I added the bounds condition and is fine if I remove so I'm really out of ideas
I think you need to replace the | with a %7C, for example see here:
How to make an NSURL that contains a | (pipe character)?
As mentioned in the comments on the answer, you could look into using stringByAddingPercentEscapesUsingEncoding for a method to escape the URL for you (so eg you wouldn't need to replace spaces with a +, etc).

Resources