本文介绍了如何让经度,纬度的城市名称的Android code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要转换包含城市名称的文本字段取一文,我想将其转换为经度和纬度。
I want to convert a text taken from a text field which contains the city name, and I want to convert it to longitude and latitude.
这是我做了什么:
String location=city.getText().toString();
String inputLine = "";
String result = "";
location=location.replaceAll(" ", "%20");
String myUrl="http://maps.google.com/maps/geo?q="+location+"&output=csv";
try{
URL url=new URL(myUrl);
URLConnection urlConnection=url.openConnection();
BufferedReader in = new BufferedReader(new
InputStreamReader(urlConnection.getInputStream()));
while ((inputLine = in.readLine()) != null) {
result=inputLine;
}
lat = result.substring(6, result.lastIndexOf(","));
longi = result.substring(result.lastIndexOf(",") + 1);
}
catch(Exception e){
e.printStackTrace();
}
//////////////////////////////////
if (location=="" )
{
latitude=loc.getLatitude();
longitude=loc.getLongitude();
}
else
{
latitude=Double.parseDouble(lat);
longitude=Double.parseDouble(longi);
}
但code不走else语句
but the code doesn't take the else statement
我改变了网址是:
字符串myUrl =http://maps.googleapis.com/maps/api/geo$c$c/json?address=+位置+&放大器,传感器=真;
String myUrl="http://maps.googleapis.com/maps/api/geocode/json?address="+location+"&sensor=true";
和是这种结果:
{
"results" : [
{
"address_components" : [
{
"long_name" : "Nablus",
"short_name" : "Nablus",
"types" : [ "locality", "political" ]
}
],
"formatted_address" : "Nablus",
"geometry" : {
"location" : {
"lat" : 32.22504,
"lng" : 35.260971
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 32.2439165,
"lng" : 35.2929858
},
"southwest" : {
"lat" : 32.20615960000001,
"lng" : 35.2289562
}
}
},
"types" : [ "locality", "political" ]
}
],
"status" : "OK"
}
我怎么能使用经度和纬度在我的code ??
how could I use latitude and longitude in my code??
推荐答案
目前使用的。它确实pretty的大致相同的地理编码的API。
There is an easier way by using the Geocoder. It does pretty much the same as the Geocoding API.
if(Geocoder.isPresent()){
try {
String location = "theNameOfTheLocation";
Geocoder gc = new Geocoder(this);
List<Address> addresses= gc.getFromLocationName(location, 5); // get the found Address Objects
List<LatLng> ll = new ArrayList<LatLng>(addresses.size()); // A list to save the coordinates if they are available
for(Address a : addresses){
if(a.hasLatitude() && a.hasLongitude()){
ll.add(new LatLng(a.getLatitude(), a.getLongitude()));
}
}
} catch (IOException e) {
// handle the exception
}
}
这篇关于如何让经度,纬度的城市名称的Android code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!