1

I am using the Esri ArcGIS sdk on android, and I am trying to do the following:

  • Get my position

  • When my position is clicked, show a callout

So far i've been lucky in finishing the first one, by using a locationServ class

I've also created the callout and tested it, it's fine but my question here is how to make the map only show the callout when I click my position and not anywhere else on the map ?

Here's my code:

package com.appzgate.locator;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.graphics.Color;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.esri.android.map.Callout;
import com.esri.android.map.GraphicsLayer;
import com.esri.android.map.LocationService;
import com.esri.android.map.MapView;
import com.esri.android.map.bing.BingMapsLayer;
import com.esri.android.map.bing.BingMapsLayer.MapStyle;
import com.esri.android.map.event.OnSingleTapListener;
import com.esri.android.toolkit.map.MapViewHelper;
import com.esri.core.geometry.GeometryEngine;
import com.esri.core.geometry.Point;
import com.esri.core.geometry.SpatialReference;
import com.esri.core.map.Graphic;
import com.esri.core.symbol.SimpleMarkerSymbol;

public class PlacelocatorActivity extends Activity {

MapView mMapView;
BingMapsLayer bing;
GraphicsLayer locationLayer;
LocationService loc;
private Callout m_callout;
private int m_calloutStyle;
private ViewGroup calloutContent;
Graphic graphic;
Point mLocation = null, p, mapPoint;
final SpatialReference wm = SpatialReference.create(102100);
final SpatialReference egs = SpatialReference.create(4326);
MapViewHelper mvHelper;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mMapView = (MapView) findViewById(R.id.map);
    mLocation = new Point();
    mMapView.enableWrapAround(true);
     mvHelper = new MapViewHelper(mMapView);

    m_calloutStyle = R.xml.identify_calloutstyle;
    LayoutInflater inflater = getLayoutInflater();
    m_callout = mMapView.getCallout();
    // Get the layout for the Callout from
    // layout->identify_callout_content.xml
    calloutContent = (ViewGroup) inflater.inflate(
            R.layout.identify_callout_content, null);
    m_callout.setContent(calloutContent);

    bing = new BingMapsLayer(
            "ArUDBy4hjDCywOK-GVofer_1U6GG3GPWxaZ8zp68o01W-nQdiLgcV9MtvGEehxn5",
            MapStyle.AERIAL);
    mMapView.addLayer(bing);

    loc = mMapView.getLocationService();
    loc.setLocationListener(new MyLocationListener());
    loc.start();
    loc.setAutoPan(false);
    mLocation = loc.getPoint();

    locationLayer = new GraphicsLayer();

    mMapView.addLayer(locationLayer);
    mMapView.setOnSingleTapListener(new OnSingleTapListener() {

        @Override
        public void onSingleTap(float x, float y) {
            // TODO Auto-generated method stub
            // mapPoint = mMapView.toMapPoint(x, y);
            identifyLocation(x, y);
        }
    });
}

void identifyLocation(float x, float y) {

    // Hide the callout, if the callout from previous tap is still showing
    if (m_callout.isShowing()) {
        m_callout.hide();
    }

    Point mapPoint = mMapView.toMapPoint(x, y);

    checkGraphic();

    if (graphic != null) {
        ShowCallout(m_callout, graphic, mLocation);
    }
}

private void checkGraphic() {
    // TODO Auto-generated method stub
    SimpleMarkerSymbol sms = new SimpleMarkerSymbol(Color.GREEN, 25,
            SimpleMarkerSymbol.STYLE.CROSS);

    Map<String, Object> hm = new HashMap<String, Object>();
    hm.put("NAME", "Amman");
    hm.put("COUNTRY", "Jordan");
    graphic = new Graphic(mLocation, sms, hm);
    locationLayer.addGraphic(graphic);
}

@Override
protected void onDestroy() {
    super.onDestroy();
}

@Override
protected void onPause() {
    super.onPause();
    mMapView.pause();
}

@Override
protected void onResume() {
    super.onResume();
    mMapView.unpause();
}

private class MyLocationListener implements LocationListener {

    public MyLocationListener() {
        super();
    }

    /**
     * If location changes, update our current location. If being found for
     * the first time, zoom to our current position with a resolution of 20
     */
    public void onLocationChanged(Location loc) {
        if (loc == null)
            return;
        boolean zoomToMe = (mLocation == null) ? true : false;
        mLocation = new Point(loc.getLongitude(), loc.getLatitude());
        if (zoomToMe) {

            p = (Point) GeometryEngine.project(mLocation, egs, wm);
            // graphic = new Graphic((Geometry) p, (Symbol) sms, hm);
            // locationLayer.addGraphic(graphic);
            mMapView.zoomToResolution(p, 20.0);
        }
    }

    public void onProviderDisabled(String provider) {
        Toast.makeText(getApplicationContext(), "GPS Disabled",
                Toast.LENGTH_SHORT).show();
    }

    public void onProviderEnabled(String provider) {
        Toast.makeText(getApplicationContext(), "GPS Enabled",
                Toast.LENGTH_SHORT).show();
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
    }

}

private void ShowCallout(Callout calloutView, Graphic graphic,
        Point mapPoint) {
    Log.v("call", "in the callout");
    // Get the values of attributes for the Graphic
    String cityName = (String) graphic.getAttributeValue("NAME");
    String countryName = (String) graphic.getAttributeValue("COUNTRY");
    // String cityPopulationValue = ((Double) graphic
    // .getAttributeValue("POPULATION")).toString();
    Log.v("call", "so far so good");
    // Set callout properties
    calloutView.setCoordinates(mapPoint);
    calloutView.setStyle(m_calloutStyle);
    calloutView.setMaxWidth(325);

    // Compose the string to display the results
    StringBuilder cityCountryName = new StringBuilder();
    cityCountryName.append(cityName);
    cityCountryName.append(", ");
    cityCountryName.append(countryName);

    TextView calloutTextLine1 = (TextView) findViewById(R.id.citycountry);
    calloutTextLine1.setText(cityCountryName);

    // Compose the string to display the results
    StringBuilder cityPopulation = new StringBuilder();
    cityPopulation.append("Population: ");
    // cityPopulation.append(cityPopulationValue);

    // TextView calloutTextLine2 = (TextView) findViewById(R.id.population);
    // calloutTextLine2.setText(cityPopulation);
    calloutView.setContent(calloutContent);
    calloutView.show();
  }

}
1

I've figured this on my own by adding the following code:

int loaded = mvHelper.addMarkerGraphic(loc.getLatitude(),
                    loc.getLongitude(), "Jordan", "Amman", null, icon,
                    false, 0);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.