Vikram Sinha
Vikram Sinha

Reputation: 609

Marker pin bouncing to another location on map after zoom (not on my latitude or longitude)

This is my code in eclipse and when I zoom on my map and tap on my pin (Marker) it doesn't direct to my latitude and longitude, instead it moves to different location.

MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.flat(true);
        markerOptions.anchor(0.5f, 0.5f);
        markerOptions.icon(BitmapDescriptorFactory
                .fromResource(R.drawable.pin_client_org));
        markerOptions.title(getResources().getString(
                R.string.text_source_pin_title));
        markerOptions.position(
                new LatLng(latLng.latitude,
                        latLng.longitude));
        markerSource = map.addMarker(markerOptions);


private void setMarker(LatLng latLng, boolean isSource) {
        if (!MapFragment.this.isVisible())
            return;
        if (getActivity() != null && getActivity().getCurrentFocus() != null) {
            // inputMethodManager.hideSoftInputFromWindow(getActivity()
            // .getCurrentFocus().getWindowToken(), 0);
            activity.hideKeyboard();
        }

        if (latLng != null && map != null) {
            if (isSource) {
                if (markerSource == null) {
                    MarkerOptions markerOptions = new MarkerOptions();
                    markerOptions.flat(true);
                    markerOptions.anchor(0.5f, 0.5f);
                    markerOptions.icon(BitmapDescriptorFactory
                            .fromResource(R.drawable.pin_client_org));
                    markerOptions.title(getResources().getString(
                            R.string.text_source_pin_title));
                    markerOptions.position(
                            new LatLng(latLng.latitude,
                                    latLng.longitude));
                    markerSource = map.addMarker(markerOptions);

                    //markerSource.setDraggable(false);
                } else {
                    markerSource.setPosition(latLng);
                }
                CameraUpdateFactory.newLatLng(latLng);

            } else {
                if (markerDestination == null) {
                    MarkerOptions opt = new MarkerOptions();
                    opt.position(latLng);
                    opt.title(getResources().getString(
                            R.string.text_destination_pin_title));
                    opt.icon(BitmapDescriptorFactory
                            .fromResource(R.drawable.destination_pin));
                    markerDestination = map.addMarker(opt);

                    markerDestination.setDraggable(true);

                    if (markerSource != null) {
                        LatLngBounds.Builder bld = new LatLngBounds.Builder();

                        bld.include(new LatLng(
                                markerSource.getPosition().latitude,
                                markerSource.getPosition().longitude));
                        bld.include(new LatLng(
                                markerDestination.getPosition().latitude,
                                markerDestination.getPosition().longitude));
                        //LatLngBounds latLngBounds = bld.build();
                        //map.moveCamera(CameraUpdateFactory.newLatLngBounds(
                        //  latLngBounds, 30));
                    } else {
                        CameraUpdateFactory.newLatLng(latLng);
                    }

                } else {
                    markerDestination.setPosition(latLng);
                }
            }

            getAddressFromLocation(markerSource.getPosition(), etSource);
        } else {
            Toast.makeText(getActivity(), getResources().getString(
                            R.string.unable_location),
                    Toast.LENGTH_LONG).show();
        }
    }

    super.onActivityCreated(savedInstanceState);
    // activity.layoutDestination.setVisibility(View.VISIBLE);
    // activity.tvTitle.setVisibility(View.GONE);

    activity.btnNotification.setVisibility(View.VISIBLE);
    activity.tvTitle.setVisibility(View.VISIBLE);
    activity.setIcon(R.drawable.fare_info);
    activity.setTitle(getString(R.string.text_make_request));
    activity.btnNotification.setOnClickListener(this);
    // etSource = activity.etSource;
    // activity.imgClearDst.setOnClickListener(this);
    adapter=new PlacesAutoCompleteAdapter(activity, R.layout.autocomplete_list_text);

    adapterDestination=new PlacesAutoCompleteAdapter(activity, R.layout.autocomplete_list_text);

    etSource.setAdapter(adapter);
    locHelper=new LocationHelper(activity);

    locHelper.setLocationReceivedLister(this);
    etDestination.setAdapter(adapterDestination);
    etSource.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick (View v){
            etSource.setText("");
            layoutMarker.setVisibility(View.GONE);
            if (sendReqLayout.getVisibility() == View.VISIBLE) {
                cancelConfirmation();
            }
        }
    });

    etSource.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange (View v,boolean hasFocus){
            // TODO Auto-generated method stub
            if (hasFocus) {
                etSource.setText("");
                layoutMarker.setVisibility(View.GONE);
                if (sendReqLayout.getVisibility() == View.VISIBLE) {
                    cancelConfirmation();
                }
            }
        }
    });

    etSource.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick (AdapterView < ? > arg0, View arg1,int arg2, long arg3){
            final String selectedDestPlace = adapter.getItem(arg2);

            new Thread(new Runnable() {
                @Override
                public void run() {
                    final LatLng latlng = getLocationFromAddress(selectedDestPlace);
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            isMapTouched = true;
                            curretLatLng = latlng;
                            isSource = true;
                            map.clear(); //25-02-2016
                            setMarker(curretLatLng, isSource);
                            //setMarkerOnRoad(curretLatLng, curretLatLng);
                            animateCameraToMarker(curretLatLng, true);
                            stopUpdateProvidersLoaction();
                            getAllProviders(curretLatLng);

                        }
                    })
                }
            })
        });
    });

This is what my code is. Please help me to understand this vague problem.

Upvotes: 1

Views: 125

Answers (1)

user8073782
user8073782

Reputation:

The issue with map is when ever you scroll the map it changes the location and as soon as you tap anywhere on the map your tap doesn't responds where it should be so you just have manage to stop your map being scroll.

that should solve your problem.

Upvotes: 1

Related Questions