Android 在地图视图上显示地址
我有两节课 Contactdetailfragment从联系人列表中获取地址 ContactDetailFragment.javaAndroid 在地图视图上显示地址,android,android-mapview,Android,Android Mapview,我有两节课 Contactdetailfragment从联系人列表中获取地址 ContactDetailFragment.java public class ContactDetailFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> { . . . @Override public void onLoadFinished(Loader
public class ContactDetailFragment extends Fragment implements
LoaderManager.LoaderCallbacks<Cursor> {
.
.
.
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
// If this fragment was cleared while the query was running
// eg. from from a call like setContact(uri) then don't do
// anything.
if (mContactUri == null) {
return;
}
switch (loader.getId()) {
case ContactDetailQuery.QUERY_ID:
// Moves to the first row in the Cursor
if (data.moveToFirst()) {
// For the contact details query, fetches the contact display name.
// ContactDetailQuery.DISPLAY_NAME maps to the appropriate display
// name field based on OS version.
final String contactName = data.getString(ContactDetailQuery.DISPLAY_NAME);
if (mIsTwoPaneLayout && mContactName != null) {
// In the two pane layout, there is a dedicated TextView
// that holds the contact name.
mContactName.setText(contactName);
} else {
// In the single pane layout, sets the activity title
// to the contact name. On HC+ this will be set as
// the ActionBar title text.
getActivity().setTitle(contactName);
}
}
break;
case ContactAddressQuery.QUERY_ID:
// This query loads the contact address details. More than
// one contact address is possible, so move each one to a
// LinearLayout in a Scrollview so multiple addresses can
// be scrolled by the user.
// Each LinearLayout has the same LayoutParams so this can
// be created once and used for each address.
final LinearLayout.LayoutParams layoutParams =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
// Clears out the details layout first in case the details
// layout has addresses from a previous data load still
// added as children.
mDetailsLayout.removeAllViews();
// Loops through all the rows in the Cursor
if (data.moveToFirst()) {
do {
// Builds the address layout
final LinearLayout layout = buildAddressLayout(
data.getInt(ContactAddressQuery.TYPE),
data.getString(ContactAddressQuery.LABEL),
data.getString(ContactAddressQuery.ADDRESS));
// Adds the new address layout to the details layout
mDetailsLayout.addView(layout, layoutParams);
} while (data.moveToNext());
} else {
// If nothing found, adds an empty address layout
mDetailsLayout.addView(buildEmptyAddressLayout(), layoutParams);
}
break;
}
}
公共类ContactDetailFragment扩展了片段实现
LoaderManager.LoaderCallbacks{
.
.
.
@凌驾
public void onLoadFinished(加载器、光标数据){
//如果在查询运行时清除了此片段
//例如,从一个像setContact(uri)这样的调用开始,然后不要这样做
//什么都行。
if(mContactUri==null){
回来
}
开关(loader.getId()){
案例ContactDetailQuery.QUERY\u ID:
//移动到光标中的第一行
if(data.moveToFirst()){
//对于联系人详细信息查询,获取联系人显示名称。
//ContactDetailQuery.DISPLAY\u名称映射到相应的显示器
//基于操作系统版本的名称字段。
最终字符串contactName=data.getString(ContactDetailQuery.DISPLAY\u NAME);
if(mIsTwoPaneLayout&&mContactName!=null){
//在双窗格布局中,有一个专用的文本视图
//保存联系人姓名的。
mContactName.setText(contactName);
}否则{
//在单窗格布局中,设置活动标题
//联系人姓名。在HC+上,这将设置为
//操作栏标题文本。
getActivity().setTitle(contactName);
}
}
打破
案例ContactAddressQuery.QUERY\u ID:
//此查询加载联系人地址详细信息。超过
//一个联系地址是可能的,所以将每个地址移动到
//滚动视图中的线性布局,以便可以显示多个地址
//由用户滚动。
//每个LinearLayout都具有相同的LayoutParams,因此可以
//创建一次并用于每个地址。
最终LinearLayout.LayoutParams LayoutParams=
新建LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_父对象,
ViewGroup.LayoutParams.WRAP_内容);
//在显示详细信息时,首先清除详细信息布局
//布局仍然具有来自上一次数据加载的地址
//作为儿童添加。
mDetailsLayout.removeallview();
//循环遍历光标中的所有行
if(data.moveToFirst()){
做{
//构建地址布局
最终线性布局布局=buildAddressLayout(
data.getInt(ContactAddressQuery.TYPE),
data.getString(ContactAddressQuery.LABEL),
getString(ContactAddressQuery.ADDRESS));
//将新地址布局添加到详细信息布局
mDetailsLayout.addView(布局、布局参数);
}while(data.moveToNext());
}否则{
//如果未找到任何内容,则添加空地址布局
addView(buildEmptyAddressLayout(),layoutParams);
}
打破
}
}
在地图上显示地址
MainActivity.java
public class MainActivity extends FragmentActivity implements LocationListener {
.
.
.
private void setUpMap() {
Geocoder geocoder = new Geocoder(this, Locale.US);
String staddress = "<address goes here>";
try{
List<Address> loc = geocoder.getFromLocationName(staddress, 5);
Address addr = loc.get(0);
//myLocation.setLatitude(addr.getLatitude());
//myLocation.setLongitude(addr.getLongitude());
mMap.addMarker(new MarkerOptions().draggable(true).position(new LatLng(addr.getLatitude(), addr.getLongitude())).title("Marker"));
}
catch(IOException e) {
Log.e("IOException", e.getMessage());
Toast.makeText(this, "IOException: " + e.getMessage(),20).show();
}
//mMap.addMarker(new MarkerOptions().draggable(true).position(new LatLng(0, 0)).title("Marker"));
}
公共类MainActivity扩展FragmentActivity实现LocationListener{
.
.
.
私有void setUpMap(){
Geocoder Geocoder=新的Geocoder(this,Locale.US);
字符串staddress=“”;
试一试{
List loc=geocoder.getFromLocationName(StatAddress,5);
地址addr=loc.get(0);
//myLocation.setLatitude(addr.getLatitude());
//myLocation.setLongitude(addr.getLongitude());
mMap.addMarker(新MarkerOptions().draggable(true).position(新LatLng(addr.getLatitude(),addr.getLongitude()).title(“标记”);
}
捕获(IOE异常){
Log.e(“IOException”,e.getMessage());
Toast.makeText(这个“IOException:+e.getMessage(),20.show();
}
//mMap.addMarker(新的MarkerOptions().draggable(true).position(新的LatLng(0,0)).title(“Marker”);
}
如何在地图上显示ContactAddressQuery.ADDRESS
对不起,我的英语不好
// create marker
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title(staddress);
然后展示这个
另一个选项是使用FrameLayout
。地图将添加到底部,并且TextView
将以透明或半透明的背景漂浮在其上。然后,您可以更改此TextView
的文本以显示地址。图像是在ContactDetailFragment.java中拍摄的,您有一个带有两个相同大小写的开关,这是一个输入错误吗?我有地址字符串和geocoder转换成latlng,geocoder只处理fragmentactivity。它不处理fragment。@user3901571它也将在
fragment
中处理。构造函数是geocoder(Context-Context,Locale-Locale)
。在片段中使用getActivity()
来获取宿主Activity
作为上下文。但基本上我要说的是,您需要创建一个不同的XML并将其膨胀。:)我使用((MainActivity)getActivity()).setUpMap(data.getString(ContactAddressQuery.ADDRESS));但给出一个错误。08-02 11:54:54.245:E/AndroidRuntime(9906):致命异常:main 08-02 11:54:54:54.245:E/AndroidRuntime(9906):java.lang.ClassCastException:com.example.android.contactslist.ui.ContactDetailActivity无法强制转换为com.E