MapQuest Maps Android SDK

Markers

Put a Marker on the Map

Android with Map and Marker
public class MainActivity extends Activity {
    private final LatLng MAPQUEST_HEADQUARTERS_LOCATION = new LatLng(39.750307, -104.999472);
    private MapView mMapView;
    private MapboxMap mMapboxMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MapQuest.start(getApplicationContext());
        setContentView(R.layout.activity_main);

        mMapView = (MapView) findViewById(R.id.mapquestMapView);

        mMapView.onCreate(savedInstanceState);
        mMapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(MapboxMap mapboxMap) {
                mMapboxMap = mapboxMap;
                mMapView.setStreetMode();
                mMapboxMap.moveCamera(CameraUpdateFactory.newLatLngZoom(MAPQUEST_HEADQUARTERS_LOCATION, 12));
                addMarker(mapboxMap);
            }
        });
    }

    private void addMarker(MapboxMap mapboxMap) {
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(MAPQUEST_HEADQUARTERS_LOCATION);
        markerOptions.title("MapQuest");
        markerOptions.snippet("Welcome to Denver!");
        mapboxMap.addMarker(markerOptions);
    }

Adding annotations from MapQuest APIs

MapQuest provides a powerful set of APIs to work with our Maps SDKs such as geocoding, search, and navigation. Often you'll want to show annotations from a search ahead request or navigation stops.

In the sample below first sets the camera to the area around your current location, and then creates the Search Ahead instance. It passes in a search in the queryString around the current location and then puts markers on the map.

private void performSearch(String queryString, LocationResult locationResult) {

	Location lastLocation = locationResult.getLastLocation();
	mMapboxMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lastLocation.getLatitude(), lastLocation.getLongitude()), 10));

	if (mSearchAheadServiceV3 == null) {
		mSearchAheadServiceV3 = new SearchAheadService(this, BuildConfig.API_KEY);
	}

	List<SearchCollection> searchCollections = Collections.singletonList(SearchCollection.POI);
	try {
		MQLatLng latLng = new MQLatLng(lastLocation);

		SearchAheadQuery query = new SearchAheadQuery.Builder(queryString, searchCollections).location(latLng).build();
		mSearchAheadServiceV3.predictResultsFromQuery(query,
				new SearchAheadService.SearchAheadResponseCallback() {

					@Override
					public void onSuccess(@NonNull SearchAheadResponse searchAheadResponse) {

						MainActivity.this.runOnUiThread(() -> {
							for (SearchAheadResult result : searchAheadResponse.getResults()) {
								MarkerOptions markerOptions = new MarkerOptions();
								Place place = result.getPlace();
								AddressProperties aProp = place.getAddressProperties();

								String address = new StringBuilder()
										.append(StringUtils.defaultIfEmpty(aProp.getStreet(), "")).append("\n")
										.append(StringUtils.defaultIfEmpty(aProp.getCity(), "")).append(" ")
										.append(StringUtils.defaultIfEmpty(aProp.getStateCode(), "")).append( " ")
										.append(StringUtils.defaultIfEmpty(aProp.getPostalCode(), "")).append("\n")
									   .toString();
								markerOptions.position( MBLatLngUtil.fromMQLatLng( place.getLatLng() ) );
								markerOptions.title(result.getDisplayString());
								markerOptions.snippet(address);
								mMapboxMap.addMarker(markerOptions);
							}
						});
					 }

					@Override
					public void onError(Exception e) {
						L.e("Search Ahead V3 Failure", e);
					}
				});
	} catch (IllegalQueryParameterException e) {
		L.e("Error performing search", e);
	}
}

The Search Ahead and Maps SDK have different LatLng classes so you will have to convert between them.

import com.mapbox.mapboxsdk.geometry.LatLng;

public final class MBLatLngUtil extends LatLng {

    static LatLng fromMQLatLng(com.mapquest.android.commoncore.model.LatLng latLng) {
        return new LatLng((double)latLng.getLatitude(), (double)latLng.getLongitude());
    }
}

Responding to Marker Events

The MapQuest Android SDK provides a listener for capturing when a user taps on a marker. By default, all markers come with an onMarkerClick event listener for displaying and hiding info windows. You can override this default event listener and set your own with the setOnMarkerClickListener method.

mapboxMap.setOnMarkerClickListener(new MapboxMap.OnMarkerClickListener() {
  @Override
  public boolean onMarkerClick(@NonNull Marker marker) {
    Toast.makeText(MainActivity.this, marker.getTitle(), Toast.LENGTH_LONG).show();
    return true;
  }
});

Updating the Marker

private final LatLng CAMERA_LOC = new LatLng(39.745391, -105.00653);

// Change the marker location
marker.setPosition(CAMERA_LOC);

// Update the marker icon
marker.setIcon(icon);