我正在使用LocationManager,Location和Geocoder尝试使用getFromLocationName()
方法查找商家的地址。
问题是当我打印返回的地址时,它离我的位置不远。我尝试打开GPS并关闭wifi以获得不同的结果,但一切都保持不变。请务必注意,我正在使用LocationManager返回最佳提供程序。
同样重要的是要注意,当我使用getFromLocation()
时,我确实得到了我的确切位置。这里的问题是我无法指定商家名称来获取其地址。
package restaurantcheckin.pkg;
import java.util.List;
import java.util.Locale;
import java.util.List;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;
import restaurantcheckin.pkg.RowItem0;
public class MainActivity extends Activity
{
Geocoder ugeo;
List<Address> adr;
LocationManager lm;
Location ulo;
final double range = 1.00;
String lName;
String stre;
String city;
String state;
String zip;
String adrOut;
String lat;
String lon;
String bp;
Criteria cri;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tView0 = (TextView) findViewById(R.id.tView);
this.context = this;
cri = new Criteria();
lName = "Pizza Hut";
lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
bp = lm.getBestProvider(cri,false);
Log.d("Debug",bp);
ulo = lm.getLastKnownLocation(bp);
if(ulo == null)
{
Toast.makeText(this, "Location not found...", Toast.LENGTH_LONG).show();
}
else
{
try
{
ugeo = new Geocoder(context);
adr = ugeo.getFromLocationName(lName, 1, ulo.getLatitude() - range, ulo.getLongitude(), ulo.getLatitude(), ulo.getLongitude() - range);
Log.d("Debug","Latit" + ulo.getLatitude() + "Long" + ulo.getLongitude());
stre = adr.get(0).getThoroughfare();
Log.d("Debug","stre");
city = adr.get(0).getLocality();
Log.d("Debug","city");
state = adr.get(0).getAdminArea();
Log.d("Debug","state");
zip = adr.get(0).getPostalCode();
Log.d("Debug","zip");
adrOut = stre + "\n" + city + ", " + state + " " + zip;
Log.d("Debug","adrOut");
}
catch(Exception e)
{
Log.d("Exception","Line 68");
}
}
tView0.setText(adrOut);
Log.d("Debug","setText");
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
如果有人可以提供帮助,请提出建议。谢谢。