PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label google-maps-markers. Show all posts
Showing posts with label google-maps-markers. Show all posts

Friday, October 28, 2022

[FIXED] How to check id String is Empty then put a define a default value if is Empty in android studio

 October 28, 2022     android, bearing, google-maps-markers, is-empty, java     No comments   

Issue

this is an taxi app where user get the rotation(bearing) of driver is working well exept with devices that has not the rotaion (bearing) like this Here is the image

if driver device doesn t have this feature then the users will get an error and the app crash

so my question is how to avoid this by check if Sting is Empty and if is empty we have to put a default value on it

this it my code

if (icondriver.equals("2")) {
                   driverMarkers.add(
                           gMap.addMarker(new MarkerOptions()
                                   .position(currentDriverPos)
                                   .icon(BitmapDescriptorFactory.fromResource(R.drawable.carmap))
                                   .anchor((float) 0.5, (float) 0.5)
---------error line------------->>  .rotation(Float.parseFloat(driver.getBearing()))
                                   .flat(true)
                           )

this is how to get the String value

public String getBearing() {
       return bearing;
   }

this is the error

enter image description here


Solution

This assumes that if the String is empty, 0 is passed as default blank value. Also assumes the String will never be null.

(...)
rotation(Float.parseFloat(driver.getBearing().trim().isEmpty()?"0":driver.getBearing()))
(...)

parseFloat will automatically trim() the String. But in order to really check if isEmpty is not giving false negatives, trimming the String guarantees no String with just whitespaces is passed as valid. String s = " "; will return false if called isEmpty() directly on it, without the trim() operation first.

So, the condition driver.getBearing().trim().isEmpty()?"0":driver.getBearing() is telling:

  • If the String is empty, pass "0" (or your desired default value).
  • If not, pass the String value as it is (driver.getBearing()).


Answered By - aran
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Monday, September 26, 2022

[FIXED] How to correct smooth moving the marker in google maps v2?

 September 26, 2022     android, google-maps, google-maps-markers, gps, java     No comments   

Issue

In my application needs to display a smooth "move" google maps marker from one point to another. I use the following method for the animation:

public void animateMarker(final Marker marker, final LatLng toPosition,
                          final boolean hideMarker) {
    final Handler handler = new Handler();
    final long start = SystemClock.uptimeMillis();
    Projection proj = mMap.getProjection();
    Point startPoint = proj.toScreenLocation(marker.getPosition());
    final LatLng startLatLng = proj.fromScreenLocation(startPoint);
    final long duration = 500;

    final Interpolator interpolator = new LinearInterpolator();

    handler.post(new Runnable() {
        @Override
        public void run() {
            long elapsed = SystemClock.uptimeMillis() - start;
            float t = interpolator.getInterpolation((float) elapsed
                    / duration);
            double lng = t * toPosition.longitude + (1 - t)
                    * startLatLng.longitude;
            double lat = t * toPosition.latitude + (1 - t)
                    * startLatLng.latitude;
            marker.setPosition(new LatLng(lat, lng));

            if (t < 1.0) {
                // Post again 16ms later.
                handler.postDelayed(this, 16);
            } else {
                if (hideMarker) {
                    marker.setVisible(false);
                } else {
                    marker.setVisible(true);
                }
            }
        }
    });
}

But as a result of simply creating a new marker to the new location (though old is not removed):

enter image description here


Solution

I copied some of the code from the project mentioned in the official video.

I tried to reproduce it with this code and this seems to be working for me, so hopefully my code would help you, even for a bit.

static final LatLng SomePos = new LatLng(37.7796354, -122.4159606);

    try {
        if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        }
        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        googleMap.setMyLocationEnabled(true);
        googleMap.setTrafficEnabled(false);
        googleMap.setIndoorEnabled(false);
        googleMap.setBuildingsEnabled(true);
        googleMap.getUiSettings().setZoomControlsEnabled(true);
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(SomePos));
        googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder()
                .target(googleMap.getCameraPosition().target)
                .zoom(17)
                .bearing(30)
                .tilt(45)
                .build()));

        myMarker = googleMap.addMarker(new MarkerOptions()
                .position(SomePos)
                .icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher))
                .title("Hello world"));


        googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener()
        {
            @Override
            public boolean onMarkerClick(Marker arg0) {

                final LatLng startPosition = myMarker.getPosition();
                final LatLng finalPosition = new LatLng(37.7801569,-122.4148528);
                final Handler handler = new Handler();
                final long start = SystemClock.uptimeMillis();
                final Interpolator interpolator = new AccelerateDecelerateInterpolator();
                final float durationInMs = 3000;
                final boolean hideMarker = false;

                handler.post(new Runnable() {
                    long elapsed;
                    float t;
                    float v;

                    @Override
                    public void run() {
                        // Calculate progress using interpolator
                        elapsed = SystemClock.uptimeMillis() - start;
                        t = elapsed / durationInMs;
                        v = interpolator.getInterpolation(t);

                        LatLng currentPosition = new LatLng(
                                startPosition.latitude*(1-t)+finalPosition.latitude*t,
                                startPosition.longitude*(1-t)+finalPosition.longitude*t);

                        myMarker.setPosition(currentPosition);

                        // Repeat till progress is complete.
                        if (t < 1) {
                            // Post again 16ms later.
                            handler.postDelayed(this, 16);
                        } else {
                            if (hideMarker) {
                                myMarker.setVisible(false);
                            } else {
                                myMarker.setVisible(true);
                            }
                        }
                    }
                });

                return true;

            }

        });

    } catch (Exception e) {
        e.printStackTrace();
    }


Answered By - kaho
Answer Checked By - Timothy Miller (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing