如何从显示 Google 地图的列表视图中打开 activity?
How to open a activity from a list view which shows Google map?
我是构建 android 应用程序的新手,我一直在尝试从列表中的一行中打开一个新的 activity,其中每个 activity 显示 google每行的地图。到目前为止,我已经设法通过单击该行打开了一个新的 activity,但我不知道如何在它们上面插入 Google 地图。我已经下载了 Google api 密钥并将其保存在我的 AndroidManifest 中。
我的代码看起来像这样打开列表:
package com.MaheshGurung.androidapp;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class secondActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// storing string resources into Array
String []Buildings = getResources().getStringArray(R.array.Buildings);
// Binding resources Array to ListAdapter
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.screen1, R.id.listbuilding, Buildings));
}
protected void onListItemClick(ListView lv, View v, int position, long id){
super.onListItemClick(lv, v, position, id);
Intent intent = new Intent();
intent.setClass(this, ListSelect.class);
intent.putExtra("position", position);
// Or / And
intent.putExtra("id", id);
startActivity(intent);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
并且为每一行打开新 activity 的代码如下所示:
package com.MaheshGurung.androidapp;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class secondActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// storing string resources into Array
String []Buildings = getResources().getStringArray(R.array.Buildings);
// Binding resources Array to ListAdapter
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.screen1, R.id.listbuilding, Buildings));
}
protected void onListItemClick(ListView lv, View v, int position, long id){
super.onListItemClick(lv, v, position, id);
Intent intent = new Intent();
intent.setClass(this, ListSelect.class);
intent.putExtra("position", position);
// Or / And
intent.putExtra("id", id);
startActivity(intent);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
和 .xml 文件看起来像
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/my_textview"/>
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_below="@+id/my_textview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
知道在哪里为 google 地图或任何其他建议的方法编写代码会很有帮助。
你需要做的是两件事:
- 当你点击ListView项时,它需要传递信息给第二个activity。
- 第二个activity需要接收指令,并加载合适的地图。
顺便说一句,如果您希望列表与地图在同一屏幕上(例如,对于平板电脑),使用 Fragments 可能会更好,那么它会更容易。
对于第一个,那里有很多资源。我会向您指出这个问题:How do I pass data between Activities in Android application?。我明白了,您已经有了类似的代码。我建议不要传递位置,而是传递 lat/lng/zoom level 或类似的东西,这样地图就不需要弄清楚任何东西。这使得它更容易在其他组件中重用。
对于第二个,您应该参考 Google Maps API,其中显示了如何更改视图。实际上,您想要做的是更改相机位置,他们提供了以下示例代码。只需在第二个 activity 中接收 lat/lng 边界,获取 API 中指定的 mMap 引用,然后将其传递类似于下面的管理方式。
private GoogleMap mMap;
// Create a LatLngBounds that includes Australia.
private LatLngBounds AUSTRALIA = new LatLngBounds(
new LatLng(-44, 113), new LatLng(-10, 154));
// Set the camera to the greatest possible zoom level that includes the
// bounds
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(AUSTRALIA, 0));
我是构建 android 应用程序的新手,我一直在尝试从列表中的一行中打开一个新的 activity,其中每个 activity 显示 google每行的地图。到目前为止,我已经设法通过单击该行打开了一个新的 activity,但我不知道如何在它们上面插入 Google 地图。我已经下载了 Google api 密钥并将其保存在我的 AndroidManifest 中。
我的代码看起来像这样打开列表:
package com.MaheshGurung.androidapp;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class secondActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// storing string resources into Array
String []Buildings = getResources().getStringArray(R.array.Buildings);
// Binding resources Array to ListAdapter
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.screen1, R.id.listbuilding, Buildings));
}
protected void onListItemClick(ListView lv, View v, int position, long id){
super.onListItemClick(lv, v, position, id);
Intent intent = new Intent();
intent.setClass(this, ListSelect.class);
intent.putExtra("position", position);
// Or / And
intent.putExtra("id", id);
startActivity(intent);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
并且为每一行打开新 activity 的代码如下所示:
package com.MaheshGurung.androidapp;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class secondActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// storing string resources into Array
String []Buildings = getResources().getStringArray(R.array.Buildings);
// Binding resources Array to ListAdapter
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.screen1, R.id.listbuilding, Buildings));
}
protected void onListItemClick(ListView lv, View v, int position, long id){
super.onListItemClick(lv, v, position, id);
Intent intent = new Intent();
intent.setClass(this, ListSelect.class);
intent.putExtra("position", position);
// Or / And
intent.putExtra("id", id);
startActivity(intent);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
和 .xml 文件看起来像
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/my_textview"/>
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_below="@+id/my_textview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
知道在哪里为 google 地图或任何其他建议的方法编写代码会很有帮助。
你需要做的是两件事:
- 当你点击ListView项时,它需要传递信息给第二个activity。
- 第二个activity需要接收指令,并加载合适的地图。
顺便说一句,如果您希望列表与地图在同一屏幕上(例如,对于平板电脑),使用 Fragments 可能会更好,那么它会更容易。
对于第一个,那里有很多资源。我会向您指出这个问题:How do I pass data between Activities in Android application?。我明白了,您已经有了类似的代码。我建议不要传递位置,而是传递 lat/lng/zoom level 或类似的东西,这样地图就不需要弄清楚任何东西。这使得它更容易在其他组件中重用。
对于第二个,您应该参考 Google Maps API,其中显示了如何更改视图。实际上,您想要做的是更改相机位置,他们提供了以下示例代码。只需在第二个 activity 中接收 lat/lng 边界,获取 API 中指定的 mMap 引用,然后将其传递类似于下面的管理方式。
private GoogleMap mMap;
// Create a LatLngBounds that includes Australia.
private LatLngBounds AUSTRALIA = new LatLngBounds(
new LatLng(-44, 113), new LatLng(-10, 154));
// Set the camera to the greatest possible zoom level that includes the
// bounds
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(AUSTRALIA, 0));