谷歌地图MapOverlay问题
我的问题是我在特定位置上使用 PushPin 显示 Google 地图,但在我的应用程序中,如果用户可以单击图钉,位置名称将显示在 toast 或文本视图中,但我不知道该怎么做?
请帮我。
以下是我的代码:-
Map_Activity.java:-
public class Map_Activity extends MapActivity {
String mArea;
MapView mapView;
MapController mc;
GeoPoint p;
String[] mLongitude,mLatitude,mAreaArray;
double lat,lng;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.google_map_screen);
Bundle bdl=getIntent().getExtras();
mArea= bdl.getString("Area Name");
mAreaArray=getResources().getStringArray(R.array.PlaceName);
mLongitude=getResources().getStringArray(R.array.Longitude);
mLatitude=getResources().getStringArray(R.array.Latitude);
for(int i=0;i<mAreaArray.length;i++){
if(mArea.equals(mAreaArray[i])){
lat=Float.valueOf(mLatitude[i]);
lng=Float.valueOf(mLongitude[i]);
}
}
mapView = (MapView) findViewById(R.id.googlemap);
LinearLayout zoomLayout = (LinearLayout) findViewById(R.id.zoom);
@SuppressWarnings("deprecation")
View zoomView = mapView.getZoomControls();
zoomLayout.addView(zoomView, new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
mapView.displayZoomControls(true);
mc = mapView.getController();
p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
mc.animateTo(p);
mc.setZoom(17);
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
MapController mc = mapView.getController();
switch (keyCode) {
case KeyEvent.KEYCODE_3:
mc.zoomIn();
break;
case KeyEvent.KEYCODE_1:
mc.zoomOut();
break;
}
return super.onKeyDown(keyCode, event);
}
class MapOverlay extends com.google.android.maps.Overlay {
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
super.draw(canvas, mapView, shadow);
// ---translate the GeoPoint to screen pixels---
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
// ---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.pushpin);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 50, null);
return true;
}
@Override
public boolean onTap(GeoPoint p, MapView mapView) {
// TODO Auto-generated method stub
Toast.makeText(Google_Map_Activity.this, "Sorry Don't Click", Toast.LENGTH_SHORT).show();
return super.onTap(p, mapView);
}
}
}
My problem is I am displaying Google Map with PushPin on Particular Location but in my application if user can click on the pushpin the location name is display in toast or text-view, but i don't know how to do this?
Please Help me.
Following is my Code:-
Map_Activity.java:-
public class Map_Activity extends MapActivity {
String mArea;
MapView mapView;
MapController mc;
GeoPoint p;
String[] mLongitude,mLatitude,mAreaArray;
double lat,lng;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.google_map_screen);
Bundle bdl=getIntent().getExtras();
mArea= bdl.getString("Area Name");
mAreaArray=getResources().getStringArray(R.array.PlaceName);
mLongitude=getResources().getStringArray(R.array.Longitude);
mLatitude=getResources().getStringArray(R.array.Latitude);
for(int i=0;i<mAreaArray.length;i++){
if(mArea.equals(mAreaArray[i])){
lat=Float.valueOf(mLatitude[i]);
lng=Float.valueOf(mLongitude[i]);
}
}
mapView = (MapView) findViewById(R.id.googlemap);
LinearLayout zoomLayout = (LinearLayout) findViewById(R.id.zoom);
@SuppressWarnings("deprecation")
View zoomView = mapView.getZoomControls();
zoomLayout.addView(zoomView, new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
mapView.displayZoomControls(true);
mc = mapView.getController();
p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
mc.animateTo(p);
mc.setZoom(17);
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
MapController mc = mapView.getController();
switch (keyCode) {
case KeyEvent.KEYCODE_3:
mc.zoomIn();
break;
case KeyEvent.KEYCODE_1:
mc.zoomOut();
break;
}
return super.onKeyDown(keyCode, event);
}
class MapOverlay extends com.google.android.maps.Overlay {
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
super.draw(canvas, mapView, shadow);
// ---translate the GeoPoint to screen pixels---
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
// ---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.pushpin);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 50, null);
return true;
}
@Override
public boolean onTap(GeoPoint p, MapView mapView) {
// TODO Auto-generated method stub
Toast.makeText(Google_Map_Activity.this, "Sorry Don't Click", Toast.LENGTH_SHORT).show();
return super.onTap(p, mapView);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请点击以下链接:
http://developer.android.com/guide/tutorials /views/hello-mapview.html
http://www.vogella.de/articles/AndroidLocationAPI/article.html
follow these links:
http://developer.android.com/guide/tutorials/views/hello-mapview.html
http://www.vogella.de/articles/AndroidLocationAPI/article.html
请点击以下链接。这提供了所有步骤的详细解释。
http://mobiforge.com/developing/story/using-google-maps-android
Follow the bellow link. This provides detailed explanation for all the steps.
http://mobiforge.com/developing/story/using-google-maps-android