【发布时间】:2016-05-30 03:38:23
【问题描述】:
我正在使用以下代码通过Google Places API for Android 在我的应用程序上显示地图:
public void showMap(View view){
int PLACE_PICKER_REQUEST = 1;
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
try {
startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}
布局:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="30"
android:orientation="horizontal"
android:weightSum="100">
<TextView
android:id="@+id/locationLabel"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
android:layout_weight="75"
android:fontFamily="sans-serif-condensed"
android:textSize="45dp"
/>
<ImageView
android:id="@+id/taskLocation"
android:layout_width="0dp"
android:layout_margin="5dp"
android:layout_height="wrap_content"
android:layout_weight="25"
android:src="@drawable/addlocation"
android:onClick="showMap"/>
</LinearLayout>
在图片点击事件时调用上述方法。
代码取自https://developers.google.com/places/android-api/placepicker
上述代码一执行,屏幕上就会显示一张地图。但是,地图会立即消失。
可能是什么问题?
编辑:
完整的活动代码:
public class TaskGenerator extends FragmentActivity {
private TaskDataSource dataSource = null;
private String dayName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("TaskGenerator: in onCreate");
Intent intent=getIntent();
setContentView(R.layout.task_generator);
dayName=intent.getCharSequenceExtra("DAY_NAME").toString();
}
public static class TimePickerFragment extends DialogFragment
implements TimePickerDialog.OnTimeSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
TaskGenerator taskGenerator=(TaskGenerator)getActivity();
taskGenerator.updateTimerLabel(hourOfDay, minute, view.is24HourView());
//dismiss();
}
}
private void updateTimerLabel(int hourOfDay, int minute, boolean is24Hour){
TextView textView=(TextView)findViewById(R.id.timerlabel);
textView.setTextSize(45);
if(hourOfDay<=12) {
textView.setText(hourOfDay + ":" + minute+" AM");
}else{
textView.setText(hourOfDay + ":" + minute+" PM");
}
}
public void showTimePickerDialog(View v) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getFragmentManager(), "timePicker");
}
public void saveTask(View view){
EditText taskTitle = (EditText) findViewById(R.id.taskTitle);
EditText taskDescription = (EditText) findViewById(R.id.taskDescription);
TextView taskTimer=(TextView) findViewById(R.id.timerlabel);
int []dayId= {R.id.sundayToggle, R.id.mondayToggle, R.id.tuesdayToggle, R.id.wednesdayToggle, R.id.thursdayToggle, R.id.fridayToggle, R.id.saturdayToggle};
Map<String, String> dayMap = new HashMap<>();
dayMap.put("MON","Monday");
dayMap.put("TUE","Tuesday");
dayMap.put("WED","Wednesday");
dayMap.put("THURS","Thursday");
dayMap.put("FRI","Friday");
dayMap.put("SAT","Saturday");
dayMap.put("SUN","Sunday");
dataSource = new TaskDataSource(this);
try{
dataSource.open();
}catch(SQLException e){
e.printStackTrace();
}
for(int i=0;i<7;i++){
ToggleButton dayToggle = (ToggleButton)findViewById(dayId[i]);
System.out.println(taskTitle.getText()+" "+taskDescription.getText()+" "+taskTimer.getText()+" "+dayToggle.isChecked()+" "+dayToggle.getTextOn());
if(dayToggle.isChecked()){
Task task = dataSource.createTask(dayMap.get(dayToggle.getTextOn().toString()), taskTitle.getText().toString(),
taskDescription.getText().toString(), taskTimer.getText().toString());
System.out.println(task);
}
}
dataSource.close();
Intent intent = new Intent(this, TaskActivity.class);
intent.putExtra("DAY_NAME",dayName);
startActivity(intent);
finish();
}
public void showMap(View view){
int PLACE_PICKER_REQUEST = 1;
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
try {
startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}
【问题讨论】:
-
发布整个代码。不只是一个sn-p。负责此行为的相关代码可能在其他地方。
-
我也是指 Java 代码。不仅仅是 XML。
-
@IceMAN 出了什么问题?
-
您确定您发布了正确的代码吗?我看不到任何与地图有关的东西。在 XML 和 Java 中都没有。
-
@IceMAN 请检查此链接developers.google.com/places/android-api/placepicker。只需要 3 行代码即可显示地图(地点选择器)。
标签: android google-maps google-places-api