0

I want to get the current location of the user by using Google Maps V2. I get this error, the method icon(bitmapdescriptor) is undefined for the type marker.

This is my code based on android + google map api v2 + current location

import android.app.Dialog;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Getting Google Play availability status
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
    // Showing status
    if(status!=ConnectionResult.SUCCESS){ // Google Play Services not available
        int requestCode = 10;
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
        dialog.show();
    }
    else{ // Google Play Services are available
        // Getting reference to the SupportMapFragment of activity_main.xml
        SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        // Getting Google Map object from the fragment
        GoogleMap googleMap = fm.getMap();
        // Enabling MyLocation Layer of Google
        googleMap.setMyLocationEnabled(true);
        // Getting LocationManager object from System Service LOCATION_SERVICE
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        // Creating a criteria object to retrieve provider
        Criteria criteria = new Criteria();
        // Getting the name of the best provider
        String provider = locationManager.getBestProvider(criteria, true);
        // Getting current location
        Location location = locationManager.getLastKnownLocation(provider);
        LocationListener locationListener = new LocationListener(){
            public void onLocationChanged(Location location){
                // Redraw the marker when get location update
                drawMarker(location);
            }

            @Override
            public void onProviderDisabled(String provider) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProviderEnabled(String provider) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onStatusChanged(String provider, int status,
                    Bundle extras) {
                // TODO Auto-generated method stub

            }
        };
        if(location!=null){
            // Place the initial marker
            drawMarker(location);
        }
        locationManager.requestLocationUpdates(provider, 0, 0, locationListener);
    }
}

private void drawMarker(Location location){
    SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    GoogleMap googleMap = fm.getMap();
    googleMap.clear();
    LatLng currentPosition = new LatLng(location.getLatitude(),location.getLongitude());
    googleMap.addMarker(new MarkerOptions()
    .position(currentPosition)
    .snippet("Lat:" + location.getLatitude() + "Lng" + location.getLongitude()))
    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
    .title("ME");
}

And if i replace .icon with .setIcon, the whole new MarkerOptions() is in red and I get this error, Cannot invoke title(String) on the primitive type void. What should I do?

Community
  • 1
  • 1

1 Answers1

2

You have misplaced a parenthesis when creating your marker, therefore methods icon and title are unrecognized.

Replace your code to:

mMap.addMarker(new MarkerOptions()
        .position(currentPosition)
        .snippet("Lat:" + location.getLatitude() + "Lng" + location.getLongitude())
        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
        .title("ME"));

This was pretty obvious as MarkerOptions object doesn't have a setIcon method, but a Marker has one. And adding a MarkerOptions to the map returns a Marker object :-)

Just for clarification, you could set the icon like this:

Marker myMarker = mMap.addMarker(new MarkerOptions()
        .position(currentPosition)
        .snippet("Lat:" + location.getLatitude() + "Lng" + location.getLongitude())
        .title("ME"));

myMarker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
Simas
  • 43,548
  • 10
  • 88
  • 116
  • Hi @user3249477! Your answer actually worked! I'm so very thankful for your time to answer. I'm just knew to Android Application and I'm working on our thesis. So again, thank you very much! – Ralph Anthony Magante Aug 18 '14 at 14:18