Android中的GoogleMap不显示标记

时间:2016-03-24 04:31:59

标签: android google-maps-android-api-2

我正在为我的一个课程制作应用程序,我需要在地图上显示标记。发生了两件奇怪的事情:首先,onMapReady正在执行两次,其次,标记没有出现在地图上。这是我的代码:

Feature:
  @api
  Scenario: Clear cache
    Given the cache has been cleared
    When I am on the homepage
    Then I should get a "200" HTTP response

  Scenario:
    Given I am not logged in
    When I am on the homepage
    Then I should see the text "We love our users"

我希望你们中的一些人能够帮我弄清楚为什么标记没有出现在地图上。

提前谢谢你,

修改

这是xml:

public class MapFragment extends SupportMapFragment implements OnMapReadyCallback {

private GoogleMap mMap;
private Event mEventSelected;
private Person mPersonSelected;
private Map<Marker, Event> mMapMarkers;
private List<Polyline> mLines;
private TextView mPersonFullName;
private TextView mEventDescription;
private LinearLayout mMapEvent;
private ImageView mEventIcon;
private MenuItem searchIcon;
private MenuItem filterIcon;
private MenuItem settingsIcon;
private MenuItem topIcon;
private String EVENT_ID;


@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    mMapMarkers = new HashMap<>();
    mLines = new ArrayList<>();
    mEventSelected = null;
    mPersonSelected = null;

    getMapAsync(this);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

    super.onCreateView(inflater, container, savedInstanceState);
    View v = inflater.inflate(R.layout.fragment_map, container, false);
    drawDefaultLayout(v);
    return v;
}
private void pinEvents() {
    mMap.clear();
    Map <String, Event> events = FamilyMapData.getEvents();

    for (Event event : events.values())
    {
        LatLng coordinates = new LatLng(event.getLatitude(), event.getLongitude());
        String description = event.getDescription();
        float color = 0;
        switch (description){
            case "baptism":{
                color = BitmapDescriptorFactory.HUE_CYAN;
                break;
            }
            case "birth":{
                color = BitmapDescriptorFactory.HUE_BLUE;
                break;
            }
            case "death":{
                color = BitmapDescriptorFactory.HUE_AZURE;
                break;
            }
            case "marriage":{
                color = BitmapDescriptorFactory.HUE_RED;
                break;
            }
            case "christening":{
                color = BitmapDescriptorFactory.HUE_ORANGE;
                break;
            }
            case "census":
            {
                color = BitmapDescriptorFactory.HUE_GREEN;
                break;
            }
        }


        mMapMarkers.put(mMap.addMarker(new MarkerOptions().position(coordinates).icon(BitmapDescriptorFactory.defaultMarker(color))), event);

    }
}
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    pinEvents();
}

1 个答案:

答案 0 :(得分:0)

我猜你的onMapReady函数根本没有触发。 所以你的pinEvents函数也没有被触发。

你必须调用getMapAsync()来触发onMapReady()你在onCreate这里完成的视图仍然没有创建,所以在onCreateView中这样做。

我建议您更改类的名称,因为有与谷歌地图相关的MapFragment和SupportMapFragment以避免混淆。

如果您使用支持片段,则需要更改以下代码。

SupportMapFragment mapFragment =  ((SupportMapFragment)getActivity().getSupportFragmentManager())
                        .findFragmentById(R.id.map);
                    mapFragment.getMapAsync(this);

否则使用这个:

MapFragment mapFragment = (MapFragment) getFragmentManager()
                    .findFragmentById(R.id.map);
                mapFragment.getMapAsync(this);

希望这有帮助。