Android谷歌MAPVIEW的默认标记?

时间:2010-08-31 00:30:01

标签: android android-mapview

只是想知道我是否可以在MAPVIEW中使用标准/默认覆盖/标记?

我一直在网上搜索,所有教程都谈论扩展叠加层并将自定义图像放在上面。

有更简单的方法吗?我只想要一个标记,没什么特别的。

此致

詹姆斯

2 个答案:

答案 0 :(得分:2)

如果您使用Eclipse作为IDE,则可以将.png添加到/ drawable-hdpi,/ drawable-ldpi,/ drawable-mdpi目录中。然后,您可以放置​​并获取叠加层的引用列表,如下所示:

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) {

    }
}

我从教程中获得了大部分代码,我无法再找到链接。叠加层非常易于操作。希望这会有所帮助。

答案 1 :(得分:0)

如果您使用的是Google Maps Android API v2,则可以使用以下方法。

mapView.getMap().addMarker(new MarkerOptions()
                               .position(new LatLng(10, 10))
                               .title("Hello world"));