执行此操作"定位"按钮,我希望我的地图从MainActivity.java中的btnShowLoc函数获取输入值并显示相同的内容。
activity_main.xml中
<Button
android:onClick="btnShowLoc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Locate" />
<fragment
android:id="@+id/map"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
class="com.google.android.gms.maps.MapFragment"
android:layout_width="350dp"
android:layout_height="230dp"
tools:context=".MapLocationActivity"
tools:layout="@layout/activity_main" />
MainActivity.java
public void btnShowLoc(View view) {
EditText txtLat = (EditText) findViewById(R.id.txtLat);
EditText txtLong = (EditText) findViewById(R.id.txtLong);
//Need instruction about how to call MapActivity.java
}
MapActivity.java
public class MapActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MapFragment map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map));
map.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng Device = new LatLng(28.7041, 77.1025);
mMap.addMarker(new MarkerOptions().position(Device).title("Device Location"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(Device));
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
mMap.setTrafficEnabled(true);
mMap.setIndoorEnabled(true);
mMap.setBuildingsEnabled(true);
mMap.getUiSettings().setZoomControlsEnabled(true);
}
答案 0 :(得分:1)
使用 Intent
开始 MapActivity
Intent 是要执行的操作的抽象描述。它可以与startActivity一起使用来启动Activity,broadcastIntent可以将它发送到任何感兴趣的BroadcastReceiver组件,startService(Intent)或bindService(Intent,ServiceConnection,int)可以与后台服务进行通信。
尝试此操作首先使用Intent将您的纬度和纬度发送到 MapActivity
,如下面的代码
Intent i = new Intent(HotelDetailsClass.this, MapActivity.class);
i.putExtra("latitude","28.7041")
i.putExtra("longitude","77.1025");
startActivity(i);
现在在 MapActivity
中添加此代码Double latitude, longitude;
Intent i = getIntent();
latitude = Double.valueOf(i.getStringExtra("latitude"));
longitude = Double.valueOf(i.getStringExtra("longitude"));
现在加载地图
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Location and move the camera
LatLng location = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(location).title(title));
mMap.moveCamera(CameraUpdateFactory.newLatLng(location));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 14.0f));
}