我正在构建一个Android应用程序,用户可以使用Google地图选择所需的收藏位置。
为此,首先我使用camreaposition
函数处理模型以获取地图的中心点。其次,我写了一个代码。因此,当用户从屏幕上移除触摸时,该中心位置的Latlng应该是吐司。
通过实现两者,我的应用程序正在压缩列出的错误:
这是MainActivity的代码:
RelativeLayout rel = (RelativeLayout) findViewById(R.id.heyhello);
TouchableWrapper tWrapper = new TouchableWrapper(MainActivitytut.this, new IOnReleaseListenrer() {
@Override
public void onRelease() {
// TODO Auto-generated method stub
LatLng pos = googleMap.getCameraPosition().target;
String he = pos.toString();
Log.e("", "Pos: " + pos.toString());
googleMap.addMarker(new MarkerOptions().position(pos));
Log.e("TAG", googleMap.getCameraPosition().target.toString());
lat = googleMap.getCameraPosition().target.toString();
Toast.makeText(getApplicationContext(), he,
Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "hey",
Toast.LENGTH_LONG).show();
}
});
这是XML代码:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/heyhello"
xmlns:android="http://schemas.android.com/apk/res/android">
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
<TextView
android:id="@+id/locinfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="104dp"
android:layout_marginTop="113dp"
android:text="TextView" />
</RelativeLayout>
这是TouchableWrapper类代码:
public class TouchableWrapper extends RelativeLayout {
private IOnReleaseListenrer callback;
private View referenceView;
private boolean moveTookPlace=false;
private final int TOUCH_TOLERANCE = 30;
private float mX_TouchDown, mY_TouchDown, mX_TouchUp, mY_TouchUp;
public TouchableWrapper(Context context, final IOnReleaseListenrer callback) {
super(context);
this.callback = callback;
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
break;
case MotionEvent.ACTION_UP:
touch_up(x,y);
break;
}
return super.dispatchTouchEvent(event);
}
private void touch_start(float x, float y) {
mX_TouchDown = x;
mY_TouchDown = y;
}
private void touch_move(float x, float y) {
}
private void touch_up(float x , float y) {
float x_travelled = Math.abs(x-mX_TouchDown);
float y_travelled = Math.abs(y-mY_TouchDown);
if((x_travelled>TOUCH_TOLERANCE)||(y_travelled>TOUCH_TOLERANCE)){
callback.onRelease();
}
}
这是IOnReleaseListenrer代码:
public interface IOnReleaseListenrer {
public void onRelease();
}
logcat list-
的错误致命例外:MAIN JAVA.LANG.RUNTIMEEXCEPTION:无法启动 ACTIVITY COMPONENTINFO。 COM.AMAL.GOOGLEMAP / COM.AMAL.GOOGLEMAP.MAINACTIVITYTUT}:JAVA.LANG.I llegalStateException:指定的子级已有父级。您 必须首先在孩子的父母身上调用删除view()。在 android.app.ActivityThread.performLaunchActivity(ActivityThread - java:2196)at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2223) 在android.app.ActivityThread.access $ 600(ActivityThread.java:149)at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1309) 在android.os.Handler.dispatchMessage(Handler.java:99)at android.os.Looper.loop(Looper.java:153)