Android的地理codeR getFromLocationName总是返回null [英] Android Geocoder getFromLocationName always returns null
问题描述
我一直在努力地缘codeA字符串来获得它的坐标,但是我的程序总是崩溃,因为当过我尝试使用 getFromLocationName()
返回空
。我一直在试图解决这一问题了几个小时,但没有什么工作。这是我的code
公共类MainActivity延伸活动{
私人GoogleMap的MMAP;
名单<地址>地址;
MarkerOptions迈阿密;
字符串myLocation =迈阿密,佛罗里达;
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
如果(MMAP == NULL){
MMAP =((MapFragment)getFragmentManager()。findFragmentById(
R.id.map))的GetMap()。
}
如果(MMAP!= NULL){
地理codeR地理codeR =新的地缘codeR(本);
双纬度= 0;
双经度= 0;
而(地址== NULL){
尝试 {
地址=地理coder.getFromLocationName(myLocation,1);
}赶上(IOException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}
}
地址地址= addresses.get(0);
如果(addresses.size()大于0){
纬度= address.getLatitude();
经度= address.getLongitude();
}
经纬度城=新的经纬度(纬度,经度);
迈阿密=新MarkerOptions()位置(市).title伪(迈阿密)。
mMap.addMarker(迈阿密);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(市,15));
}
}
地理codeR并不总是返回值。你可以尝试在发送请求3次循环。我应该能够ATLEAST返回一次。如果没有的话,他们可能是连接问题,也可以是其他的问题,如服务器DIS不会回复你的要求。试试,看看这些线程:
地理codeR并不总是返回一个值和<一href="http://stackoverflow.com/questions/4567216/geo$c$cr-getfromlocationname-returns-only-null">geo$c$cr.getFromLocationName只返回空
更新:
我有一个while循环,很好,但我曾经尝试它最大为10倍。有时,它再也没有回来,即使它是连接吨互联网任何东西。然后,我用这个的更可靠的方式来获得地址每次:
公开的JSONObject getLocationInfo(){
HTTPGET HTTPGET =新HTTPGET(http://maps.google.com/maps/api/geo$c$c/json?latlng=+纬度+,+经度+与&amp;传感器=真);
HttpClient的客户端=新DefaultHttpClient();
HTT presponse响应;
StringBuilder的StringBuilder的=新的StringBuilder();
尝试 {
响应= client.execute(HTTPGET);
HttpEntity实体= response.getEntity();
InputStream的流= entity.getContent();
INT B:
而((二= stream.read())!= - 1){
stringBuilder.append((char)的B);
}
}赶上(ClientProtocolException E){
}赶上(IOException异常E){
}
的JSONObject的JSONObject =新的JSONObject();
尝试 {
的JSONObject =新的JSONObject(stringBuilder.toString());
}赶上(JSONException E){
e.printStackTrace();
}
返回的JSONObject;
}
我把它叫做如下:
的JSONObject RET = getLocationInfo();
JSONObject的位置;
字符串location_string;
尝试 {
位置= ret.getJSONArray(结果)getJSONObject(0)。
location_string = location.getString(formatted_address);
Log.d(测试,formattted地址:+ location_string);
}赶上(JSONException E1){
e1.printStackTrace();
}
希望这有助于。我也累了依托地缘$ C $铬。这为我工作。 如果您要更换的纬度和经度坐标的网址,看到返回的JSON对象在Web浏览器。你会看到刚刚发生了什么。
I have been trying to geocode a string to get its coordinates but my program always crashes because when ever I try to use getFromLocationName()
it returns null
. I have been trying to fix this for hours but nothing is working. Here is my code
public class MainActivity extends Activity {
private GoogleMap mMap;
List<Address> addresses;
MarkerOptions miami;
String myLocation = "Miami,Florida";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (mMap == null) {
mMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
}
if (mMap != null) {
Geocoder geocoder = new Geocoder(this);
double latitude = 0;
double longitude = 0;
while(addresses==null){
try {
addresses = geocoder.getFromLocationName(myLocation, 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Address address = addresses.get(0);
if (addresses.size() > 0) {
latitude = address.getLatitude();
longitude = address.getLongitude();
}
LatLng City = new LatLng(latitude, longitude);
miami = new MarkerOptions().position(City).title("Miami");
mMap.addMarker(miami);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(City, 15));
}
}
Geocoder doesn't always return a value. You can try to send a request 3 times in a for loop. I should be able to return atleast once. If not then, their might be a connection issue or can be other issues like server dis not reply to your request. Try and see these threads:
Geocoder doesn't always return a value and geocoder.getFromLocationName returns only null
Updated:
I had a while loop as well but I used to try it maximum for 10 times. Sometimes, it never returned anything even if it was connected t internet. Then, I used this much more reliable way to get the address everytime:
public JSONObject getLocationInfo() {
HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?latlng="+lat+","+lng+"&sensor=true");
HttpClient client = new DefaultHttpClient();
HttpResponse response;
StringBuilder stringBuilder = new StringBuilder();
try {
response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int b;
while ((b = stream.read()) != -1) {
stringBuilder.append((char) b);
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
JSONObject jsonObject = new JSONObject();
try {
jsonObject = new JSONObject(stringBuilder.toString());
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObject;
}
I called it as follows:
JSONObject ret = getLocationInfo();
JSONObject location;
String location_string;
try {
location = ret.getJSONArray("results").getJSONObject(0);
location_string = location.getString("formatted_address");
Log.d("test", "formattted address:" + location_string);
} catch (JSONException e1) {
e1.printStackTrace();
}
Hope this helps. I was also tired of relying on geocoder. This worked for me. If you replace the URL with the lat and longitude coordinates and see the returned JSON object in a web browser. You'll see what just happened.
这篇关于Android的地理codeR getFromLocationName总是返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!