Pratik Pratik - 1 year ago 94
Android Question

how to display map in android with marker

I am working on android app and I am totally newbe in this. So I want to know how to display marker in map and how to change his position on specific time like defining thread or anything in background which will send the latitude and longitude value and marker was move on that

Answer Source

If you're going to show just a single item, MapView.addView() and later updating position with setLayoutParams does the trick.

private ImageView mCurrentPointer;

public void onLocationChanged(Location l) {
    int latitude = (int) (l.getLatitude() * 1e6);
    int longitude = (int) (l.getLongitude() * 1e6);
    // Prepare new LayoutParams object that centers on our new latitude/longitude
    MapView.LayoutParams lp = new MapView.LayoutParams(MapView.LayoutParams.WRAP_CONTENT, 
            MapView.LayoutParams.WRAP_CONTENT, new GeoPoint(latitude, longitude),

    if (mCurrentPointer == null) {
        // If "current location" pin is null, we haven't added it
        // to MapView yet. So instantiate it and add it to MapView:
        mCurrentPointer = new ImageView(this); 
        mMapView.addView(mCurrentPointer, lp);
    } else {
        // If it's already added, just update its location
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download