In my application am setting a default value to latitude
and longitude
.
When i type the cityname in edittext
i need to move that point to that place where i need
问题:
回答1:
Use the below code to get the lat and long of city name
String location=cityName;
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;
}
String lat = result.substring(6, result.lastIndexOf(","));
String longi = result.substring(result.lastIndexOf(",") + 1);
}
catch(Exception e){
e.printStackTrace();
}
回答2:
My suggestion is to use Google GeoCoding REST API , not Geocoder.
Because I once used it in my country,China,I have a error below:
"java.io.IOException: Service not Available"
after search , It`s seems that
"The Geocoder class requires a backend service that is not included in the core android framework. The Geocoder query methods will return an empty list if there no backend service in the platform. Use the isPresent() method to determine whether a Geocoder implementation exists."
So I turn to Google GeoCoding REST API , It`s easy , just request like this:
http://maps.googleapis.com/maps/api/geocode/json?address=NewYork&sensor=false
You will get a json that has geo location in it. And You can also get Address from locaiton through it:
http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=false
If you can read Chinese , I have a blog about this,check this out: http://wangchao.de/android%E5%88%A9%E7%94%A8google-geocoding-api%E6%9B%BF%E4%BB%A3geocoder-%E8%A7%A3%E5%86%B3%E5%9C%B0%E5%9D%80%E8%AF%AD%E8%A8%80%E9%97%AE%E9%A2%98
回答3:
Use the Geocoder class.
Geocode gc = new Geocoder();
List<Address> ads = gc.getFromLocationName(cityName,maxResults);
From the docs :
A class for handling geocoding and reverse geocoding. Geocoding is the process of transforming a street address or other description of a location into a (latitude, longitude) coordinate. Reverse geocoding is the process of transforming a (latitude, longitude) coordinate into a (partial) address. The amount of detail in a reverse geocoded location description may vary, for example one might contain the full street address of the closest building, while another might contain only a city name and postal code. The Geocoder class requires a backend service that is not included in the core android framework. The Geocoder query methods will return an empty list if there no backend service in the platform. Use the isPresent() method to determine whether a Geocoder implementation exists.
回答4:
Try to get the GeoPoint of cityname
GeoPoint startGP = new GeoPoint(
(int) (Double.parseDouble(cityname.getText()) * 1E6));
Then u can able to get the latitude and longitude from GeoPoint by using below code.
Double.toString((double)startGP.getLatitudeE6() / 1E6),
Double.toString((double)dest.getLongitudeE6() / 1E6)