如何操作OnCreate中的按钮获取Lat Lng

时间:2018-03-13 09:11:55

标签: java android google-maps android-studio

据我所知,这个问题已在多个版本中提出,而且品种很多,但我仍然无法获得Google Maps API的概念。

我在MAP片段上创建了一个按钮1,点击该按钮时,应该在我的蓝点位置上创建一个标记。但我无法在OnCreate()

中获取Lat Lng值
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        checkLocationPermission();
    }
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);


    button = (Button) findViewById(R.id.button1);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Toast.makeText(getApplicationContext(), "Button Clicked", Toast.LENGTH_SHORT).show();
        }
    });
}

onLocationChanged工作正常,它一直显示蓝点,因为它应该这样做 - 好吧我有一个问题就是保持点在中心 - 但这就是我的功课

@Override
public void onLocationChanged(Location location) {

    mLastLocation = location;


    if (mCurrLocationMarker != null) {
        mCurrLocationMarker.remove();
    }

    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());

    //Place current location marker
    //MarkerOptions markerOptions = new MarkerOptions();
    //markerOptions.position(latLng);
    //markerOptions.title("فتحت البرنامج هنا");
    //markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
    //mCurrLocationMarker = mMap.addMarker(markerOptions);

    //move map camera
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    mMap.animateCamera(CameraUpdateFactory.zoomTo(18));

    if (mGoogleApiClient != null) {
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
    }
}

任何帮助将不胜感激,我是Java Android世界的新手,并且尝试自学,不是这次: - )

1 个答案:

答案 0 :(得分:0)

也许这样的事情(如果我理解你的问题):

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        if (mCurrLocationMarker != null) {
            mCurrLocationMarker.remove();
        }

        if (mLastLocation != null) {
            LatLng latLng = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());

            //Place current location marker
            MarkerOptions markerOptions = new MarkerOptions();
            markerOptions.position(latLng);
            markerOptions.title("فتحت البرنامج هنا");
            markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
            mCurrLocationMarker = mMap.addMarker(markerOptions);
        }
    }
});
@Override
public void onLocationChanged(Location location) {

    mLastLocation = location;

    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());

    //move map camera
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    mMap.animateCamera(CameraUpdateFactory.zoomTo(18));

    if (mGoogleApiClient != null) {
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
    }
}