我试图在谷歌地图活动中搜索位置名称。在onsearch方法address.getLatitude()和address.getLongitude不在work.its显示红色文本。任何人请解决我的错误。在我的代码下面
public void onSearch(View view)
{
EditText location_tf = (EditText)findViewById(R.id.TFaddress);
String location = location_tf.getText().toString();
List<Address> addressList = null;
if(location != null || !location.equals(""))
{
Geocoder geocoder = new Geocoder(this);
try {
addressList = geocoder.getFromLocationName(location , 1);
} catch (IOException e) {
e.printStackTrace();
}
Address address = addressList.get(0);
LatLng latLng = new LatLng(address.getLatitude() , address.getLongitude());
mMap.addMarker(new MarkerOptions().position(latLng).title("Marker"));
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(0,0);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
答案 0 :(得分:0)
似乎您导入了错误的Address类或根本没有导入。可以使用以下行导入The class which you are looking for:
import android.location.Address;
上述类包含getLatitude()
和getLongitude()
方法。另一个包含Address
类的包,我猜错误地导入了以下内容: