package com.hl;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.maps.MapActivity;
public class Geo extends MapActivity {
private EditText input;
private Button btn;
private TextView show;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
input = (EditText)findViewById(R.id.input);
btn = (Button)findViewById(R.id.btn);
show = (TextView)findViewById(R.id.show);
btn.setOnClickListener(new OnClickListener() {
public void onClick(final View v) {
show.setText(performGeocode(input.getText().toString(), true));
}
});
}
private String performGeocode(final String in, final boolean isAddr) {
String result= null;
if (this.input != null) {
Geocoder geocoder = new Geocoder(this,Locale.CHINA);
if (isAddr) {
try {
List
addresses = geocoder.getFromLocationName(in,5);
if (addresses != null) {
result = addresses.get(0).toString();
}
} catch (IOException e) {
Toast.makeText(Geo.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
} else {
try {
String[] coords = in.split(",");
if ((coords != null) && (coords.length == 2)) {
List addresses = geocoder.getFromLocation(
Double.parseDouble(coords[0]),
Double.parseDouble(coords[1]), 1);
result = addresses.get(0).toString();
}
} catch (IOException e) {
Toast.makeText(Geo.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
return result;
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
}
注意不要在android 8的模拟器上做测试,不然不能成功的!!