Masz współrzędne kraju?
Jak uzyskać nazwę kraju w Androidzie, jeśli są znane współrzędne geograficzne? Jak zrobić to w najprostszy sposób?
15
Author: joao2fast4u, 2012-06-18
4 answers
Użyj do tego funkcji poniżej.
public void getAddress(double lat, double lng) {
Geocoder geocoder = new Geocoder(HomeActivity.mContext, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
Address obj = addresses.get(0);
String add = obj.getAddressLine(0);
GUIStatics.currentAddress = obj.getSubAdminArea() + ","
+ obj.getAdminArea();
GUIStatics.latitude = obj.getLatitude();
GUIStatics.longitude = obj.getLongitude();
GUIStatics.currentCity= obj.getSubAdminArea();
GUIStatics.currentState= obj.getAdminArea();
add = add + "\n" + obj.getCountryName();
add = add + "\n" + obj.getCountryCode();
add = add + "\n" + obj.getAdminArea();
add = add + "\n" + obj.getPostalCode();
add = add + "\n" + obj.getSubAdminArea();
add = add + "\n" + obj.getLocality();
add = add + "\n" + obj.getSubThoroughfare();
Log.v("IGA", "Address" + add);
// Toast.makeText(this, "Address=>" + add,
// Toast.LENGTH_SHORT).show();
// TennisAppActivity.showDialog(add);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
Dodaj poniżej uprawnienia do manifestu
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
14
Author: Dipak Keshariya,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-06-18 12:25:23
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-06-18 12:25:23
Here you go
Geocoder gcd = new Geocoder(context, Locale.getDefault());
List<Address> addresses = gcd.getFromLocation(lat, lng, 1);
if (addresses.size() > 0)
{
String countryName=addresses.get(0).getCountryName();
}
25
Author: Vipul Shah,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-06-18 12:20:32
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-06-18 12:20:32
Użyj tego
try {
Geocoder geo = new Geocoder(this.getApplicationContext(), Locale.getDefault());
List<Address> addresses = geo.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
if (addresses.isEmpty()) {
placeName.setText("Waiting for Location");
}
else {
if (addresses.size() > 0) {
placeName.setText(addresses.get(0).getFeatureName() + ", " + addresses.get(0).getLocality() +", " + addresses.get(0).getAdminArea() + ", " + addresses.get(0).getCountryName());
}
}
}
catch(Exception e){
Toast.makeText(this, "No Location Name Found", 600).show();
}
Użyj tego w pliku manifestu
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
5
Author: Cool Compiler,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-06-18 12:28:06
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-06-18 12:28:06
Ostatnio zrobiłem to, aby odczytać dane z adresu URL API sieci Web i przeanalizować JSON.
Przykładowy adres URL punktu (40.714224, -73.961452) to:
http://maps.google.com/maps/geo?q=40.714224,-73.961452&output=json&oe=utf8&sensor=true_or_false&key=your_api_key
, która daje następujące Wyjście:
{
"name": "40.714224,-73.961452",
"Status": {
"code": 200,
"request": "geocode"
},
"Placemark": [ {
"id": "p1",
"address": "285 Bedford Ave, Brooklyn, NY 11211, USA",
"AddressDetails": {
"Accuracy" : 8,
"Country" : {
"AdministrativeArea" : {
"AdministrativeAreaName" : "NY",
"SubAdministrativeArea" : {
"Locality" : {
"DependentLocality" : {
"DependentLocalityName" : "Williamsburg",
"PostalCode" : {
"PostalCodeNumber" : "11211"
},
"Thoroughfare" : {
"ThoroughfareName" : "285 Bedford Ave"
}
},
"LocalityName" : "Brooklyn"
},
"SubAdministrativeAreaName" : "Kings"
}
},
"CountryName" : "USA",
"CountryNameCode" : "US"
}
},
"ExtendedData": {
"LatLonBox": {
"north": 40.7154779,
"south": 40.7127799,
"east": -73.9600584,
"west": -73.9627564
}
},
"Point": {
"coordinates": [ -73.9614074, 40.7141289, 0 ]
}
} ]
}
Uważam, że GSON jest całkiem dobry do parsowania JSON w Androidzie.
2
Author: Richard,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-06-18 12:29:08
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-06-18 12:29:08