标记选项图标出错

时间:2014-08-17 17:27:10

标签: android eclipse google-maps

我想使用Google Maps V2获取用户的当前位置。我收到此错误,方法图标(bitmapdescriptor)未定义类型标记。

这是我基于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");
}

如果我用.setIcon替换.icon,则全新的MarkerOptions()为红色,我得到此错误,无法在基本类型void上调用title(String)。我该怎么办?

1 个答案:

答案 0 :(得分:2)

您在创建标记时放错了括号,因此方法icontitle无法识别。

将您的代码替换为:

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

这很明显,因为MarkerOptions对象没有setIcon方法,但Marker有一个方法。向地图添加MarkerOptions会返回Marker个对象: - )

为了澄清,您可以设置如下图标:

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

myMarker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));