将 TextView 更改为位置名称
Change TextView to name of location
我想将 TextView 的文本更改为标记指示的位置的名称。我遇到的问题是我必须从另一个 java 文件中调用这个名称,但这似乎不起作用。
在 PlaceInfo.java 文件中:
PlaceInfo 文件获取标记位置的名称(您搜索的位置)。这是通过 Google 地图 API.
完成的
public class PlaceInfo {
private String address;
private LatLng latLng;
private String name;
public PlaceInfo(String name, String address, LatLng latLng) {
this.name = name;
this.address = address;
this.latLng = latLng;
}
public PlaceInfo() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public LatLng getLatLng() {
return latLng;
}
public void setLatLng(LatLng latLng) {
this.latLng = latLng;
}
@Override
public String toString() {
return "PlaceInfo{" +
"address='" + address + '\'' +
", latLng=" + latLng +
", name='" + name + '\'' +
'}';
}
}
并在 Activity 中使用文本视图。问题来了,该改的
public class MarkerActivity extends Activity {
private PlaceInfo mPlace;
private TextView textElement;
private Context context;
ImageView imageView;
Random r;
Integer[] images = {
R.drawable.markerphoto1,
R.drawable.markerphoto2,
R.drawable.markerphoto3,
R.drawable.markerphoto4
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_marker);
context = this;
//random image on page
imageView = (ImageView) findViewById(R.id.imageView);
r = new Random();
imageView.setImageResource(images[r.nextInt(images.length)]);
//set font for location_title
TextView textView = (TextView) findViewById(location_title);
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/Lato-Regular.ttf");
textView.setTypeface(typeface);
//set location_title to name of location
mPlace = new PlaceInfo();
textElement = (TextView) findViewById(R.id.location_title);
textElement.setText(String.valueOf(mPlace.getName()));
}
}
现在是回调代码,位于地图Activity。
private ResultCallback<PlaceBuffer> mUpdatePlaceDetailsCallback = new ResultCallback<PlaceBuffer>() {
@Override
public void onResult(@NonNull PlaceBuffer places) {
if (!places.getStatus().isSuccess()){
places.release();
return;
}
final Place place = places.get(0);
mPlace = new PlaceInfo();
mPlace.setAddress(place.getAddress().toString());
mPlace.setLatLng(place.getLatLng());
mPlace.setName(place.getName().toString());
moveCamera(mPlace.getLatLng(), DEFAULT_ZOOM);
}
};
希望你能帮帮我。谢谢
您应该使用 PlaceInfo
class
到 getName
的对象。但是你正在调用 class
对象。
它应该像下面这样
textElement.setText(placeInfo.getName());
String address;
private ResultCallback<PlaceBuffer> mUpdatePlaceDetailsCallback = new ResultCallback<PlaceBuffer>() {
@Override
public void onResult(@NonNull PlaceBuffer places) {
if (!places.getStatus().isSuccess()){
places.release();
return;
}
final Place place = places.get(0);
mPlace = new PlaceInfo();
mPlace.setAddress(place.getAddress().toString());
mPlace.setLatLng(place.getLatLng());
mPlace.setName(place.getName().toString());
address = place.getAddress().toString();
moveCamera(mPlace.getLatLng(), DEFAULT_ZOOM);
}
并且在MarkerActivity
textElement.setText(getIntent().getStringExtra("address"));
我想将 TextView 的文本更改为标记指示的位置的名称。我遇到的问题是我必须从另一个 java 文件中调用这个名称,但这似乎不起作用。
在 PlaceInfo.java 文件中: PlaceInfo 文件获取标记位置的名称(您搜索的位置)。这是通过 Google 地图 API.
完成的public class PlaceInfo {
private String address;
private LatLng latLng;
private String name;
public PlaceInfo(String name, String address, LatLng latLng) {
this.name = name;
this.address = address;
this.latLng = latLng;
}
public PlaceInfo() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public LatLng getLatLng() {
return latLng;
}
public void setLatLng(LatLng latLng) {
this.latLng = latLng;
}
@Override
public String toString() {
return "PlaceInfo{" +
"address='" + address + '\'' +
", latLng=" + latLng +
", name='" + name + '\'' +
'}';
}
}
并在 Activity 中使用文本视图。问题来了,该改的
public class MarkerActivity extends Activity {
private PlaceInfo mPlace;
private TextView textElement;
private Context context;
ImageView imageView;
Random r;
Integer[] images = {
R.drawable.markerphoto1,
R.drawable.markerphoto2,
R.drawable.markerphoto3,
R.drawable.markerphoto4
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_marker);
context = this;
//random image on page
imageView = (ImageView) findViewById(R.id.imageView);
r = new Random();
imageView.setImageResource(images[r.nextInt(images.length)]);
//set font for location_title
TextView textView = (TextView) findViewById(location_title);
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/Lato-Regular.ttf");
textView.setTypeface(typeface);
//set location_title to name of location
mPlace = new PlaceInfo();
textElement = (TextView) findViewById(R.id.location_title);
textElement.setText(String.valueOf(mPlace.getName()));
}
}
现在是回调代码,位于地图Activity。
private ResultCallback<PlaceBuffer> mUpdatePlaceDetailsCallback = new ResultCallback<PlaceBuffer>() {
@Override
public void onResult(@NonNull PlaceBuffer places) {
if (!places.getStatus().isSuccess()){
places.release();
return;
}
final Place place = places.get(0);
mPlace = new PlaceInfo();
mPlace.setAddress(place.getAddress().toString());
mPlace.setLatLng(place.getLatLng());
mPlace.setName(place.getName().toString());
moveCamera(mPlace.getLatLng(), DEFAULT_ZOOM);
}
};
希望你能帮帮我。谢谢
您应该使用 PlaceInfo
class
到 getName
的对象。但是你正在调用 class
对象。
它应该像下面这样
textElement.setText(placeInfo.getName());
String address;
private ResultCallback<PlaceBuffer> mUpdatePlaceDetailsCallback = new ResultCallback<PlaceBuffer>() {
@Override
public void onResult(@NonNull PlaceBuffer places) {
if (!places.getStatus().isSuccess()){
places.release();
return;
}
final Place place = places.get(0);
mPlace = new PlaceInfo();
mPlace.setAddress(place.getAddress().toString());
mPlace.setLatLng(place.getLatLng());
mPlace.setName(place.getName().toString());
address = place.getAddress().toString();
moveCamera(mPlace.getLatLng(), DEFAULT_ZOOM);
}
并且在MarkerActivity
textElement.setText(getIntent().getStringExtra("address"));