2016-06-29 3 views
1

最近、GoogleマップのAPIを使用してアンドロイドスタジオでアプリケーションを作成しようとしています。ユーザーがGoogleマップでマーカーを作成して編集できるようにするアクティビティ

目的:目的は地図をクリックすることができることです は、新しい活動は、テキストフィールドは、ユーザーが名前、スニペット、場所など(マーカーのプロパティ)を編集できるようにして現れ、そして最後に作成します彼らが入力したプロパティでクリックした場所のマーカー。

私は何時間も研究してきましたが、別のクラスから新しいマーカーを作成することはできません。

マップコードクリック:あなたが上見ることができるように

public void onMapClick(LatLng latLng) { 
 
       
 
       //Coordinates of click 
 
       LatLng new_pos = new LatLng(latLng.latitude, latLng.longitude); 
 

 
       //Needs to be user-editable through an activity or pop up window 
 
       String name = "name"; 
 
       String snippet = "snippet"; 
 

 
       mMap.addMarker(new MarkerOptions() 
 
         .position(new_pos) 
 
         .title(name) 
 
         .snippet(snippet)); 
 
       mMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(lat, lon))); 
 

 
      }

を、これは、マーカーを作成し、プロパティを設定し、マップのクリックイベントです。

ただし、このマーカーのプロパティ(名前やスニペットなど)は、作成時にユーザーがテキストフィールドを使用して編集できる必要があります。

私はこの問題にどのようにアプローチすべきですか?両方LatLngMarkerOptionsとして

+0

にあなたの活動を追加することを忘れないでくださいアクティビティ?その後、ユーザーは情報を変更したり、ボタンを押したり、マーカを更新したりすることができます –

+0

これは別のアクティビティからは可能ですか?マップアクティビティを使用しているので、一連のEditTextには余裕がありません。 –

答えて

2

便利あなたがActivityを開始し、それは性質の変化するマップにMarkerを追加することから、結果を返すために、標準のAndroid mecanismを使用することができますParcelableを実装します。

activity_maps.xml(トリビアル、しかし完全な例にする)

<fragment android:id="@+id/map" 
    android:name="com.google.android.gms.maps.SupportMapFragment" 
    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" 
    tools:context="es.jcyl.ita.testmap.MapsActivity"/> 

をMapsActivity.java

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { 
    private GoogleMap mMap; 
    private static final int EDIT_REQUEST = 1; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

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

    @Override 
    public void onMapReady(GoogleMap map) { 
     this.mMap = map; 

     mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() { 
      @Override 
      public void onMapClick(final LatLng latLng) { 
       Intent edit = new Intent(MapsActivity.this, EditActivity.class); 
       edit.putExtra("location", latLng); 
       MapsActivity.this.startActivityForResult(edit, EDIT_REQUEST); 
      } 
     }); 
    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     switch(requestCode) { 
      case (EDIT_REQUEST) : { 
       if (resultCode == Activity.RESULT_OK) { 
        MarkerOptions markerOptions = data.getParcelableExtra("marker"); 
        mMap.addMarker(markerOptions); 
       } 
       break; 
      } 
     } 
    } 
} 

editactivity.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical"> 

    <EditText 
     android:id="@+id/title" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="Title"/> 

    <Button 
     android:id="@+id/save" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Save"/> 
</LinearLayout> 

EditActivity.java

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

     final LatLng latlng = (LatLng) getIntent().getParcelableExtra("location"); 

     final EditText title = (EditText) findViewById(R.id.title); 
     Button boton = (Button) findViewById(R.id.save); 
     boton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(final View view) { 
       MarkerOptions marker = new MarkerOptions().position(latlng); 
       if (title.getText() != null) { 
        marker.title(title.getText().toString()); 
       } 

       Intent resultIntent = new Intent(); 
       resultIntent.putExtra("marker", marker); 
       setResult(Activity.RESULT_OK, resultIntent); 
       finish(); 
      } 
     }); 
    } 
} 

注:おそらくあなたが同じに表示 `EditText`sのセットにその情報を転送することができAndroidManifest.xmlファイル

+0

これはちょうどうまくいきます! –

関連する問題