【发布时间】:2015-05-19 11:39:57
【问题描述】:
我和我的朋友正在创建一个涉及使用谷歌地图的手机应用程序,这样我们就可以放置标记,一旦选择就会显示信息。我们遇到的问题是我们无法让谷歌地图插件工作。
我们做了以下工作: - 新增:Android 支持存储库、Google play 服务、Google 存储库。 - 以下内容已添加到我们的 gradle 模块中。
android {
compileSdkVersion 21
buildToolsVersion '21.1.2'
defaultConfig {
applicationId "com.example.wilmar.rentacube"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
我们还将项目与 gradle 文件同步。但是,当我们尝试在模拟器中运行它时,它说我们需要更新 google play 服务。
任何帮助将不胜感激, 提前谢谢你。:)
我们的谷歌地图活动中的代码应要求:
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.example.wilmar.rentacube.R;
import com.google.android.gms.maps.GoogleMap;
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 GoogleMapsActivity extends FragmentActivity {
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_google_maps);
setUpMapIfNeeded();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
/**
* Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly
* installed) and the map has not already been instantiated.. This will ensure that we only ever
* call {@link #setUpMap()} once when {@link #mMap} is not null.
* <p/>
* If it isn't installed {@link SupportMapFragment} (and
* {@link com.google.android.gms.maps.MapView MapView}) will show a prompt for the user to
* install/update the Google Play services APK on their device.
* <p/>
* A user can return to this FragmentActivity after following the prompt and correctly
* installing/updating/enabling the Google Play services. Since the FragmentActivity may not
* have been completely destroyed during this process (it is likely that it would only be
* stopped or paused), {@link #onCreate(Bundle)} may not be called again so we should call this
* method in {@link #onResume()} to guarantee that it will be called.
*/
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
/**
* This is where we can add markers or lines, add listeners or move the camera. In this case, we
* just add a marker near Africa.
* <p/>
* This should only be called once and when we are sure that {@link #mMap} is not null.
*/
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
}
注意!:此代码由 Android Studio 自动生成!
【问题讨论】:
-
已经为谷歌地图创建了 api 密钥?
-
是的,这已被创建
-
请发布您的代码
-
我们正在使用 google maps 活动,由 android studio 提供给我们
标签: android google-maps android-studio google-play-services