如何在同一布局上显示 google 地图和标记?
How to show google map and marker on the same layout?
目标
单击显示地图按钮后,我想启动一个新的 activity(我称之为 MapActivity),其中需要显示 google 地图和标记。
我在哪里
单击 "Show Map" 按钮后,将开始使用地图和标记创建新的 activity,但两种布局都会叠加。
MainActivity.class
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = (Button) findViewById(R.id.find);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Spinner fromSpinner = (Spinner) findViewById(R.id.from);
Spinner toSpinner = (Spinner) findViewById(R.id.to);
Location fromLocation = (Location) fromSpinner.getSelectedItem();
Location toLocation = (Location) toSpinner.getSelectedItem();
new ShortestDistanceTask(fromLocation, toLocation).execute();
}
});
}
public void onShowMap(View v){
if (v.getId() == R.id.showMap){
Intent i = new Intent(MainActivity.this, MapActivity.class);
startActivity(i);
}
}
MapActivity.class
public class MapActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_activity);
FragmentManager FM = getFragmentManager();
FragmentTransaction FT = FM.beginTransaction();
Marker_Activity MA = new Marker_Activity();
FT.add(R.id.maplayout ,MA);
FT.commit();
}
}
Marker_Activity.class
public class Marker_Activity extends Fragment {
GoogleMap googleMap;
private static final LatLng Echostar = new LatLng(10,20);
public void processMap(View v){
if(googleMap == null){
googleMap = ((MapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();
}
if(googleMap != null){
googleMap.addMarker(new MarkerOptions().position(Echostar).title("This is the marked area"));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(Echostar,100));
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.map_activity, container, false);
processMap(v);
return v;
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="innoday.echostar.com.echopath.MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:weightSum="1">
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/from"
android:spinnerMode="dropdown" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/to"
android:layout_gravity="center_horizontal"
android:spinnerMode="dropdown" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Find"
android:id="@+id/find"
android:layout_gravity="center_horizontal" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Map"
android:id="@+id/showMap"
android:layout_gravity="center_horizontal"
android:onClick="onShowMap" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.23"
android:orientation="vertical"
android:id="@+id/table">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
</TableRow>
</TableLayout>
</LinearLayout>
</RelativeLayout>
map_activity.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="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="@+id/maplayout" />
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:name="com.google.android.gms.maps.MapFragment"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
你应该有一个 google_maps.xml 文件
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/mapviewfragment"/>
其中 mapviewfragment.xml 是
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/googlemap" />
</LinearLayout>
</LinearLayout>
这样您的 google 地图就可以占据整个屏幕并且可以在上面显示标记。
您的 MapActivity 应该实现
GoogleApiClient.ConnectionCallbacks
,
GoogleApiClient.OnConnectionFailedListener
,
OnMapReadyCallback
、
LocationListener
并至少覆盖
onMapReady()
、
onLocationChanged()
、
onConnected()
方法。
目标
单击显示地图按钮后,我想启动一个新的 activity(我称之为 MapActivity),其中需要显示 google 地图和标记。
我在哪里
单击 "Show Map" 按钮后,将开始使用地图和标记创建新的 activity,但两种布局都会叠加。
MainActivity.class
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = (Button) findViewById(R.id.find);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Spinner fromSpinner = (Spinner) findViewById(R.id.from);
Spinner toSpinner = (Spinner) findViewById(R.id.to);
Location fromLocation = (Location) fromSpinner.getSelectedItem();
Location toLocation = (Location) toSpinner.getSelectedItem();
new ShortestDistanceTask(fromLocation, toLocation).execute();
}
});
}
public void onShowMap(View v){
if (v.getId() == R.id.showMap){
Intent i = new Intent(MainActivity.this, MapActivity.class);
startActivity(i);
}
}
MapActivity.class
public class MapActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_activity);
FragmentManager FM = getFragmentManager();
FragmentTransaction FT = FM.beginTransaction();
Marker_Activity MA = new Marker_Activity();
FT.add(R.id.maplayout ,MA);
FT.commit();
}
}
Marker_Activity.class
public class Marker_Activity extends Fragment {
GoogleMap googleMap;
private static final LatLng Echostar = new LatLng(10,20);
public void processMap(View v){
if(googleMap == null){
googleMap = ((MapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();
}
if(googleMap != null){
googleMap.addMarker(new MarkerOptions().position(Echostar).title("This is the marked area"));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(Echostar,100));
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.map_activity, container, false);
processMap(v);
return v;
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="innoday.echostar.com.echopath.MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:weightSum="1">
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/from"
android:spinnerMode="dropdown" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/to"
android:layout_gravity="center_horizontal"
android:spinnerMode="dropdown" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Find"
android:id="@+id/find"
android:layout_gravity="center_horizontal" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Map"
android:id="@+id/showMap"
android:layout_gravity="center_horizontal"
android:onClick="onShowMap" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.23"
android:orientation="vertical"
android:id="@+id/table">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
</TableRow>
</TableLayout>
</LinearLayout>
</RelativeLayout>
map_activity.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="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="@+id/maplayout" />
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:name="com.google.android.gms.maps.MapFragment"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
你应该有一个 google_maps.xml 文件
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/mapviewfragment"/>
其中 mapviewfragment.xml 是
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/googlemap" />
</LinearLayout>
</LinearLayout>
这样您的 google 地图就可以占据整个屏幕并且可以在上面显示标记。
您的 MapActivity 应该实现
GoogleApiClient.ConnectionCallbacks
,
GoogleApiClient.OnConnectionFailedListener
,
OnMapReadyCallback
、
LocationListener
并至少覆盖
onMapReady()
、
onLocationChanged()
、
onConnected()
方法。