Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default marker for android google MAPVIEW?

Just wondering if there is a standard/default overlay/marker that I can use in the MAPVIEW?

I have been searching on the web and all tutorials talk about extending the Overlay and put your custom image on it.

Is there a easier way? I just want to have a marker, nothing fancy.

Regards

James

like image 230
James Lin Avatar asked Aug 31 '10 00:08

James Lin


People also ask

What are Android markers?

Markers are objects of type Marker , and are added to the map with the GoogleMap. addMarker(markerOptions) method. Markers are designed to be interactive. They receive click events by default, and are often used with event listeners to bring up info windows.

How do I add a marker to Google Maps Android?

For adding a custom marker to Google Maps navigate to the app > res > drawable > Right-Click on it > New > Vector Assets and select the icon which we have to show on your Map. You can change the color according to our requirements. After creating this icon now we will move towards adding this marker to our Map.

What is marker ID in Google Map?

A marker identifies a location on a map. By default, a marker uses a standard image.


1 Answers

If you are using Eclipse as your IDE, you can add a .png, for example, to your /drawable-hdpi, /drawable-ldpi, /drawable-mdpi directories. You can then place and obtain a list of references to your overlays somewhat like this:

package com.practice.mapper;

import java.util.ArrayList;

import android.graphics.drawable.Drawable;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

public class Itemization extends ItemizedOverlay<OverlayItem> {

    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();

    public Itemization(Drawable defaultMarker) {

        super(boundCenterBottom(defaultMarker));
        // super(defaultMarker);

    }

    @Override
    protected OverlayItem createItem(int i) {
        return mOverlays.get(i);
    }

    public void addOverlay(OverlayItem overlay) {
        mOverlays.add(overlay);
        populate();
    }

    @Override
    public int size() {
        return mOverlays.size();
    }

}



package com.practice.mapper;

import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ZoomControls;

public class Mapper extends MapActivity implements LocationListener {

    Location presentLocation;
    ZoomControls z;
    LinearLayout linearLayout;
    MapView mapView;
    List<Overlay> mapOverlays;
    Drawable drawable;
    Itemization itemizedOverlay;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);

        mapOverlays = mapView.getOverlays();
        drawable = this.getResources().getDrawable(R.drawable.androidmarker);
        itemizedOverlay = new Itemization(drawable);

        GeoPoint point = new GeoPoint(19240000, -99120000);
        OverlayItem overlayitem = new OverlayItem(point, "", "");
        itemizedOverlay.addOverlay(overlayitem);

        GeoPoint point2 = new GeoPoint(35410000, 139460000);
        OverlayItem overlayitem2 = new OverlayItem(point2, "", "");
        itemizedOverlay.addOverlay(overlayitem2);

        mapOverlays.add(itemizedOverlay);
    }

    public void btnUpdateClicked(View v) {

        LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

        LocationProvider p = lm.getProvider(LocationManager.GPS_PROVIDER);

        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 20000, 1, this);

        List<String> enabledProv = lm.getProviders(true);

        Location l1 = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        Location l2 = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        Location l3 = lm.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);

        Button b = new Button(this.getApplicationContext());
        b.setOnClickListener(new OnClickListener(){

            public void onClick(View v) {

            }});

        lm.toString();
    }

    @Override
    protected boolean isRouteDisplayed() {

        return false;
    }

    public void onLocationChanged(Location location) {

        presentLocation = location;
        Log.d("TEST", "New location received");
    }

    public void onProviderDisabled(String provider) {

    }

    public void onProviderEnabled(String provider) {

    }

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

    }
}

I got most of this code from a tutorial I can no longer find the link to. The overlays are pretty simple to manipulate. Hope this helps.

like image 175
Alex Avatar answered Sep 17 '22 17:09

Alex