在地图上标记位置名称?
Posted
技术标签:
【中文标题】在地图上标记位置名称?【英文标题】:Mark name of location on map? 【发布时间】:2011-08-29 18:41:12 【问题描述】:在我的 android 应用中,我想在地图上标记一个位置(我知道位置的地址)。当我在地图上标记它时,我也想显示位置的名称。我该怎么做?
我有这个:OverlayItem(p ,"Kaufland", "Magic Kingdom"),但在地图上只显示点,没有名字。
这是我的代码:
package com.ShoppingList.Shops;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import com.ShoppingList.R;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import java.util.ArrayList;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
public class ShowMap extends MapActivity
private MapView mapView;
MapController mc;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.showmap);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setClickable(true);
Drawable marker=getResources().getDrawable(R.drawable.pushpin);
marker.setBounds(0, 0, marker.getIntrinsicWidth(),
marker.getIntrinsicHeight());
InterestingLocations funPlaces = new InterestingLocations(marker);
mapView.getOverlays().add(funPlaces);
GeoPoint pt = funPlaces.getCenter(); // get the first-ranked point
mapView.getController().setCenter(pt);
mapView.getController().setZoom(15);
@Override
protected boolean isLocationDisplayed()
return false;
@Override
protected boolean isRouteDisplayed()
return false;
class InterestingLocations extends ItemizedOverlay
private List locations = new ArrayList();
private Drawable marker;
String adresa;
public InterestingLocations(Drawable marker)
super(marker);
this.marker=marker;
// create locations of interest
Bundle bundle = getIntent().getExtras();
adresa = bundle.getString("adress");
Geocoder geoCoder = new Geocoder(ShowMap.this, Locale.getDefault());
try
List addresses = geoCoder.getFromLocationName(adresa, 5);
String add = "";
if (addresses.size() > 0)
GeoPoint p = new GeoPoint((int) (addresses.get(0).getLatitude() * 1E6),
(int) (addresses.get(0).getLongitude() * 1E6));
locations.add(new OverlayItem(p ,"Kaufland", "Magic Kingdom"));
catch (IOException e)
e.printStackTrace();
populate();
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow)
super.draw(canvas, mapView, shadow);
boundCenterBottom(marker);
@Override
protected OverlayItem createItem(int i)
return locations.get(i);
@Override
public int size()
return locations.size();
【问题讨论】:
【参考方案1】:我不敢相信没有更简单的方法,但它看起来并不存在。这是一种方法: http://binwaheed.blogspot.com/2011/05/android-display-title-on-marker-in.html
除非你想要丑陋的文本,否则我强烈建议使用抗锯齿:
TextPaint paintText = new TextPaint(Paint.ANTI_ALIAS_FLAG);
【讨论】:
【参考方案2】:我使用 Toast 来显示标记所指示的文本。这是我为演示项目解决它的方法:
-- 开始 Location.java --
package com.cb2enterprises.geocode;
import com.google.android.maps.GeoPoint;
public class Location
public GeoPoint Point;
public String Title;
public String Snippet;
public Location(GeoPoint point, String title, String snippet)
Point = point;
Title = title;
Snippet = snippet;
-- 结束--
-- 开始 PushpinOverlay.java --
package com.cb2enterprises.geocode;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.widget.Toast;
import android.view.Gravity;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
import java.util.ArrayList;
import java.util.List;
public class PushpinOverlay extends ItemizedOverlay<OverlayItem>
private List<Location> mItems;
Context mContext = null;
public PushpinOverlay(Context context, Drawable marker)
super(boundCenterBottom(marker));
mContext = context;
public void setItems(ArrayList<Location> items)
mItems = items;
populate();
@Override
protected OverlayItem createItem(int i)
return new OverlayItem(mItems.get(i).Point, mItems.get(i).Title, mItems.get(i).Snippet);
@Override
public int size()
return mItems.size();
@Override
protected boolean onTap(int i)
Toast msg = Toast.makeText(mContext, mItems.get(i).Title, Toast.LENGTH_LONG);
msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2);
msg.show();
return true;
-- 结束--
-- 开始 GeoCodeDemoActivity.java --
package com.cb2enterprises.geocode;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
public class GeoCodeDemoActivity extends MapActivity
Geocoder geocoder = null;
MapView mapView = null;
ProgressDialog progDialog = null;
List<Address> addressList = null;
MyLocationOverlay curLocOverlay = null;
ArrayList<Location> locations = null;
PushpinOverlay pushpin = null;
@Override
protected boolean isLocationDisplayed()
return false;
@Override
protected boolean isRouteDisplayed()
return false;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView)findViewById(R.id.geoMap);
mapView.setBuiltInZoomControls(true);
locations = new ArrayList<Location>();
Drawable icon = getResources().getDrawable(R.drawable.pin);
pushpin = new PushpinOverlay(this, icon);
// put the current location as hockey puck
curLocOverlay = new MyLocationOverlay(this, mapView);
curLocOverlay.runOnFirstFix(new Runnable()
public void run()
mapView.getController().animateTo(curLocOverlay.getMyLocation());
);
mapView.getOverlays().add(curLocOverlay);
mapView.getController().setZoom(12);
Button geoBtn = (Button)findViewById(R.id.geocodeBtn);
geocoder = new Geocoder(this);
geoBtn.setOnClickListener(new OnClickListener()
@Override
public void onClick(View arg0)
EditText loc = (EditText)findViewById(R.id.location);
String locationName = loc.getText().toString();
progDialog = ProgressDialog.show(GeoCodeDemoActivity.this, "Processing",
"Finding location", true, false);
findLocation(locationName);
);
@Override
public void onResume()
super.onResume();
curLocOverlay.enableMyLocation();
@Override
public void onPause()
super.onPause();
curLocOverlay.disableMyLocation();
private void findLocation(final String locationName)
Thread thrd = new Thread()
public void run()
try
//do background work
addressList = geocoder.getFromLocationName(locationName, 5);
//send message to handler to process results
uiCallback.sendEmptyMessage(0);
catch (IOException e)
e.printStackTrace();
;
thrd.start();
//ui thread callback handler
private Handler uiCallback = new Handler()
@Override
public void handleMessage(Message msg)
progDialog.dismiss();
if(addressList != null && addressList.size() > 0)
int lat = (int)(addressList.get(0).getLatitude()*1E6);
int lng = (int)(addressList.get(0).getLongitude()*1E6);
GeoPoint pt = new GeoPoint(lat, lng);
locations.add(new Location(pt, addressList.get(0).getFeatureName(), addressList.get(0).getAddressLine(0)));
pushpin.setItems(locations);
mapView.getOverlays().add(pushpin);
mapView.getController().setCenter(locations.get(locations.size()-1).Point);
mapView.getController().setZoom(15);
else
Dialog foundNothingDlg = new AlertDialog.Builder(GeoCodeDemoActivity.this)
.setIcon(0)
.setTitle("Failed to find location")
.setPositiveButton("Ok", null)
.setMessage("Location not found...")
.create();
foundNothingDlg.show();
;
【讨论】:
以上是关于在地图上标记位置名称?的主要内容,如果未能解决你的问题,请参考以下文章