【问题标题】:java - cannot resolve getMap() google maps api v2java - 无法解析 getMap() google maps api v2
【发布时间】:2016-11-12 10:54:39
【问题描述】:

您好,我正在尝试将一个 kml 文件导入到我的 android 应用程序中,该应用程序将为文件中的每个点放置标记。我正在遵循

的基本说明

https://developers.google.com/maps/documentation/android-api/utility/setup

当我尝试打电话时

KmlLayer layer = new KmlLayer(getMap(), R.raw.kmlFile, getApplicationContext());

我得到错误无法解析 getMap()。据我所知,它应该包含在 com.google.android.gms.maps.SupportMapFragment 中;但是,即使调用它仍然无法正常工作。

这是我的java

package pcbapps.pokemap;

import android.net.Uri;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;


public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
private KmlLayer layer;
private double lat, lng;
/**
 * ATTENTION: This was auto-generated to implement the App Indexing API.
 * See https://g.co/AppIndexing/AndroidStudio for more information.
 */
private GoogleApiClient client;

@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);
    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}


/**
 * 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);


    KmlLayer layer;
    layer = new KmlLayer(getMap(), R.raw.scullin, getApplicationContext());
    layer.addLayerToMap();
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));

    //mMap.addMarker(new MarkerOptions().)



}


@Override
public void onStart() {
    super.onStart();

    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    client.connect();
    Action viewAction = Action.newAction(
            Action.TYPE_VIEW, // TODO: choose an action type.
            "Maps Page", // TODO: Define a title for the content shown.
            // TODO: If you have web page content that matches this app activity's content,
            // make sure this auto-generated web page URL is correct.
            // Otherwise, set the URL to null.
            Uri.parse("http://host/path"),
            // TODO: Make sure this auto-generated app URL is correct.
            Uri.parse("android-app://pcbapps.pokemap/http/host/path")
    );
    AppIndex.AppIndexApi.start(client, viewAction);
}

@Override
public void onStop() {
    super.onStop();

    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    Action viewAction = Action.newAction(
            Action.TYPE_VIEW, // TODO: choose an action type.
            "Maps Page", // TODO: Define a title for the content shown.
            // TODO: If you have web page content that matches this app activity's content,
            // make sure this auto-generated web page URL is correct.
            // Otherwise, set the URL to null.
            Uri.parse("http://host/path"),
            // TODO: Make sure this auto-generated app URL is correct.
            Uri.parse("android-app://pcbapps.pokemap/http/host/path")
    );
    AppIndex.AppIndexApi.end(client, viewAction);
    client.disconnect();
}
}

这也是将信息从 kml 文件导入 android 的正确方法。感谢任何人的帮助。

【问题讨论】:

    标签: java android google-maps android-studio kml


    【解决方案1】:

    无需调用 getMap(),因为回调“onMapReady(GoogleMap googleMap)”提供了 GoogleMap 实例。 所以只需将调用更改为:

    layer = new KmlLayer(googleMap, R.raw.scullin, getApplicationContext());
    

    【讨论】:

      【解决方案2】:

      改变

      layer = new KmlLayer(getMap(), R.raw.scullin, getApplicationContext());
      

      layer = new KmlLayer(mMap, R.raw.scullin, getApplicationContext());
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-18
        • 2013-02-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多