我正在尝试将地址转换为long和lat,但它返回0.请告诉我我做错了什么。这是我的代码:
geocoder = new Geocoder(this);
buttonSave.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
EditText loc = (EditText)findViewById(R.id.txtAddress);
String locationName = loc.getText().toString();
List<Address> addressList = geocoder.getFromLocationName(locationName, 2);
if(addressList!=null && addressList.size()>0) {
lat = (int)(addressList.get(0).getLatitude()*1000000);
lng = (int)(addressList.get(0).getLongitude()*1000000);
}
} catch (IOException e) {
e.printStackTrace();
}
lattitude = Integer.toString(lat);
longertude = Integer.toString(lng);
我的LOGCAT:
07-03 13:44:28.799: I/System.out(274): [INFO:19529614]: LogSource: Running flush
07-03 13:44:28.799: I/System.out(274): [INFO:19529623]: LogSource: Sending payload [bytes=267]
07-03 13:44:32.262: D/SntpClient(83): request time failed: java.net.SocketException: Address family not supported by protocol
07-03 13:44:33.809: I/System.out(274): [INFO:19534623]: LogSource: Running delayed flush
07-03 13:44:33.809: W/System.err(2517): java.io.IOException: Unable to parse response from server
07-03 13:44:33.823: I/System.out(274): [INFO:19534640]: LogSource: Running flush
07-03 13:44:33.829: I/System.out(274): [INFO:19534651]: LogSource: Sending payload [bytes=267]
07-03 13:44:33.869: W/System.err(2517): at android.location.Geocoder.getFromLocationName(Geocoder.java:178)
07-03 13:44:33.869: W/System.err(2517): at de.vogella.android.locationapi.simple.InsertStock$1.onClick(InsertStock.java:108)
07-03 13:44:33.879: W/System.err(2517): at android.view.View.performClick(View.java:3110)
07-03 13:44:33.891: W/System.err(2517): at android.view.View$PerformClick.run(View.java:11928)
07-03 13:44:33.899: W/System.err(2517): at android.os.Handler.handleCallback(Handler.java:587)
07-03 13:44:33.899: W/System.err(2517): at android.os.Handler.dispatchMessage(Handler.java:92)
07-03 13:44:33.899: W/System.err(2517): at android.os.Looper.loop(Looper.java:132)
07-03 13:44:33.913: W/System.err(2517): at android.app.ActivityThread.main(ActivityThread.java:4025)
07-03 13:44:33.919: W/System.err(2517): at java.lang.reflect.Method.invokeNative(Native Method)
07-03 13:44:33.939: W/System.err(2517): at java.lang.reflect.Method.invoke(Method.java:491)
07-03 13:44:33.939: W/System.err(2517): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
07-03 13:44:33.939: W/System.err(2517): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
07-03 13:44:33.939: W/System.err(2517): at dalvik.system.NativeStart.main(Native Method)
但没有错误(红字)
答案 0 :(得分:1)
我刚刚将locationName号码更改为2
try
{
address = coder.getFromLocationName(searchedAddress,**2**);
if (address == null) {
Log.d(TAG, "############Address not correct #########");
}
答案 1 :(得分:0)
您可以使用它从地址获取Lat,Long:
public String getLatitudeAndLongitudeFromGoogleMapForAddress(String searchedAddress){
Geocoder coder = new Geocoder(this);
List<Address> address;
try
{
address = coder.getFromLocationName(searchedAddress,5);
if (address == null) {
Log.d(TAG, "############Address not correct #########");
}
Address location = address.get(0);
Log.d(TAG, "Address Latitude : "+ location.getLatitude() + "Address Longitude : "+ location.getLongitude());
for(int i = 0; i<address.size();i++)
{
Address location2 = address.get(i);
Log.d(TAG, "Address : "+ location2.getAddressLine(0));
}
return "lat="+ location.getLatitude() +"&long="+ location.getLongitude();
}
catch(Exception e)
{
Log.d(TAG, "MY_ERROR : ############Address Not Found");
return "ERROR";
}
}
如果你在Android模拟器中绑定它,那么它将无法工作: 这是一个bug in the emulator for 2.2