Android:在不使用Fragment或MapsActivity的情况下显示MapView

时间:2017-02-02 18:34:35

标签: java android google-maps android-gradle

我想显示一个MapView,但MapView始终是灰色的,左下角是Google图标。有人告诉我,如果活动实现OnMapReadyCallBack(),我们可以在没有MapsActivity的情况下使用MapView。 我不知道我想念的是什么

这是我的应用程序gradle文件

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "24.0.3"

    defaultConfig {
        applicationId "pls.annesophie.androidvoyagesmets"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.google.android.gms:play-services:7.5.0'
    compile 'com.android.support:appcompat-v7:23.4.0'


}

这是我的项目gradle文件

    // Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.0'
        classpath 'com.google.gms:google-services:3.0.0'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

我的活动:

public class DetailActivity extends AppCompatActivity implements OnMapReadyCallback {

private GoogleMap map;


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


    // Gets the MapView from the XML layout and creates it
    MapView mapView = (MapView) findViewById(R.id.map);
    mapView.onCreate(savedInstanceState);

    // Gets to GoogleMap from the MapView and does initialization stuff
    map = mapView.getMap();
    map.getUiSettings().setMyLocationButtonEnabled(false);
    map.setMyLocationEnabled(true);
    MapsInitializer.initialize(DetailActivity.this);
    onMapReady(map);

    // Updates the location and zoom of the MapView
    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10);
    map.animateCamera(cameraUpdate);

//A lot of non pertinent things i think
}

@Override
public void onMapReady (GoogleMap map){
    this.map = map;
    map.setMyLocationEnabled(true);
}

感谢您的帮助

0 个答案:

没有答案