Java 谷歌地图片段不';我跟不上
在花了一些时间为几家公司编写网站后,我刚刚开始开发移动应用程序,但我肯定错过了一些东西。我已经建立了一个应用程序的主要活动和谷歌地图活动 activity_main.xmlJava 谷歌地图片段不';我跟不上,java,android,google-maps,android-fragments,Java,Android,Google Maps,Android Fragments,在花了一些时间为几家公司编写网站后,我刚刚开始开发移动应用程序,但我肯定错过了一些东西。我已经建立了一个应用程序的主要活动和谷歌地图活动 activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" an
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<include
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout="@layout/activity_maps"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
activity_maps.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:id="@+id/map"
tools:context="com.monkeybrainapps.sidechalkv1.MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment" />
MapsActivity.java
public class MapsActivity 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_maps);
setUpMapIfNeeded();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
/**
* Sets up the map if it is possible to do so (i.e., the Googlce Play services APK is corretly
* 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").snippet("Snippet"));
// Enable MyLocation Layer of Google Map
mMap.setMyLocationEnabled(false);
// Get LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Create a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Get the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
// Get Current Location
Location myLocation = locationManager.getLastKnownLocation(provider);
// set map type
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
// Get latitude of the current location
double latitude = myLocation.getLatitude();
// Get longitude of the current location
double longitude = myLocation.getLongitude();
// Create a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
// Show the current location in Google Map
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
mMap.animateCamera(CameraUpdateFactory.zoomTo(18));
mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("You are here!").snippet("Consider yourself located"));
mMap.getUiSettings().setAllGesturesEnabled(false);
}
}
公共类映射活动扩展了FragmentActivity{
私有GoogleMap mMap;//如果Google Play services APK不可用,则可能为空。
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_映射);
setupmapifneed();
}
@凌驾
受保护的void onResume(){
super.onResume();
setupmapifneed();
}
/**
*如果可能的话,设置地图(例如,Googlce Play services APK正确
*已安装)并且映射尚未实例化。这将确保
*当{@link#mMap}不为null时,调用{@link#setUpMap()}一次。
*
*如果未安装{@link SupportMapFragment}(和
*{@link com.google.android.gms.maps.MapView MapView})将提示用户
*在其设备上安装/更新Google Play services APK。
*
*用户可以按照提示正确返回此FragmentActivity
*安装/更新/启用Google Play服务。因为碎片活动可能不会
*在这一过程中被完全破坏(很可能
*停止或暂停),{@link#onCreate(Bundle)}可能不会被再次调用,因此我们应该调用它
*方法来保证将调用它。
*/
私有void setUpMapIfNeeded(){
//执行空检查以确认尚未实例化映射。
如果(mMap==null){
//尝试从SupportMapFragment获取映射。
mMap=((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
//检查我们是否成功获得地图。
如果(mMap!=null){
setUpMap();
}
}
}
/**
*在这里,我们可以添加标记或线条、添加侦听器或移动摄影机
*在非洲附近加一个标记。
*
*当我们确定{@link#mMap}不是null时,应该只调用一次。
*/
私有void setUpMap(){
mMap.addMarker(newmarkeroptions().position(newlatlng(0,0)).title(“Marker”).snippet(“snippet”);
//启用Google地图的MyLocation层
mMap.setMyLocationEnabled(false);
//从系统服务位置\u服务获取LocationManager对象
LocationManager LocationManager=(LocationManager)getSystemService(Context.LOCATION\u服务);
//创建条件对象以检索提供程序
标准=新标准();
//获取最佳提供商的名称
字符串提供程序=locationManager.getBestProvider(条件为true);
//获取当前位置
Location myLocation=locationManager.getLastKnownLocation(提供者);
//设置地图类型
mMap.setMapType(GoogleMap.MAP\u TYPE\u NORMAL);
//获取当前位置的纬度
双纬度=myLocation.getLatitude();
//获取当前位置的经度
double longitude=myLocation.getLongitude();
//为当前位置创建板条对象
LatLng LatLng=新LatLng(纬度、经度);
//在谷歌地图中显示当前位置
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
//放大谷歌地图
mMap.animateCamera(CameraUpdateFactory.zoomTo(18));
mMap.addMarker(newmarkeroptions().position(newlatlng(纬度、经度)).title(“你在这里!”).snippet(“认为你自己在这里”);
mMap.getUiSettings().setAllGesturesEnabled(false);
}
}
我可以看到我的activity_主要指向activity_映射的方式,以及activity_映射如何连接到MapsActivity,但没有使用setUpMap方法中MapsActivity中的设置。我知道这段代码可以工作,因为我已经在一个新的谷歌地图应用程序中测试了这段代码,该应用程序可以从Android Studio新项目向导创建
我如何才能让我的片段按照它应该从中提取的文件中的说明进行操作?我觉得我一定是错过了什么。检查google play服务是否最新,或者检查地图的apikey,或者尝试我的基本地图应用程序。