0

我在下面发布了代码片段,因为我找不到命令setUpMapIfNeeded();。当我输入必要的命令时,到处都是红线。任何人都可以帮我解决这个代码吗?另外,我的模拟器中没有任何标记。

我愿意接受建议,请帮助我。我用的min sdk版本是17,android studio版本是2.1。我正在使用 Googlemaps 活动。我仍然不明白为什么我没有得到setUpMapIfNeeded命令以及内置代码片段的其余部分。

代码截图

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }
}

同样在尝试丢失的代码后,我无法获得任何标记。然而,无论我在哪里在线搜索,我都会得到这个教程

如果您知道任何其他代码,请告诉我。

4

2 回答 2

1

当您使用 MapsActivty 时,不需要任何额外的代码。所有必要的代码都是由 Android Studio 自己生成的。如果您查看教程,setUpMapIfNeeded() 中的代码与您在 onCreate() 中的代码相同。添加 Google MapsActivty 时,显示地图所需的只是Google Maps API 密钥。将 APi 密钥粘贴到您的“google_maps_api.xml”(在 values 文件夹内)中,您就完成了。运行应用程序,您将在 Activity 中获得地图。

获取 Google Maps API 密钥的步骤在google_maps_api.xml 的注释中给出。

另外,如果要添加自己的标记,请使用以下代码

public void setMarkerOnMap(String name, LatLng l) {
    // Creating a marker
    MarkerOptions markerOptions = new MarkerOptions();

    // Setting the position for the marker
    markerOptions.position(l);

    // Setting the title for the marker.
    // This will be displayed on taping the marker
    markerOptions.title(name);

    // Placing a marker on the touched position
    mMap.addMarker(markerOptions);
}

像这样调用这个函数:

LatLng loc=new LatLng(latitude,longitude);
String str_placeName="Place Name";
setMarkerOnMap(str_placeName,loc);
于 2016-04-30T10:21:46.573 回答
0

您需要调用setUpMapIfNeeded()方法onCreate()

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    setUpMapIfNeeded();
}

检查本教程关于如何添加标记的问题。

于 2016-04-30T09:19:23.397 回答