Android geocoder reverse location - reliable way to get the city? - android

I have searched quite a bit without luck so far.
The Android Geocoder returns the android.location.Address object.
The city, as far as I understood, should be returned in getLocality().
It seems within USA this works well, outside not.
I am writing an international app and struggle to find a solution to find out the city of a geolocation.
Here the output from Czech Republic/Prague :
Address[addressLines=
[0:"Psohlavců 1764/2",
1:"147 00 Prague-Prague 4",
2:"Czech Republic"],
feature=2,
admin=Hlavní město Praha,
sub-admin=Prague,
locality=null,
thoroughfare=Psohlavců,
postalCode=147 00,
countryCode=CZ,
countryName=Czech Republic,
hasLatitude=true,
latitude=50.0276543,
hasLongitude=true,
longitude=14.4183926,
phone=null,
url=null,
extras=null]
locality is null, the city is within sub-admin !
The address itself is ok, so the geocoder server seems to know the city.
Here some ore random EU examples but locality works partly:
Address[addressLines=[0:"Nad lesem 440/34",1:"147 00 Prague-Prague 4",2:"Czech Republic"],feature=34,admin=Hlavní město Praha,sub-admin=Prague,locality=null,thoroughfare=Nad lesem,postalCode=147 00,countryCode=CZ,countryName=Czech Republic,hasLatitude=true,latitude=50.02424,hasLongitude=true,longitude=14.4117568,phone=null,url=null,extras=null]
Address[addressLines=[0:"Hauner Straße 4",1:"84431 Heldenstein",2:"Germany"],feature=4,admin=null,sub-admin=null,locality=Heldenstein,thoroughfare=Hauner Straße,postalCode=84431,countryCode=DE,countryName=Germany,hasLatitude=true,latitude=48.2540274,hasLongitude=true,longitude=12.3413535,phone=null,url=null,extras=null]
Address[addressLines=[0:"Igler Straße",1:"6020 Innsbruck",2:"Austria"],feature=Igler Straße,admin=Tyrol,sub-admin=Innsbruck,locality=Innsbruck,thoroughfare=Igler Straße,postalCode=6020,countryCode=AT,countryName=Austria,hasLatitude=true,latitude=47.2465698,hasLongitude=true,longitude=11.4054237,phone=null,url=null,extras=null]
Address[addressLines=[0:"Durnberg 24",1:"5724 Stuhlfelden",2:"Austria"],feature=24,admin=Salzburg,sub-admin=Zell am See District,locality=null,thoroughfare=Durnberg,postalCode=5724,countryCode=AT,countryName=Austria,hasLatitude=true,latitude=47.3233373,hasLongitude=true,longitude=12.4960482,phone=null,url=null,extras=null]
Address[addressLines=[0:"U Roháčových kasáren 14",1:"100 00 Prague 10",2:"Czech Republic"],feature=14,admin=Hlavní město Praha,sub-admin=Prague,locality=Prague 10,thoroughfare=U Roháčových kasáren,postalCode=null,countryCode=CZ,countryName=Czech Republic,hasLatitude=true,latitude=50.0704092,hasLongitude=true,longitude=14.4673473,phone=null,url=null,extras=null]
Maybe the fault is on me, but to me it seems like depending on the country and area the city will be found in different fields.
However, the address itself mostly seems to be good enough to send a postal letter.
Has someone written a clever function which tries to make more sense out of the Geocoder results ? It's a pity to see that Google has the information stored but does not provide it properly.

Going to close my question, solved it with a workaround.
Using the suggestion from dannyroa
String city="unknown";
if (address.getLocality() != null) city=address.getLocality();
else
if (address.getSubAdminArea() != null) city=address.getSubAdminArea();
This could be further extended by getting the city information out of the second address line.
removing the postal code and taking the remainder, but this information is not unique within a city and could change depending on the district/zone.

Related

Google reverse geocoding returning zero_results

I have an android app that gets the lat&long from the device and uses googles reverse geocoding api to give back an address. Everything was working fine for some time but now all of a sudden I get zero_results/
I replicate it in my browser the request is:
https://maps.googleapis.com/maps/api/geocode/json?latlng=31.783783783783782,35.22879215957073&key=MY_KEY
but if I change it to:
https://maps.googleapis.com/maps/api/geocode/json?latlng=31.78,35.22&key=MY_KEY
which is just shortening the lat&lng to 2 decimals it works, but say if I round it to 2 decimals:
https://maps.googleapis.com/maps/api/geocode/json?latlng=31.78,35.23&key=MY_KEY
It doesnt work!!!
I cant rim it without rounding it, but now i am afraid in another case it wont work, I don't know what is going on with the API, everything was working fine before.
Any help solving this would be appreciated.
Thanks
This is because Google gives no support for disputed areas (such as Kinmen County, West Bank, Crimea, etc.). ZERO_RESULTS is the intended behaviour.
You can see the problem in google public issue tracker at https://issuetracker.google.com/issues/35826813
The bad news, your point is at the DMZ. As stated above: Google Reverse Geocoding won't work near the 1949 armistice lines (and not in what used to be Jordanian territories 1949-1967).
The not-so-bad news: There may be some kind of a workaround that's feasible for some: OpenStreetMap can also provide reverse geolocation. Your coordinate:
https://nominatim.openstreetmap.org/reverse?&format=jsonv2&lat=31.779975&lon=35.227901
Yields
{
"place_id": 199263301,
"licence": "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
"osm_type": "relation",
"osm_id": 7880713,
"lat": "31.7782568",
"lon": "35.2315827592487",
"place_rank": 30,
"category": "historic",
"type": "heritage",
"importance": 0,
"addresstype": "historic",
"name": "Old City of Jerusalem and its Walls",
"display_name": "Old City of Jerusalem and its Walls, HaOmer, Jewish Quarter, Old City, Jerusalem, Jerusalem District, no, Israel",
"address": {
"address29": "Old City of Jerusalem and its Walls",
"footway": "HaOmer",
"suburb": "Old City",
"city": "Jerusalem",
"state": "Jerusalem District",
"postcode": "no",
"country": "Israel",
"country_code": "il"
},
"boundingbox": [
"31.7728081",
"31.7837416",
"35.2257013",
"35.2375582"
]
}
As you can see, they may be off by quite a few hundreds of meters. In this case (I was mean, choosing a case that augments the issue) it spells 15min diversion by foot, and no way by car.
Some may find it tolerable. For others it means sending someone to the wrong place.
On the other hand, as mentioned before, I did pick a nasty case. In most cases OSM are pretty much accurate to a few meters.
I just want to add that on 1/29/2018 I also started experiencing random results from the API. My code has been working great for more for a year. Now I can send a request with a well-defined location (not reverse; I start with an address or intersection in search of coordinates and geopolitical info) and I get no results. I can send again- no results. And I can send again- and I GET RESULTS! Same exact query, with differing responses. I think the root cause for my

Obtaining data from my TP-Link router programmatically

I'm trying to design an app that can communicate with my router programmatically using the same endpoints as the web interface (there's a demo on TP-Link's website). My router is a TP-Link TD-W8980, if that matters.
The format appears to be very difficult to decipher. Here is a request which obtains the data for the status part of my app. This can obtain a valid response from the router but I'm not sure why!
I'm especially confused by the #0,0,0,0,0,0#0,0,0,0,0,0] part of the response. It's the only part I haven't managed to work out but I think I recall reading it's to do with the stack?!?
[SYS_MODE#0,0,0,0,0,0#0,0,0,0,0,0]0,1
mode
[LAN_HOST_CFG#1,0,0,0,0,0#0,0,0,0,0,0]1,1
DNSServers
[WAN_DSL_INTF_CFG#1,0,0,0,0,0#0,0,0,0,0,0]2,8
upstreamCurrRate
downstreamCurrRate
upstreamMaxRate
downstreamMaxRate
upstreamNoiseMargin
downstreamNoiseMargin
upstreamAttenuation
downstreamAttenuation
[IGD_DEV_INFO#0,0,0,0,0,0#0,0,0,0,0,0]3,3
softwareVersion
hardwareVersion
upTime
[LAN_IP_INTF#0,0,0,0,0,0#0,0,0,0,0,0]4,2
IPInterfaceIPAddress
X_TPLINK_MACAddress
[LAN_HOST_ENTRY#0,0,0,0,0,0#0,0,0,0,0,0]5,4
leaseTimeRemaining
MACAddress
hostName
IPAddress
[WAN_PPP_CONN#0,0,0,0,0,0#0,0,0,0,0,0]6,4
enable
connectionStatus
externalIPAddress
DNSServers
If it helps, the names in capitals (e.g. SYS_MODE) is the name of the section. The number after the ] is a counter stating the section number (sections can be in any order). The final number following the , is the number of parameters that follow in this section.
There are also request types for each section. In the example above, the URL is http://192.168.1.1/cgi?1&1&1&1&5&5&5. As you can see the two main request types are 1 and 5.
Here is an example response from the server. As you can see, some of the sections can be returned more than once, which makes the first number of the six zeros increment each time.
[0,0,0,0,0,0]0
mode=DSL
[1,0,0,0,0,0]1
DNSServers=x.x.x.x,x.x.x.x
[1,0,0,0,0,0]2
upstreamCurrRate=928
downstreamCurrRate=3072
upstreamMaxRate=1068
downstreamMaxRate=3104
upstreamNoiseMargin=60
downstreamNoiseMargin=57
upstreamAttenuation=295
downstreamAttenuation=546
[0,0,0,0,0,0]3
softwareVersion=0.6.0 1.3 v000e.0 Build 131012 Rel.51720n
hardwareVersion=TD-W8980 v1 00000000
upTime=x
[1,1,0,0,0,0]4
IPInterfaceIPAddress=192.168.1.1
X_TPLINK_MACAddress=xx:xx:xx:xx:xx:xx
[1,0,0,0,0,0]5
leaseTimeRemaining=-1
MACAddress=xx:xx:xx:xx:xx:xx
hostName=X
IPAddress=192.168.1.2
[2,0,0,0,0,0]5
leaseTimeRemaining=-1
MACAddress=xx:xx:xx:xx:xx:xx
hostName=X
IPAddress=192.168.1.4
[3,0,0,0,0,0]5
leaseTimeRemaining=-1
MACAddress=xx:xx:xx:xx:xx:xx
hostName=X
IPAddress=192.168.1.11
[4,0,0,0,0,0]5
leaseTimeRemaining=-1
MACAddress=xx:xx:xx:xx:xx:xx
hostName=X
IPAddress=192.168.1.5
[1,2,1,0,0,0]6
enable=1
connectionStatus=Connected
externalIPAddress=x.x.x.x
DNSServers=x.x.x.x,x.x.x.x
[2,1,1,0,0,0]6
enable=0
connectionStatus=Unconfigured
externalIPAddress=0.0.0.0
DNSServers=0.0.0.0,0.0.0.0
[3,1,1,0,0,0]6
enable=0
connectionStatus=Unconfigured
externalIPAddress=0.0.0.0
DNSServers=0.0.0.0,0.0.0.0
[error]0
I would appreciate any explanation of this format and if it appears anywhere else on the web. I've never seen such a system before!

Geographic subdivisions with Geocoder

I use Geocoder to get an address from a part of string like this:
Address[addressLines=[0:"36 Avenue des Champs-Élysées",1:"75008 Paris",2:"France"],feature=36,admin=null,sub-admin=null,locality=Paris,thoroughfare=Avenue des Champs-Élysées,postalCode=75008,countryCode=FR,countryName=France,hasLatitude=true,latitude=48.8700213,hasLongitude=true,longitude=2.3080506,phone=null,url=null,extras=null]
with this code (address is my part of string):
try {
Geocoder geocoder = new Geocoder(c, Locale.FRENCH);
addresss = geocoder.getFromLocationName(address, 3);
} catch (Exception e) {}
With this address I can have the city (Address.locality) the country (Address.country), but not areas between city and country.
I don't know how countries are subdivised everywhere in the world, but for example, in France, you have the 36000 cities like "Paris", in 102 departments ("Paris" is in department "Paris" (the same name)), themselves in 22 regions ("Paris" in region "Ile de France"). Equivalent of regions in USA has to be State or maybe a subdivision of State...
I don't see how to obtain these informations with Adress. Do you know how to do that?
Thank's by advance!
It seems the android app does not provided that. But you can get it with the web api. They return a much detailed type then the android app, also look at this Address Types and Address Component Types section

Android Extract url with specific domain name from String

I am developing a JSON application. I am able to download all of the data but I'm running into an interesting issue. I am trying to grab a string with the domain name:
http://www.prindlepost.org/
When grabbing all of the JSON, I get an extremely large string which I am unable to paste in there. The part I am trying to parse out is:
<p>The road through Belgrade was quiet at 4 A.M. Besides the occasional whir of another car speeding by, my taxi was largely alone on the road. Through the windshield I could see the last traces of apartment blocks pass by as we left the outskirts of the city. Somewhere beyond the limits of my vision, I knew the airport waited, its converging neon runway lines already lighting up the pre-dawn darkness.</p>
<div class="more-link-wrap wpb_button"> Read more</div>
where I am focusing on:
Read more</div>
I'm unfamiliar with extracting strings like this. In the end, I want to be able to save the URL as its own string. For example, the above would be converted into:
String url = "http://www.prindlepost.org/2015/06/this-is-a-self-portrait/";
One thing to note, there are A LOT of URLs to narrowing down by class name may help me a bunch.
My initial guess was:
// <READ MORE>
Pattern p = Pattern.compile("href=\"(.*?)\"");
Matcher m = p.matcher(content);
String urlTemp = null;
if (m.find()) {
urlTemp = m.group(1); // this variable should contain the link URL
}
Log.d("LINK WITHIN TEXT", ""+urlTemp);
// </READ MORE>
Any help is appreciated!
It may be work trying to use something like: http://jsoup.org/
If you check out their example for parsing out links:
String html = "<p>The road through Belgrade was quiet at 4 A.M. Besides the occasional whir of another car speeding by, my taxi was largely alone on the road. Through the windshield I could see the last traces of apartment blocks pass by as we left the outskirts of the city. Somewhere beyond the limits of my vision, I knew the airport waited, its converging neon runway lines already lighting up the pre-dawn darkness.</p>"
+ "<div class=\"more-link-wrap wpb_button\">"
+ "<a href=\"http://www.prindlepost.org/2015/06/this-is-a-self-portrait/\" class=\"more-link\">"
+ "Read more</a></div>";
Document doc = Jsoup.parse(html);
Element link = doc.select("a").first();
String relHref = link.attr("href"); // == "/2015/06/this-is-a-self-portrait/"
String absHref = link.attr("abs:href"); // "http://www.prindlepost.org/2015/06/this-is-a-self-portrait/"

Finding location using MCC, MNC, LAC, and Cell ID

I know what the values are for MCC, MNC, LAC, & Cell ID. I want to in C write a program to calculate the position in the form of latitude and longitude values in Linux.
FYI:
MCC - Mobile Country Code
MNC - Mobile Network Code
LAC - Location Area Code; a 16 bit number thereby allowing 65536 location areas within one GSM PLMN
more info is available here on Wikipedia, Location Area Identity
Question:
How can I convert MCC,MNC,LAC,Cell ID into latitude and longitude values in linux?
Why does Cell ID varies every time,when trying to read?
To answer your questions:
You can access public databases from terminal or a browser to convert cell ID to lat/lon. Databases include:
Unwired Labs API
OpenCellID
Cell ID is the ID of the cell phone tower your phone/device is connected to. The moment you move a bit, or the signal of another tower nearby is better than the current one, your phone will switch over to that tower, and your Cell ID now reflects the ID of that tower.
i wrote a python script that can do this for you. You can get a binary from the pyc file.
#!/bin/python
"""
Written by Atissonoun - Credits to MFC & HAC
***You need to initialize the script in order to fix the import and the dependency.
This is only a Beta version of the project***
This python file works as the engine for the project.
imports, coordinates, run......
"""
#Importing modules
import requests
#defining a Api_Keys
Google_API_KEY="Your google API Key goes here"
OpenCell_Api_Key ="Your OpenCellID API Key goes here"
def Google(MMC,MNC,LAC,ID,API_KEY=Google_API_KEY):
url = "https://www.googleapis.com/geolocation/v1/geolocate?key={}".format(API_KEY)
data={
"radioType": "gsm",
"cellTowers":[
{
"cellId": ID,
"locationAreaCode": LAC,
"mobileCountryCode": MMC,
"mobileNetworkCode": MNC
}
]
}
response = requests.post(url, json=data)
if response.status_code == 200 :
lat=response.json()[u'location'][u'lat']
long = response.json()[u'location'][u'lng']
d={'LAT':lat,'LONG':long}
print('Located Cell: {}'.format(ID))
return d
else:
print('Error: {}'.format(response.status_code))
return None
def Opencell(MMC,MNC,LAC,ID,API_KEY=OpenCell_Api_Key):
url = "https://us1.unwiredlabs.com/v2/process.php"
data = {
"token": API_KEY,
"radio": "gsm",
"mcc": MMC,
"mnc": MNC,
"cells": [{
"lac": LAC,
"cid": ID
}]
}
response = requests.post(url, json=data)
if response.status_code == 200:
if response.json()[u'status']== 'error':
print('Error: {}'.format(response.json()[u'message']))
return None
else:
lat = response.json()[u'lat']
long = response.json()[u'lon']
d = {'LAT': lat, 'LONG': long}
print('Located Cell: {}'.format(ID))
return d
else:
print('Error: {}'.format(response.status_code))
return None
You either need a database
OpenCellID (they provide APIs for new cell measurement, get the position of a specific cell, etc)
or
use the "secret" API:
"http://www.google.com/glm/mmap" is a non-public API to convert cellLocation to latitude and longitude.
Many ways to do that are given in the answwers for this SO question.
You can use this simple but efficient web site that doesn't need any log in:
http://www.cell2gps.com/
while you can access to operator info like MCC and MNC to the wiki page:
http://en.wikipedia.org/wiki/Mobile_country_code#I
The result is the location GPS through Google Maps,

Categories

Resources