2016-08-30 8 views
0

ご質問は申し訳ありませんが、私にとってはうまくいかない状況はありませんでした。確かにこれは超簡単だ、私はちょうど正しい方向に指される必要がある。AndroidスタジオはオーバーライドメソッドでXMLから情報を取得します

目的:
ユーザーが指定した名前を使用して、マーカーのタイトルに追加できるようにします。

私が知っているもの:
xmlを膨らませて、メソッドがオーバーライドされているという理由でそれを取得できません。私の知る限りでは、メソッドをオーバーライドできなくなります。私はこれにどのようにアプローチすべきですか?

MapsAcitivy

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, addPlace.Communicator { 

    private GoogleMap mMap; 
    public static final int MY_PERMISSION_ACCESS_FINE_LOCATION = 0; 
    int mStackLevel; 
    int YES_NO_CALL; 
    FragmentManager manager = getSupportFragmentManager(); 
    final Context context = this; 

    @InjectView(R.id.btn_login) Button _loginButton; 

    ClusterManager<ClusterRequest> mClusterManager; 


    class CustomInfoWindowAdapter implements GoogleMap.InfoWindowAdapter { 

     // These are both viewgroups containing an ImageView with id "badge" and two TextViews with id 
     // "title" and "snippet". 
     private final View mWindow; 

     //private final View mContents; 

     CustomInfoWindowAdapter() { 
      mWindow = getLayoutInflater().inflate(R.layout.info_window_layout, null); 
      //mContents = getLayoutInflater().inflate(R.layout.custom_info_contents, null); 
     } 

     @Override 
     public View getInfoWindow(Marker marker) { 
      render(marker, mWindow); 
      return mWindow; 
     } 

     @Override 
     public View getInfoContents(Marker marker) { 
      // Getting view from the layout file info_window_layout 
      View v = getLayoutInflater().inflate(R.layout.info_window_layout, null); 

      // Getting the position from the marker 
      LatLng latLng = marker.getPosition(); 

      // Getting reference to the TextView to set latitude 
      TextView tvLat = (TextView) v.findViewById(R.id.tv_lat); 

      // Getting reference to the TextView to set longitude 
      TextView tvLng = (TextView) v.findViewById(R.id.tv_lng); 

      // Setting the latitude 
      tvLat.setText("Latitude:" + latLng.latitude); 

      // Setting the longitude 
      tvLng.setText("Longitude:"+ latLng.longitude); 

      // Returning the view containing InfoWindow contents 
      return v; 
      //render(marker, mContents); 
      //return mContents; 
     } 

     private void render(Marker marker, View view) { 
     } 
    } 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_maps); 
     // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
     SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 
    } 

    @Override 
    @TargetApi(23) 
    public void onMapReady(GoogleMap googleMap) { 
     mMap = googleMap; 

     // Here, thisActivity is the current activity 
     if (ContextCompat.checkSelfPermission(this, 
       Manifest.permission.ACCESS_FINE_LOCATION) 
       != PackageManager.PERMISSION_GRANTED) { 

      // Should we show an explanation? 
      if (ActivityCompat.shouldShowRequestPermissionRationale(this, 
        Manifest.permission.ACCESS_FINE_LOCATION)) { 

       // Show an expanation to the user *asynchronously* -- don't block 
       // this thread waiting for the user's response! After the user 
       // sees the explanation, try again to request the permission. 

      } else { 

       // No explanation needed, we can request the permission. 

       ActivityCompat.requestPermissions(this, 
         new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
         MY_PERMISSION_ACCESS_FINE_LOCATION); 

       // MY_PERMISSION_ACCESS_FINE_LOCATION is an 
       // app-defined int constant. The callback method gets the 
       // result of the request. 
      } 
     } 
     if (ContextCompat.checkSelfPermission(this, 
       Manifest.permission.ACCESS_FINE_LOCATION) 
       == PackageManager.PERMISSION_GRANTED) { 
      mMap.setMyLocationEnabled(true); 
      LocationManager initialLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
      Criteria criteria = new Criteria(); 
      String initialProvider = initialLocationManager.getBestProvider(criteria, true); 
      Location initialLocation = initialLocationManager.getLastKnownLocation(initialProvider); 
      mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
      double initialLatitude = initialLocation.getLatitude(); 
      double initialLongitude = initialLocation.getLongitude(); 
      LatLng initialLatLng = new LatLng(initialLatitude, initialLongitude); 
      mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(initialLatLng, 14.9f)); 
      //mMap.setOnInfoWindowClickListener(this); 
      mClusterManager = new ClusterManager<ClusterRequest>(getApplicationContext(), mMap); 
      mMap.setOnCameraChangeListener(mClusterManager); 
      mMap.setOnMarkerClickListener(mClusterManager); 
     } 
    } 

    public void onButtonClick(View view){ 
     //do something when button is clicked. 
     final Dialog dialog = new Dialog(this); 
     dialog.setContentView(R.layout.add_place); 
     dialog.setTitle("Add Place"); 

     Button btnLogin = (Button) dialog.findViewById(R.id.addPlacebtnSubmit); 
     Button btnCancel = (Button) dialog.findViewById(R.id.btnCancel); 
     final EditText txtUsername = (EditText)dialog.findViewById(R.id.txtStopName); 

     // Attached listener for add place GUI button 
     btnLogin.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       String stopName = txtUsername.getText().toString(); 
       if(stopName.length() > 4){ 
        // Here, thisActivity is the current activity 
        if (ContextCompat.checkSelfPermission(MapsActivity.this, 
          android.Manifest.permission.ACCESS_FINE_LOCATION) 
          != PackageManager.PERMISSION_GRANTED) { 

         // Should we show an explanation? 
         if (ActivityCompat.shouldShowRequestPermissionRationale(MapsActivity.this, 
           android.Manifest.permission.ACCESS_FINE_LOCATION)) { 

          // Show an expanation to the user *asynchronously* -- don't block 
          // this thread waiting for the user's response! After the user 
          // sees the explanation, try again to request the permission. 

         } else { 

          // No explanation needed, we can request the permission. 

          ActivityCompat.requestPermissions(MapsActivity.this, 
            new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 
            MY_PERMISSION_ACCESS_FINE_LOCATION); 

          // MY_PERMISSION_ACCESS_FINE_LOCATION is an 
          // app-defined int constant. The callback method gets the 
          // result of the request. 
         } 
        } 
        LocationManager currentLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
        Criteria currentCriteria = new Criteria(); 
        String currentProvider = currentLocationManager.getBestProvider(currentCriteria, true); 
        Location currentLocation = currentLocationManager.getLastKnownLocation(currentProvider); 
        double currentLatitude = currentLocation.getLatitude(); 
        double currentLongitude = currentLocation.getLongitude(); 
        LatLng currentLatLng = new LatLng(currentLatitude, currentLongitude); 

        ClusterRequest testCluster = new ClusterRequest(currentLatitude, currentLongitude); 
        mClusterManager.addItem(testCluster); 
        mClusterManager.setRenderer(new MapIconRender(getApplicationContext(), mMap, mClusterManager)); 

        dialog.dismiss(); 

       } 
       else{ 
        txtUsername.setError("Stop name must be at least 5 characters long."); 
       } 
      } 
     }); 
} 

MapIconRender.java

public class MapIconRender extends DefaultClusterRenderer<ClusterRequest> { 

     public MapIconRender(Context context, GoogleMap map, 
          ClusterManager<ClusterRequest> clusterManager) { 
      super(context, map, clusterManager); 
     } 

      @Override 
      protected void onBeforeClusterItemRendered (ClusterRequest item, MarkerOptions 
      markerOptions){ 
       markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.map_marker_outside_azure)); 
       markerOptions.snippet("Status: "); 
       markerOptions.title(stopName); //<------ THIS IS WHERE I WANT THE STOP NAME TO BE WHAT THE USER ENTERED. 
       super.onBeforeClusterItemRendered(item, markerOptions); 
      } 
} 

add_place.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_height="match_parent" 
    android:paddingLeft="10dp" android:paddingRight="10dp" 
    android:background="#fff" android:layout_width="300dp" 
    android:paddingTop="10dp"> 
    <View 
     android:id="@+id/HorizontalLine" 
     android:layout_width="match_parent" 
     android:layout_height="1dip" 
     android:background="#aaa" /> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="Name:" 
     android:paddingTop="20dp" 
     android:id="@+id/name" /> 
    <EditText android:layout_height="wrap_content" 
     android:id="@+id/txtStopName" 
     android:maxLength="100" 
     android:singleLine="true" 
     android:hint="Name of this stop (5-100 characters)" 
     android:layout_width="match_parent"> 
     <requestFocus></requestFocus> 
    </EditText> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="Type:" 
     android:paddingTop="20dp" 
     android:layout_marginBottom="10dp" 
     android:id="@+id/type" /> 

    <Spinner 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/spinner" 
     android:entries="@array/type_arrays" 
     android:prompt="@string/type_prompt" 
     android:layout_marginBottom="10dp"/> 
    <View 
     android:id="@+id/HorizontalLine2" 
     android:layout_width="match_parent" 
     android:layout_height="1dip" 
     android:background="#aaa" /> 
    <LinearLayout android:layout_width="match_parent" 
     android:layout_height="wrap_content" android:id="@+id/linearLayout1" 
     android:paddingTop="5dp"> 

     <Button android:layout_width="wrap_content" 
      android:layout_height="wrap_content" android:layout_weight="0.5" 
      android:id="@+id/btnCancel" android:text="Cancel" 
      android:onClick="addPlaceCancel"/> 
     <Button android:layout_width="wrap_content" 
      android:layout_height="wrap_content" android:layout_weight="0.5" 
      android:id="@+id/addPlacebtnSubmit" android:text="Submit" 
      android:onClick="addPlaceSubmit" 
      android:layout_marginBottom="10dp"/> 
    </LinearLayout> 
</LinearLayout> 

ClusterRequest.java

public class ClusterRequest implements ClusterItem { 
private final LatLng mPosition; 

public ClusterRequest(double lat, double lng) { 
    mPosition = new LatLng(lat, lng); 
} 

@Override 
public LatLng getPosition() { 
    return mPosition; 
    } 
} 

答えて

0

これはあなたが(私に知らせてください)が、私はのためにリリースGoogleのデモに驚くほど十分な答えを見つけた何を意味するのかである場合@SuhyeonLeeわかりませんそれらのカスタマイズクラスタデモ私は何を学んだのだろうか。
mClusterManager.setRenderer(new MapIconRender(getApplicationContext(), mMap, mClusterManager));

経由MapIconRender.javaクラスを呼び出すときClusterRequestクラス内の変数は、ここMapIconRenderでアクセスできるようになりますどんな意味MapIconRender.javaクラス

protected void onBeforeClusterItemRendered (ClusterRequest item, MarkerOptions 
      markerOptions) 

にこの方法を実行します。 javaクラスであり、(item.variableName)を介してアクセスできます。私はClusterRequestファイルのコードを投稿しなかったことをお詫びします。私はそれが必要だとは思わなかった。私の元の質問にそのスニペットを追加してください。

次のようにとにかく、私は追加するために必要な新しいコードはでした:
ClusterRequest testCluster = new ClusterRequest(currentLatitude, currentLongitude, stopName); //stopName was added here.

そして中:私は経由ClusterRequestクラスにマーカーの名前(またはマーカーのタイトルを)渡すために必要なMapsActivityクラスで
私が広告することができます 私MapIconRender.javaクラスの

public class ClusterRequest implements ClusterItem { 
private final LatLng mPosition; 
public final String stopName; //Add a "global variable" here so it can be accessible in the MapIconRender class. 

public ClusterRequest(double lat, double lng, String _stopName) { 
    mPosition = new LatLng(lat, lng); 
    stopName = _stopName; 
} 

@Override 
public LatLng getPosition() { 
    return mPosition; 
    } 
} 



そして最後に:私は介して、マーカー名(別名stopName)の「グローバル変数」を追加する必要がClusterRequestクラスDを経由して、マーカーのタイトル名:

public class MapIconRender extends DefaultClusterRenderer<ClusterRequest> { 

     public MapIconRender(Context context, GoogleMap map, 
          ClusterManager<ClusterRequest> clusterManager) { 
      super(context, map, clusterManager); 
     } 

      @Override 
      protected void onBeforeClusterItemRendered (ClusterRequest item, MarkerOptions 
      markerOptions){ 
       markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.map_marker_outside_azure)); 
       markerOptions.snippet("Status: "); 
       markerOptions.title("Title: " + item.stopName); //Now we can access the title of the marker by calling item.variableName 
       super.onBeforeClusterItemRendered(item, markerOptions); 
      } 
} 

イムここでnoobの質問については本当に申し訳ありませんが、私はこれは私がやったように、そのうちの誰かが同じ質問を持っていることに役立ちます願っていますし、あなたの答えをありがとう! "SO"コミュニティは素晴らしいです!

0

onClusterItemRendered()メソッドをオーバーライドできませんか?

onBeforeClusterItemRendered()にマーカー名を保存して、すべてのクラスグローバル変数Stringに保存してonClusterItemRendered()のマーカー名をMarker.setTitle(yourSavedTitle);などで設定できると思います。


このような何か:

public class MapIconRender extends DefaultClusterRenderer<ClusterRequest> { 
    private String mMarkerTitle; 

    public MapIconRender(Context context, GoogleMap map, 
         ClusterManager<ClusterRequest> clusterManager) { 
     super(context, map, clusterManager); 
    } 

     @Override 
     protected void onBeforeClusterItemRendered (ClusterRequest item, MarkerOptions 
     markerOptions){ 
      markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.map_marker_outside_azure)); 
      markerOptions.snippet("Status: "); 
      markerOptions.title(stopName); //<------ THIS IS WHERE I WANT THE STOP NAME TO BE WHAT THE USER ENTERED. 
      mMarkerTitle = stopName; 
      super.onBeforeClusterItemRendered(item, markerOptions); 
     } 

     @Override 
     protected void onClusterItemRendered (ClusterRequest item, Marker marker){ 
      marker.setTitle(mMarkerTitle); 
     } 
} 
+0

あなたは何を言っているのですか?私がこのことを正しく考えていれば、私の問題は解決しませんでした(私は挑戦的にここで間違っている可能性があります)。しかし、私はまだadd_place.xmlからユーザーの入力からタイトルの名前を引っ張らないので、クラスのローカル変数であり、値のxmlには到達しません。繰り返しますが、私は間違っているかもしれませんが、あなたが例を挙げるなら、私はあなたの説明を多かれ少なかれ知っています。あなたは感謝のために努力してくれてありがとうございます! – rapid3642

関連する問題