在mapview上显示地址

时间:2014-08-02 07:28:52

标签: android android-mapview

我有两个班级

Contactdetailfragment从联系人列表中获取地址。

ContactDetailFragment.java

 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;
            }
        }

MainActivity在地图上显示地址

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"));

    }

如何在地图上显示ContactAddressQuery.ADDRESS?

抱歉我的英语不好。

1 个答案:

答案 0 :(得分:0)

// create marker 
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title(staddress);  

然后显示这个?

另一种选择是使用FrameLayout。地图将添加到底部,TextView将浮动在透明或半透明背景上。然后,您可以更改此TextView的文字以显示地址 enter image description here

拍摄图像from this blog post