根据显示在地图上警告对话框地点点击? [英] Display alert dialog box based on Maps place Click?
问题描述
我在练的Android地图现在我成功地得到映射在我的模拟器当我在谷歌搜索市场,我发现一个有趣的应用程序,我是尝试开发示例应用是
<一个href=\"https://play.google.com/store/apps/details?id=streetdirectory.mobile&feature=search_result#?t=W251bGwsMSwxLDEsInN0cmVldGRpcmVjdG9yeS5tb2JpbGUiXQ\" rel=\"nofollow\">https://play.google.com/store/apps/details?id=streetdirectory.mobile&feature=search_result#?t=W251bGwsMSwxLDEsInN0cmVldGRpcmVjdG9yeS5tb2JpbGUiXQ..
包com.example.tutorials;进口java.io.IOException异常;
进口java.util.Locale中;进口android.app.Activity;
进口android.content.Context;
进口android.graphics.drawable.Drawable;
进口android.location.Geo codeR;
进口android.location.LocationManager;
进口android.os.Bundle;进口com.google.android.maps.GeoPoint;
进口com.google.android.maps.MapActivity;
进口com.google.android.maps.MapController;
进口com.google.android.maps.MapView;进口com.google.android.maps.MapView.LayoutParams;
进口android.view.View;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.LinearLayout;
进口android.widget.Toast;
公共类GoogleMap的扩展MapActivity
{ MapView类MapView类; / **当第一次创建活动调用。 * /
@覆盖
保护布尔isRouteDisplayed()
{
返回false;
} 公共无效changeMap(字符串区)
{ 图形页面=(图形页面)findViewById(R.id.mapview);
MapController MC = mapView.getController();
GeoPoint的myLocation = NULL; 双纬度= 0;
双经度= 0;
尝试
{ 地理codeR G =新的地缘codeR(这一点,Locale.getDefault()); java.util.List的&LT; android.location.Address&GT;结果= g.getFromLocationName(区,1);
如果(result.size()大于0){ Toast.makeText(GoogleMap.this,国家+将String.valueOf(result.get(0).getCountryName()),Toast.LENGTH_SHORT).show();
纬度= result.get(0).getLatitude();
经度= result.get(0).getLongitude(); }
其他{
Toast.makeText(GoogleMap.this,没有找到记录,Toast.LENGTH_SHORT).show();
返回;
}
}
赶上(IOException异常IO)
{
Toast.makeText(GoogleMap.this,连接错误,Toast.LENGTH_SHORT).show();
}
myLocation =新的GeoPoint(
(INT)(LAT * 1E6)
(INT)(LNG * 1E6));
可绘制可绘制= this.getResources()getDrawable(R.drawable.icon)。
mc.animateTo(myLocation);
mc.setZoom(10);
mapView.invalidate();
}
@覆盖
公共无效的onCreate(捆绑savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
按钮btnSearch =(按钮)findViewById(R.id.btnSearch);
btnSearch.setOnClickListener(新View.OnClickListener(){ @覆盖
公共无效的onClick(视图v){
的EditText txtSearch =(EditText上)findViewById(R.id.txtMapSearch);
字符串面积= txtSearch.getText()的toString()。
Toast.makeText(GoogleMap.this,点击 - +将String.valueOf(区),Toast.LENGTH_SHORT).show();
GoogleMap.this.changeMap(区);
}
}); 图形页面=(图形页面)findViewById(R.id.mapview);
MapController mapController = mapView.getController();
mapController.setZoom(14); 的LinearLayout zoomLayout =(的LinearLayout)findViewById(R.id.zoom); } }
本code我得到Android中maps.how搜索的地方我可以提供对话框。当我们点击地图上的地方?
先谢谢了......
公共类的MapViewer扩展MapActivity {MapView类MapView类;MapController mapController;GeoPoint的mgeoPoint;可绘制标记;MyLocationOverlay mLocationOverlay;MotionEventê;
@覆盖
保护无效的onCreate(捆绑冰柱){ // TODO自动生成方法存根
super.onCreate(冰柱);
的setContentView(R.layout.main); 图形页面=(图形页面)findViewById(R.id.mapView); mapController = mapView.getController(); mapView.setBuiltInZoomControls(真); mapView.setStreetView(真);
标记= getResources()getDrawable(R.drawable.pushpin)。 marker.setBounds(0,0,marker.getIntrinsicWidth(),标记
.getIntrinsicHeight()); 。调用MapView.getOverlays()增加(新MapOverlay(标记));
mLocationOverlay =新MyLocationOverlay(这一点,MapView类);
调用MapView.getOverlays()加(mLocationOverlay)。 setViewLocation();
}
@覆盖
保护对话框onCreateDialog(INT ID){ // TODO自动生成方法存根 开关(ID){
情况下0:
返回新AlertDialog.Builder(本).setTitle(你好)的setIcon(
R.drawable.ic_launcher).setPositiveButton(是,
新OnClickListener(){ @覆盖
公共无效的onClick(DialogInterface对话,诠释它){ // TODO自动生成方法存根 }
})。setCancelable(真).setNegativeButton(取消,
新OnClickListener(){ @覆盖
公共无效的onClick(DialogInterface对话,诠释它){ // TODO自动生成方法存根 }
}) .setOnCancelListener(新OnCancelListener(){ @覆盖
公共无效onCancel(DialogInterface对话){ // TODO自动生成方法存根
Toast.makeText(getApplicationContext(),辞退,
Toast.LENGTH_SHORT).show();
} })。创建();
默认:
打破;
}
返回null;
}
私人无效setViewLocation(){ 的String [] =坐标{22.716221,75.896816}; 双纬度= Double.parseDouble(坐标[0]);
双经度= Double.parseDouble(坐标[1]);
mgeoPoint =新的GeoPoint((INT)(LAT * 1E6),(INT)(LNG * 1E6)); mapController.animateTo(mgeoPoint);
mapController.setZoom(15); mapView.invalidate();}
@覆盖
保护布尔isRouteDisplayed(){ // TODO自动生成方法存根
返回false;
}
点scrPoint;
私人的GeoPoint用GetPoint(双纬度,双LON){ 返回(新的GeoPoint((int)的(纬度* 1E6),(INT)(LON * 1E6)));
}
类MapOverlay扩展
com.google.android.maps.ItemizedOverlay&LT; OverlayItem&GT; { 清单&LT; OverlayItem&GT; ListofGeopoints =新的ArrayList&LT; OverlayItem&GT;();
公共MapOverlay(可绘制defaultMarker){ 超(defaultMarker); ListofGeopoints.add(新OverlayItem(用GetPoint(22.716221,75.896816)
在印度)); 填充(); }
@覆盖
保护布尔中的onTap(INT指数){ 开关(指数){
情况下0:
Toast.makeText(getApplicationContext(),GeoLocation中:0,
Toast.LENGTH_LONG).show();
的ShowDialog(0);
打破;
} 返回true;
}
字符串添加=; 清单&LT;地址&gt; add_List =新的ArrayList&LT;地址&gt;();
私人无效的getAddress(){ add_List = ReverseGeo code
.getFromLocation(35.594227,-105.223618,2); }
@覆盖
保护OverlayItem createItem中(int i)以{
返回(ListofGeopoints.get(ⅰ));
}
@覆盖
公众诠释大小(){
返回ListofGeopoints.size();
}
}
}
I am practicing the Android maps now i am getting the maps successfully in my emulator when i search in the Google market i found one Interesting App i am try for to develop the example app is
package com.example.tutorials;
import java.io.IOException;
import java.util.Locale;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Geocoder;
import android.location.LocationManager;
import android.os.Bundle;
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 com.google.android.maps.MapView.LayoutParams;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
public class GoogleMap extends MapActivity
{
MapView mapView;
/** Called when the activity is first created. */
@Override
protected boolean isRouteDisplayed()
{
return false;
}
public void changeMap(String area)
{
mapView = (MapView) findViewById(R.id.mapview);
MapController mc=mapView.getController();
GeoPoint myLocation=null;
double lat = 0;
double lng = 0;
try
{
Geocoder g = new Geocoder(this, Locale.getDefault());
java.util.List<android.location.Address> result=g.getFromLocationName(area, 1);
if(result.size()>0){
Toast.makeText(GoogleMap.this, "country: " + String.valueOf(result.get(0).getCountryName()), Toast.LENGTH_SHORT).show();
lat = result.get(0).getLatitude();
lng = result.get(0).getLongitude();
}
else{
Toast.makeText(GoogleMap.this, "record not found", Toast.LENGTH_SHORT).show();
return;
}
}
catch(IOException io)
{
Toast.makeText(GoogleMap.this, "Connection Error", Toast.LENGTH_SHORT).show();
}
myLocation = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
mc.animateTo(myLocation);
mc.setZoom(10);
mapView.invalidate();
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnSearch=(Button) findViewById(R.id.btnSearch);
btnSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText txtSearch=(EditText)findViewById(R.id.txtMapSearch);
String area=txtSearch.getText().toString();
Toast.makeText(GoogleMap.this, "Click-" + String.valueOf(area), Toast.LENGTH_SHORT).show();
GoogleMap.this.changeMap(area);
}
});
mapView = (MapView) findViewById(R.id.mapview);
MapController mapController = mapView.getController();
mapController.setZoom(14);
LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);
}
}
by this code i am getting search place in Android maps.how can i provide dialog box .when we click on the maps place?
Thanks in Advance......
public class MapViewer extends MapActivity {
MapView mapView;
MapController mapController;
GeoPoint mgeoPoint;
Drawable marker;
MyLocationOverlay mLocationOverlay;
MotionEvent e;
@Override
protected void onCreate(Bundle icicle) {
// TODO Auto-generated method stub
super.onCreate(icicle);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapView);
mapController = mapView.getController();
mapView.setBuiltInZoomControls(true);
mapView.setStreetView(true);
marker = getResources().getDrawable(R.drawable.pushpin);
marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker
.getIntrinsicHeight());
mapView.getOverlays().add(new MapOverlay(marker));
mLocationOverlay = new MyLocationOverlay(this, mapView);
mapView.getOverlays().add(mLocationOverlay);
setViewLocation();
}
@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
switch (id) {
case 0:
return new AlertDialog.Builder(this).setTitle("Hello").setIcon(
R.drawable.ic_launcher).setPositiveButton("Yes",
new OnClickListener() {
@Override
public void onClick(DialogInterface dialog , int which) {
// TODO Auto-generated method stub
}
}).setCancelable(true).setNegativeButton("Cancel",
new OnClickListener() {
@Override
public void onClick(DialogInterface dialog , int which) {
// TODO Auto-generated method stub
}
})
.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Dismiss",
Toast.LENGTH_SHORT).show();
}
}).create();
default:
break;
}
return null;
}
private void setViewLocation() {
String[] coordinates = { "22.716221", "75.896816" };
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
mgeoPoint = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
mapController.animateTo(mgeoPoint);
mapController.setZoom(15);
mapView.invalidate();
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
Point scrPoint;
private GeoPoint getPoint(double lat , double lon) {
return (new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6)));
}
class MapOverlay extends
com.google.android.maps.ItemizedOverlay<OverlayItem> {
List<OverlayItem> ListofGeopoints = new ArrayList<OverlayItem>();
public MapOverlay(Drawable defaultMarker ) {
super(defaultMarker);
ListofGeopoints.add(new OverlayItem(getPoint(22.716221, 75.896816),
"IN", "India"));
populate();
}
@Override
protected boolean onTap(int index) {
switch (index) {
case 0:
Toast.makeText(getApplicationContext(), "GeoLocation : 0",
Toast.LENGTH_LONG).show();
showDialog(0);
break;
}
return true;
}
String add = "";
List<Address> add_List = new ArrayList<Address>();
private void getAddress() {
add_List = ReverseGeocode
.getFromLocation(35.594227, -105.223618, 2);
}
@Override
protected OverlayItem createItem(int i) {
return (ListofGeopoints.get(i));
}
@Override
public int size() {
return ListofGeopoints.size();
}
}
}
这篇关于根据显示在地图上警告对话框地点点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!