简体   繁体   English

当我单击按钮时重新转换(例如,从当前活动到自己的活动,如“意图”)

[英]Re-Transition when I click on button (e.g./i.e. Like Intent from current Activity to self Activity)

Basically I am working on Google Map in which I have Google Map Activity and user click on any place of Google Map I was adding marker on this click and I have one button on this button click I take this marker position and put it to my Firebase Database.My complete code was working, but the problem is that when I click on the button which takes marker latlang to Firebase, my latlang value successfully update and my map Activity is re-transited (eg/ie like Intent from current Activity to self Activity) that for my map was reloaded and I lose marker on screen. 基本上,我在Google Map上工作,在该地图上我拥有Google Map活动,并且用户在Google Map的任何位置单击,我为此单击添加了标记,并且在此按钮上单击了一个按钮,然后单击该标记位置并将其放置到Firebase中数据库。我的完整代码正在运行,但是问题是,当我单击将标记latlang带到Firebase的按钮时,我的latlang值成功更新,并且我的地图活动被重新传输(例如,例如,将Intent从当前Activity转换为self活动),该地图已重新加载,但我在屏幕上丢失了标记。

Here is my Java code: 这是我的Java代码:

import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.pm.PackageManager;
import android.location.Location;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.firebase.client.Firebase;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class PickUpActivity extends AppCompatActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, View.OnClickListener, GoogleMap.OnMapClickListener {
    private static final int PERMISSION_REQUEST_CODE = 1;
    public double marker_latitude;
    public double marker_longitude;
    private GoogleMap mMap;
    private GoogleApiClient googleApiClient;
    private Marker marker;
    private double latitude;
    private double longitude;
    private Button btn;
    private Bus bus;
    private Firebase ref;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pick_up);
        Firebase.setAndroidContext(this);
        btn = (Button) findViewById(R.id.btn_pick);
        ref = new Firebase(Config.FIREBASE_URL);
        bus = new Bus();
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
        mMap = mapFragment.getMap();
        googleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addApi(LocationServices.API)
                .build();

        btn.setOnClickListener(this);
        mMap.setOnMapClickListener(this);
    }


    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {
        getCurrentLocation();

    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    protected void onStart() {
        googleApiClient.connect();
        super.onStart();
    }

    @Override
    protected void onStop() {
        googleApiClient.disconnect();
        super.onStop();
    }

    private void getCurrentLocation() {
        if (!checkPermission()) {
            requestPermission();
        }

        Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
        if (location != null) {

            longitude = location.getLongitude();
            latitude = location.getLatitude();

            moveMap();
        }
    }

    private void moveMap() {
        LatLng latLng = new LatLng(latitude, longitude);
        mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        mMap.animateCamera(CameraUpdateFactory.zoomTo(15));
    }

    private boolean checkPermission() {
        int result = ContextCompat.checkSelfPermission(PickUpActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION);
        if (result == PackageManager.PERMISSION_GRANTED) {
            return true;
        } else {
            return false;
        }
    }

    private void requestPermission() {

        if (ActivityCompat.shouldShowRequestPermissionRationale(PickUpActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION)) {
            Toast.makeText(PickUpActivity.this, "GPS permission allows us to access location data. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();
        } else {
            ActivityCompat.requestPermissions(PickUpActivity.this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_REQUEST_CODE);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case PERMISSION_REQUEST_CODE:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    getCurrentLocation();
                } else {

                }
                break;
        }
    }

    @Override
    public void onClick(View v) {
        if (v == btn) {
            if (marker == null) {
                Toast.makeText(PickUpActivity.this, "Please Select Any Place", Toast.LENGTH_SHORT).show();
            } else {
                bus.setlatitude(marker_latitude);
                bus.setlongitude(marker_longitude);
                ref.child("school1").child("bus1").child("parents").child("parent01").child("pickuppoint").setValue(bus);
                Toast.makeText(PickUpActivity.this, "Pick Up Point Set", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public void onMapClick(LatLng latLng) {
        mMap.clear();
        marker = mMap.addMarker(new MarkerOptions()
                .position(latLng) //setting position
                .draggable(true) //Making the marker draggable
                .title("" + latLng));
        marker_latitude = latLng.latitude;
        marker_longitude = latLng.longitude;
    }
}

During analysis I found problem in below code: 在分析过程中,我在以下代码中发现了问题:

bus.setlatitude(marker_latitude);
bus.setlongitude(marker_longitude);
   ref.child("school1").child("bus1").child("parents").child("parent01").child("pickuppoint").setValue(bus);

If I put some static value on bus.setlatitude() and bus.setlongitude no re-transition occur. 如果我在bus.setlatitude()bus.setlongitude上放置一些静态值, bus.setlongitude不会发生重新转换。 I don't know what I am doing wrong and what is solution for this problem. 我不知道我在做什么错,什么是解决该问题的方法。

if your user click on your Map the map will be cleared: mMap.clear(); 如果您的用户单击您的地图,则地图将被清除: mMap.clear();

@Override
public void onMapClick(LatLng latLng) {

    mMap.addMarker(new MarkerOptions()
            .position(latLng) //setting position
            .draggable(true) //Making the marker draggable
            .title(""+latLng));
    marker_latitude = latLng.latitude;
    marker_longitude= latLng.longitude;

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何(即,什么意图动作)启动电子邮件应用程序的设置电子邮件帐户活动(添加新的电子邮件帐户活动) - How (i.e., what intent action) to start the set up email account activity (add new email account activity) of the email application 哪种方法是将大量数据(即对象)从一个Activity转移到Android中的另一个Activity? - Which is the best way to transfer bulk of data (i.e. objects) from one Activity to another Activity in Android? 如何将我的活动显示在Android联系人菜单中,例如facebook app? - How can I have my activity display in android contact menu, like i.e. facebook app? Android:如何检测Back按钮是否会退出应用程序(即这是堆栈中剩余的最后一个活动)? - Android: How can I detect if the Back button will exit the app (i.e. this is the last activity left on the stack)? 在Android应用程序中,'ListActivity'可以作为主要活动吗?即打开应用程序时可以启动“ListActivity”吗? - In an Android application can a 'ListActivity' be the main activity? i.e. can a 'ListActivity' start when I open an app? 当我单击后退按钮从当前活动 android 转到上一个活动时 - When I click back button goto previous activity from current activity android 检测从我的后台服务启动(任何)应用程序(即活动)的最佳方法? - Best way to detect (any) app(i.e. activity) launch from a my background service? 如何使用任何通知消息(例如“ abc”)开始活动 - how to start activity with any notification message e.g. “abc” 如何在 Android 上检查设备自然(默认)方向(即获取例如 Motorola Charm 或 Flipout 的横向) - How to check device natural (default) orientation on Android (i.e. get landscape for e.g., Motorola Charm or Flipout) 我想单击按钮将我的第一个活动的内容(即Java代码)复制到片段中 - I want to copy the content of my first activity (i.e the java code ) to my fragment on the click of a button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM