2016-08-10 8 views
3

私はAndroidスタジオ2.1.2で作業しています。私は周りにチェックして、ほとんどの質問は、Androidスタジオの古いバージョンと私の状況に適用されないいくつかの古いクラスを使用します。アクティビティの中間エリアにあるGoogleマップ

[ファイル]> [新規プロジェクト]>私はオプションのGoogleマップアクティビティを使用しました。私はそこにあるデフォルトのコードを変更していません。そのXMLには断片があります。今、この活動を、断片を使って別の活動に取り入れる方法がありますか?基本的には、画面の中央に地図を表示するアプリを作っています。この真ん中の部分は断片だと思っています。下部および上部には、ユーザーが対話するためのいくつかのコンポーネントがあります。

マップを新しいアクティビティに表示するのではなく、特定のアクティビティの中間部分に残しておきます。

デフォルトのGoogleマップアクティビティに新しいUIコンポーネントを追加するだけですか?

最初に、フラグメント(独自の.javaファイル、XMLレイアウト)を作成し、そこでマップコードを配置して、必要なアクティビティに引き継ぐ考えがありました。これを行うための最良の方法は何でしょうか?私はどんな助けにも感謝します。

+0

あなたは、標準のGoogleマップのアクティビティテンプレートを使用するには...あなたが欲しいしかし、変更する自由を感じます。または最初から始めましょう。あなたは何を持っているのですか? –

+0

私の問題は、私はすでに中央の地図、つまり主なアクティビティだけを必要とする作業中のアクティビティを既に持っているので、最初から始めたくないということです。私はそこにライフサイクルコールバックを実装したいと思っています。私は後でそれを実現するためにGoogleマップのアクティビティを作成しました。 – user3650467

+0

最初からアプリを起動するという意味ではありませんでした。つまり、空のXMLとJavaファイルを作成してから、Google Mapsの部分を読み込み始めます。 –

答えて

7

私は、同じ結果、アクティビティ内のマップ、他のUIコンポーネントと一緒に到着できることを理解しました。 Googleマップアクティビティが使用されているかどうか、または空のアクティビティが作成され、マップコードがその中に実装されているかどうか。私のコードは以下に含まれています。私はEmpty Activity(Android 2.1.2)を選択し、マップコードを.javaファイルに持ってきて、タグのあるマップフラグメントをアクティビティのXMLレイアウトに配置しました。

エラーは、私はXMLレイアウトで

android:name="com.google.android.gms.maps.MapFragment" 

を使用しますが、宣言とSupportMapFragmentでmapFragment(オブジェクトインスタンス)を初期化していたということでした。 Googleドキュメントからコードをコピーして貼り付ける際に混乱が生じました。ここで自分の投稿を更新しようとしていたので、私はそのエラーを見つけました。正しいことは、MapFragment(APIレベル12以上)を使用している場合、それをすべて使用することです。 SupportMapFragment(APIレベル12以下)を使用する場合は、SupportMapFragmentをすべて使用してください。

以下のコードは修正です。誰かが同じ問題を抱えている場合に備えて。

XMLレイアウト(activity_main.xml)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.example.myapp.MainActivity"> 

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="70dp" 
     android:orientation="horizontal"> 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/teachers_link" 
      android:layout_marginLeft="20dp" 
      android:layout_marginRight="20dp" /> 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/parent_link" 
      android:layout_marginLeft="20dp" /> 

    </LinearLayout> 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="340dp" 
     android:orientation="vertical"> 
     <fragment xmlns:android="http://schemas.android.com/apk/res/android" 
      android:name="com.google.android.gms.maps.SupportMapFragment" 
      android:id="@+id/map" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 
    </LinearLayout> 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="135dp" 
     android:layout_marginTop="12dp" 
     android:orientation="vertical"> 
     <EditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:hint="@string/search_term" /> 
     <Spinner 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="5dp" 
      android:layout_marginBottom="5dp" 
      android:id="@+id/select_level" 
      android:entries="@array/select_level"></Spinner> 
     <Spinner 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/select_course" 
      android:entries="@array/select_course"></Spinner> 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/btn_search" /> 
    </LinearLayout> 


</LinearLayout> 

</ScrollView> 

</LinearLayout> 

.javaファイル(MainActivity.java)

package name; 

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 MainActivity extends AppCompatActivity implements OnMapReadyCallback{ 

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

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.map); 
    mapFragment.getMapAsync(this); 
} 


@Override 
public void onMapReady(GoogleMap map) { 
    map.addMarker(new MarkerOptions() 
      .position(new LatLng(0, 0)) 
      .title("Marker")); 
} 

} 
関連する問題