how to find continent in particular location using Geocoder in android app
Asked Answered
P

2

7
   i am developing android application using Geocoder services,
   I have an application where I try to get address of a location based on 
   the  latitude,longitude coordinates its working fine.  
   my problem is how to get continent of particular address.

example : double lat=17; double lon=78.49;

List addresses = new Geocoder(Shout.this,Locale.getDefault()).getFromLocation(lat, lon, 1);

i am using this code output is India,Hyderabad this address related how to find continent ,please help me some valuable solution.

i am getting country name dynamically using geocoder is their any chance to get in continent name along with country.it is difficult to maintain statically i need dynamically any free services find continent based services

Thanks In Advance

Puffery answered 18/12, 2010 at 6:2 Comment(0)
N
2

I don't think there is such information given by Google's API. The only solution I can think of is to have some kind of static data structure mapping country names to continent names (which you would presumably have to find yourself somehow, for instance by scraping this web page... ). Then you could text search the string that GeoCoder gives you and return the continent corresponding to the closest matching country name (which would be the key in your String mapping). (You would also have to treat the special case of US addresses which, annoyingly, end just with the state code)

Neonatal answered 18/12, 2010 at 9:48 Comment(6)
You would have to make sure that GeoCoder in your code was asked to return the address in only the language that your HashMap was set up, otherwise you will have problems... You may still have to internationalise Continent names unless you are only catering for users of one language.Neonatal
already i am getting country name dynamically using geocoder is their any chance to get in continent name along with country.it is difficult to maintain statically i need dynamicallyPuffery
Yes I understand, I was trying to suggest a way to take the country name that you were receiving and use this nasty Map idea to turn it into a continent name. If there is no built-in way of doing this then I can't think of any better way right now, unfortunately.Neonatal
any third party free services get continent name along with countryPuffery
Well here is an official Google Answer. They suggest something similar. mail-archive.com/[email protected]/…Neonatal
@downvoter: why was this down-voted? There is nothing directly wrong with this question, so you should at least give a reason for down-voting! +1 to compensate.Cooperman
H
3

As we have limited number of Continents, it's good to use static list of data.

We can use below json_str (found here) which have CountryCode & Continent

String json_str = "{\"AD\":\"Europe\",\"AE\":\"Asia\",\"AF\":\"Asia\",\"AG\":\"North America\",\"AI\":\"North America\",\"AL\":\"Europe\",\"AM\":\"Asia\",\"AN\":\"North America\",\"AO\":\"Africa\",\"AQ\":\"Antarctica\",\"AR\":\"South America\",\"AS\":\"Australia\",\"AT\":\"Europe\",\"AU\":\"Australia\",\"AW\":\"North America\",\"AZ\":\"Asia\",\"BA\":\"Europe\",\"BB\":\"North America\",\"BD\":\"Asia\",\"BE\":\"Europe\",\"BF\":\"Africa\",\"BG\":\"Europe\",\"BH\":\"Asia\",\"BI\":\"Africa\",\"BJ\":\"Africa\",\"BM\":\"North America\",\"BN\":\"Asia\",\"BO\":\"South America\",\"BR\":\"South America\",\"BS\":\"North America\",\"BT\":\"Asia\",\"BW\":\"Africa\",\"BY\":\"Europe\",\"BZ\":\"North America\",\"CA\":\"North America\",\"CC\":\"Asia\",\"CD\":\"Africa\",\"CF\":\"Africa\",\"CG\":\"Africa\",\"CH\":\"Europe\",\"CI\":\"Africa\",\"CK\":\"Australia\",\"CL\":\"South America\",\"CM\":\"Africa\",\"CN\":\"Asia\",\"CO\":\"South America\",\"CR\":\"North America\",\"CU\":\"North America\",\"CV\":\"Africa\",\"CX\":\"Asia\",\"CY\":\"Asia\",\"CZ\":\"Europe\",\"DE\":\"Europe\",\"DJ\":\"Africa\",\"DK\":\"Europe\",\"DM\":\"North America\",\"DO\":\"North America\",\"DZ\":\"Africa\",\"EC\":\"South America\",\"EE\":\"Europe\",\"EG\":\"Africa\",\"EH\":\"Africa\",\"ER\":\"Africa\",\"ES\":\"Europe\",\"ET\":\"Africa\",\"FI\":\"Europe\",\"FJ\":\"Australia\",\"FK\":\"South America\",\"FM\":\"Australia\",\"FO\":\"Europe\",\"FR\":\"Europe\",\"GA\":\"Africa\",\"GB\":\"Europe\",\"GD\":\"North America\",\"GE\":\"Asia\",\"GF\":\"South America\",\"GG\":\"Europe\",\"GH\":\"Africa\",\"GI\":\"Europe\",\"GL\":\"North America\",\"GM\":\"Africa\",\"GN\":\"Africa\",\"GP\":\"North America\",\"GQ\":\"Africa\",\"GR\":\"Europe\",\"GS\":\"Antarctica\",\"GT\":\"North America\",\"GU\":\"Australia\",\"GW\":\"Africa\",\"GY\":\"South America\",\"HK\":\"Asia\",\"HN\":\"North America\",\"HR\":\"Europe\",\"HT\":\"North America\",\"HU\":\"Europe\",\"ID\":\"Asia\",\"IE\":\"Europe\",\"IL\":\"Asia\",\"IM\":\"Europe\",\"IN\":\"Asia\",\"IO\":\"Asia\",\"IQ\":\"Asia\",\"IR\":\"Asia\",\"IS\":\"Europe\",\"IT\":\"Europe\",\"JE\":\"Europe\",\"JM\":\"North America\",\"JO\":\"Asia\",\"JP\":\"Asia\",\"KE\":\"Africa\",\"KG\":\"Asia\",\"KH\":\"Asia\",\"KI\":\"Australia\",\"KM\":\"Africa\",\"KN\":\"North America\",\"KP\":\"Asia\",\"KR\":\"Asia\",\"KW\":\"Asia\",\"KY\":\"North America\",\"KZ\":\"Asia\",\"LA\":\"Asia\",\"LB\":\"Asia\",\"LC\":\"North America\",\"LI\":\"Europe\",\"LK\":\"Asia\",\"LR\":\"Africa\",\"LS\":\"Africa\",\"LT\":\"Europe\",\"LU\":\"Europe\",\"LV\":\"Europe\",\"LY\":\"Africa\",\"MA\":\"Africa\",\"MC\":\"Europe\",\"MD\":\"Europe\",\"ME\":\"Europe\",\"MG\":\"Africa\",\"MH\":\"Australia\",\"MK\":\"Europe\",\"ML\":\"Africa\",\"MM\":\"Asia\",\"MN\":\"Asia\",\"MO\":\"Asia\",\"MP\":\"Australia\",\"MQ\":\"North America\",\"MR\":\"Africa\",\"MS\":\"North America\",\"MT\":\"Europe\",\"MU\":\"Africa\",\"MV\":\"Asia\",\"MW\":\"Africa\",\"MX\":\"North America\",\"MY\":\"Asia\",\"MZ\":\"Africa\",\"NA\":\"Africa\",\"NC\":\"Australia\",\"NE\":\"Africa\",\"NF\":\"Australia\",\"NG\":\"Africa\",\"NI\":\"North America\",\"NL\":\"Europe\",\"NO\":\"Europe\",\"NP\":\"Asia\",\"NR\":\"Australia\",\"NU\":\"Australia\",\"NZ\":\"Australia\",\"OM\":\"Asia\",\"PA\":\"North America\",\"PE\":\"South America\",\"PF\":\"Australia\",\"PG\":\"Australia\",\"PH\":\"Asia\",\"PK\":\"Asia\",\"PL\":\"Europe\",\"PM\":\"North America\",\"PN\":\"Australia\",\"PR\":\"North America\",\"PS\":\"Asia\",\"PT\":\"Europe\",\"PW\":\"Australia\",\"PY\":\"South America\",\"QA\":\"Asia\",\"RE\":\"Africa\",\"RO\":\"Europe\",\"RS\":\"Europe\",\"RU\":\"Europe\",\"RW\":\"Africa\",\"SA\":\"Asia\",\"SB\":\"Australia\",\"SC\":\"Africa\",\"SD\":\"Africa\",\"SE\":\"Europe\",\"SG\":\"Asia\",\"SH\":\"Africa\",\"SI\":\"Europe\",\"SJ\":\"Europe\",\"SK\":\"Europe\",\"SL\":\"Africa\",\"SM\":\"Europe\",\"SN\":\"Africa\",\"SO\":\"Africa\",\"SR\":\"South America\",\"ST\":\"Africa\",\"SV\":\"North America\",\"SY\":\"Asia\",\"SZ\":\"Africa\",\"TC\":\"North America\",\"TD\":\"Africa\",\"TF\":\"Antarctica\",\"TG\":\"Africa\",\"TH\":\"Asia\",\"TJ\":\"Asia\",\"TK\":\"Australia\",\"TM\":\"Asia\",\"TN\":\"Africa\",\"TO\":\"Australia\",\"TR\":\"Asia\",\"TT\":\"North America\",\"TV\":\"Australia\",\"TW\":\"Asia\",\"TZ\":\"Africa\",\"UA\":\"Europe\",\"UG\":\"Africa\",\"US\":\"North America\",\"UY\":\"South America\",\"UZ\":\"Asia\",\"VC\":\"North America\",\"VE\":\"South America\",\"VG\":\"North America\",\"VI\":\"North America\",\"VN\":\"Asia\",\"VU\":\"Australia\",\"WF\":\"Australia\",\"WS\":\"Australia\",\"YE\":\"Asia\",\"YT\":\"Africa\",\"ZA\":\"Africa\",\"ZM\":\"Africa\",\"ZW\":\"Africa\"}";

And From Geocoder() we can get CountryCode like below:

try {

    JSONObject jsonObject = new JSONObject(json_str);


    Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);

    List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);

    if (addresses.size() > 0) {

        Address fetchedAddress = addresses.get(0);

        // getCountryCode from Address
        String countryCode = fetchedAddress.getCountryCode();

        // get continentName here
        String continentName = jsonObject.getString(countryCode);

    }

} catch (IOException e) {
    e.printStackTrace();
} catch (JSONException e) {
    e.printStackTrace();
}
Haworth answered 2/1, 2018 at 11:31 Comment(0)
N
2

I don't think there is such information given by Google's API. The only solution I can think of is to have some kind of static data structure mapping country names to continent names (which you would presumably have to find yourself somehow, for instance by scraping this web page... ). Then you could text search the string that GeoCoder gives you and return the continent corresponding to the closest matching country name (which would be the key in your String mapping). (You would also have to treat the special case of US addresses which, annoyingly, end just with the state code)

Neonatal answered 18/12, 2010 at 9:48 Comment(6)
You would have to make sure that GeoCoder in your code was asked to return the address in only the language that your HashMap was set up, otherwise you will have problems... You may still have to internationalise Continent names unless you are only catering for users of one language.Neonatal
already i am getting country name dynamically using geocoder is their any chance to get in continent name along with country.it is difficult to maintain statically i need dynamicallyPuffery
Yes I understand, I was trying to suggest a way to take the country name that you were receiving and use this nasty Map idea to turn it into a continent name. If there is no built-in way of doing this then I can't think of any better way right now, unfortunately.Neonatal
any third party free services get continent name along with countryPuffery
Well here is an official Google Answer. They suggest something similar. mail-archive.com/[email protected]/…Neonatal
@downvoter: why was this down-voted? There is nothing directly wrong with this question, so you should at least give a reason for down-voting! +1 to compensate.Cooperman

© 2022 - 2024 — McMap. All rights reserved.