Sorry for the noob question but I did not come across any situations that would work for me. Im sure this is super simple, I just need to be pointed in the right direction.
Objective:
I want to be able to take the name of what the user specified and add it to the markers title.
What I know:
I am unable to inflate the xml and grab it via that way cause the method is being override and if I inflate to my knowledge will no longer allow me to override the method. How should I approach this?
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 :(得分: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);
}
}
答案 1 :(得分:0)
@SuhyeonLee不确定这是不是你的意思(请告诉我),但我确实在谷歌发布的定制群集演示的演示中找到了令人惊讶的答案。这是我学到的东西。通过调用MapIconRender.java类时
mClusterManager.setRenderer(new MapIconRender(getApplicationContext(), mMap, mClusterManager));
哪个将在MapIconRender.java类中运行此方法
protected void onBeforeClusterItemRendered (ClusterRequest item, MarkerOptions
markerOptions)
这意味着ClusterRequest类中的任何变量都可以在MapIconRender.java类中访问,并且可以通过(item.variableName)访问。我为没有发布文件ClusterRequest的代码而道歉,我认为不需要。我回去把这个片段添加到我原来的问题中。
无论如何,我需要添加的新代码如下:
在MapsActivity类中,我需要通过以下方式将ClusterRequest类的标记名称(或标记的标题)传递给:
ClusterRequest testCluster = new ClusterRequest(currentLatitude, currentLongitude, stopName); //stopName was added here.
在ClusterRequest类中,我需要通过以下方法添加标记名称(aka stopName)的“全局变量”:
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;
}
}
最后在我的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("Title: " + item.stopName); //Now we can access the title of the marker by calling item.variableName
super.onBeforeClusterItemRendered(item, markerOptions);
}
}
我真的很抱歉这里的菜鸟问题,但我希望这可以帮助那些可能和我一样有问题的人,谢谢你的答案! “SO”社区很棒!