将地图添加到班级

时间:2014-03-22 22:06:36

标签: google-maps map

您好我想在页面中添加地图(静态图片)。我将尝试解释到目前为止我所做的事情,以及我的问题是否能得到解决,以防其他人在将来遇到同样的问题。

我做了以下事情: 1.使用权限/ API更新了清单文件(使用来自Eclipse的SHA1使用Window> Preferences> Build获取API(来自Google API控制台)密钥 2.更新了将添加地图的类的XML文件 3.在Java文件类

中添加了一些Java代码

以下是清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.learn2develop.lapmaster"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/lapmaster_icon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="net.learn2develop.lapmaster.LapMasterActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:label="Logged In"
            android:name=".UserLoggedInScreen" >
                <intent-filter >
                    <action android:name="net.learn2develop.UserLoggedInScreen" />
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>        
        </activity>
        <activity
            android:label="Track Listing"
            android:name=".ListOfCircuits" >
                <intent-filter >
                    <action android:name="net.learn2develop.ListOfCircuits" />
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>        
        </activity>
        <activity
            android:label="Snetterton"
            android:name=".Snetterton" >
                <intent-filter >
                    <action android:name="net.learn2develop.Snetterton" />
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>        
        </activity>
        <!-- Other activities -->
        <!-- Google Play Services -->
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="Various" />
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />
    </application>
</manifest>

以下是XML类:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/map"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:name="com.google.android.gms.maps.SupportMapFragment" />

以下是Java类:

import android.app.Activity;
import android.os.Bundle;
import com.google.android.gms.maps.SupportMapFragment;

public class Snetterton extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.snetterton);

    SupportMapFragment map = ((SupportMapFragment)
    .getSupportFragmentManager().findFragmentById(R.id.map))
    .getMap();
}
}

正如您可能从我的代码中猜到的那样,我正在为赛道创建一个应用程序。在名为Snetterton的课程中,我想添加一张Snetterton的图片(谷歌地图)。

要到达此页面,用户: 1.从登录屏幕开始(LapMasterActivity) 2.迎接主页(UserLoggedInScreen) 3.转到带有曲目列表的页面(现在只是Snetterton)(ListOFTracks) 4.当用户单击Snetterton的按钮时,它会将它们带到Snetterton类的页面,我想要地图。 (斯内特顿)。

仅在我的代码中((SupportMapFragment)带下划线。

感谢。

1 个答案:

答案 0 :(得分:0)

自己回答(最终):

我使用的代码是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayoutFragment"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>


public class TopGear extends Activity
{   
private GoogleMap aMap;

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.top_gear);

    aMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
    aMap.getUiSettings().setAllGesturesEnabled(false);

    LatLng topGearTrack = new LatLng(51.116492, -0.541767);

    aMap.setMyLocationEnabled(true);
    aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(topGearTrack, 15));

    aMap.addMarker(new MarkerOptions()
            .title("Top Gear")
            .snippet("Welcome to the famous Top Gear circuit.")
            .position(topGearTrack));

    aMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);

    setUpMapIfNeeded();
}

public void setUpMapIfNeeded() 
{
    //Do a null check to confirm that we have not already instantiated the map.
    if (aMap == null) 
    {
        aMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
                            .getMap();
    //Check if we were successful in obtaining the map.
    if (aMap != null) 
    {
        //The Map is verified. It is now safe to manipulate the map.
        }
    }
}
}