Javascript 优化Android代码
在我的应用程序中,我在地图上画了12个标记,然后对于每个标记,我有12个dilalog框,弹出并显示标记位置的地址。我想一定有更好的方法,所以我一直在尝试创建一个列表来保存所有标记,并在点击对话框时将标记从列表传递到对话框中,这样我就不必有12个seprerate对话框 目前,我的标记正在被绘制到屏幕上,但当标记被录制时,对话框不会显示,但当我使用12个单独的对话框时,对话框会显示。谁能给我指出问题所在的正确方向Javascript 优化Android代码,javascript,android,google-maps,google-maps-api-3,Javascript,Android,Google Maps,Google Maps Api 3,在我的应用程序中,我在地图上画了12个标记,然后对于每个标记,我有12个dilalog框,弹出并显示标记位置的地址。我想一定有更好的方法,所以我一直在尝试创建一个列表来保存所有标记,并在点击对话框时将标记从列表传递到对话框中,这样我就不必有12个seprerate对话框 目前,我的标记正在被绘制到屏幕上,但当标记被录制时,对话框不会显示,但当我使用12个单独的对话框时,对话框会显示。谁能给我指出问题所在的正确方向 private List<Marker> markers = new
private List<Marker> markers = new ArrayList<Marker>();
//sets the default map location to glasgow
double defaultLat = 55.85432829452839;
double defaultLng = -4.268357989501965;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_muc_main);
//get drawable IDs
icoUser = R.drawable.yellow_point;
icoScotstoun = R.drawable.squash;
icoIbrox = R.drawable.rugby;
icoSecc = R.drawable.boxing;
icoKelvingrove = R.drawable.bowls;
icoHockey = R.drawable.hockey;
icoVeledrome = R.drawable.athetics;
icoCeltic = R.drawable.party;
icoTollcross = R.drawable.aqua;
icoHampden = R.drawable.athetics;
icoCathkin = R.drawable.cycleing;
icoStrathclyde = R.drawable.tri;
icoEdinburgh = R.drawable.athetics;
icoDundee = R.drawable.shooting;
//ArrayAdapter <String> adapter = new ArrayAdapter<String>(MucMainActivity.this, android.R.layout.simple_spinner_item, paths);
ArrayAdapter <String> adapter = new ArrayAdapter<String>(MucMainActivity.this, R.layout.spinner_layout, maptypes);
mapTypeSpinner = (Spinner) findViewById(R.id.spinner1);
mapTypeSpinner.setAdapter(adapter);
mapTypeSpinner.setOnItemSelectedListener(this);
//checks if the map has been instantiated or not if it hasn't then the map gets istantiated
if(mMap == null)
{
//passes the map fragment ID from the layout XML and casts it to a map fragment object
//and gets the google map object.
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
}
if(mMap != null)
{
defaultSetting();
addListenerOnChkIos();
mMap.setOnMarkerClickListener(this);
}
//showUserLocation();
}
//@Override
// public boolean onCreateOptionsMenu(Menu menu)
//{
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.muc_main, menu);
//return true;
//}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
// TODO Auto-generated method stub
int position = mapTypeSpinner.getSelectedItemPosition();
switch(position)
{
case 0:
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
break;
case 1:
mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
break;
case 2:
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0)
{
// TODO Auto-generated method stub
}
private void showUserLocation()
{
//get location manager
locMan = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
//get last location
Location lastLoc = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
double lat = lastLoc.getLatitude();
double lng = lastLoc.getLongitude();
//create LatLng
LatLng lastLatLng = new LatLng(lat, lng);
//remove any existing marker
if(marUser!=null) marUser.remove();
//create and set marker properties
marUser = mMap.addMarker(new MarkerOptions()
.position(lastLatLng)
.title("You are here")
.icon(BitmapDescriptorFactory.fromResource(icoUser)));
//move to location
mMap.animateCamera(CameraUpdateFactory.newLatLng(lastLatLng), 1000, null);
}
public void defaultSetting()
{
//sets the map type
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
// sets the default location of the map to Glasgow
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(defaultLat, defaultLng), 12.0f), null );
//mMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(55.85432829452839, -4.268357989501965)), 3000, null);
//mMap.addMarker(new MarkerOptions().position(new LatLng(defaultLat,defaultLng)).title("hello"));
addMarkers();
}
public void addMarkers()
{
for ( int i = 0; i < 11; i++)
{
red = mMap.addMarker(new MarkerOptions().position(new LatLng(55.88069,-4.34025)).icon(BitmapDescriptorFactory.fromResource(icoScotstoun)));
markers.add(red);
blue = mMap.addMarker(new MarkerOptions().position(new LatLng(55.85360,-4.30454)).icon(BitmapDescriptorFactory.fromResource(icoIbrox)));
markers.add(blue);
green = mMap.addMarker(new MarkerOptions().position(new LatLng(55.86070,-4.28761)).icon(BitmapDescriptorFactory.fromResource(icoSecc)));
markers.add(green);
yellow = mMap.addMarker(new MarkerOptions().position(new LatLng(55.86782,-4.28875)).icon(BitmapDescriptorFactory.fromResource(icoKelvingrove)));
markers.add(yellow);
brown = mMap.addMarker(new MarkerOptions().position(new LatLng(55.84496,-4.23671)).icon(BitmapDescriptorFactory.fromResource(icoHockey)));
markers.add(brown);
pink = mMap.addMarker(new MarkerOptions().position(new LatLng(55.84496,-4.23671)).icon(BitmapDescriptorFactory.fromResource(icoVeledrome)));
markers.add(pink);
purple = mMap.addMarker(new MarkerOptions().position(new LatLng(55.84959,-4.20555)).icon(BitmapDescriptorFactory.fromResource(icoCeltic)));
markers.add(purple);
orange = mMap.addMarker(new MarkerOptions().position(new LatLng(55.84505,-4.17607)).icon(BitmapDescriptorFactory.fromResource(icoTollcross)));
markers.add(orange);
cyan = mMap.addMarker(new MarkerOptions().position(new LatLng(55.82570,-4.25239)).icon(BitmapDescriptorFactory.fromResource(icoHampden)));
markers.add(cyan);
magenta = mMap.addMarker(new MarkerOptions().position(new LatLng(55.79550,-4.22329)).icon(BitmapDescriptorFactory.fromResource(icoCathkin)));
markers.add(magenta);
olive = mMap.addMarker(new MarkerOptions().position(new LatLng(55.78529,-4.01481)).icon(BitmapDescriptorFactory.fromResource(icoStrathclyde)));
markers.add(olive);
darkBlue = mMap.addMarker(new MarkerOptions().position(new LatLng(55.93920,-3.17273)).icon(BitmapDescriptorFactory.fromResource(icoEdinburgh)));
markers.add(darkBlue);
LightGreen = mMap.addMarker(new MarkerOptions().position(new LatLng(56.49302,-2.74663)).icon(BitmapDescriptorFactory.fromResource(icoDundee)));
markers.add(LightGreen);
//dundeeM = mMap.addMarker(new MarkerOptions().position(new LatLng(56.49302,-2.74663)).title("DUNDEE"));
}
markers.size(); }
@Override
public boolean onMarkerClick(Marker marker)
{
if(marker.equals(marker.getId()))
{
// Create custom dialog object
final Dialog dialog = new Dialog(MucMainActivity.this);
// Include dialog.xml file
dialog.setContentView(R.layout.custom);
// Set dialog title
dialog.setTitle(marker.getTitle());
// set values for custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.textDialog);
text.setText("Cathkin Braes Country Park" + "\n"+ "cathkin Road" + "\n" + "Glasgow" + "\n" + "G45");
ImageView image = (ImageView) dialog.findViewById(R.id.imageDialog);
image.setImageResource(R.drawable.biking);
dialog.show();
Button declineButton = (Button) dialog.findViewById(R.id.declineButton);
// if decline button is clicked, close the custom dialog
declineButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// Close dialog
dialog.dismiss();
}
});
}
return true;
}
private List markers=new ArrayList();
//将默认地图位置设置为格拉斯哥
双默认LAT=55.85432829452839;
双默认液化天然气=-4.268357989501965;
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u muc\u main);
//获取可提取ID
icoUser=R.可绘制.黄色\u点;
Icoscottoun=R.drawable.squash;
icoIbrox=R.drawable.rugby;
icoSecc=R.drawable.boxing;
icoKelvingrove=R.drawable.bowls;
i曲棍球=R.可拉拔曲棍球;
二十面体=R.可绘制的无神论者;
icoCeltic=R.drawable.party;
icoTollcross=R.drawable.aqua;
icoHampden=R.drawable.athetics;
icoCathkin=R.可牵引.循环;
icoStrathclyde=R.drawable.tri;
爱丁堡=R.drawable.athetics;
icoDundee=R.drawable.shooting;
//ArrayAdapter=新的ArrayAdapter(MucMainActivity.this,android.R.layout.simple\u微调器\u项,路径);
ArrayAdapter=新的ArrayAdapter(MucMainActivity.this、R.layout.spinner\u layout、maptypes);
mapTypeSpinner=(Spinner)findViewById(R.id.spinner1);
mapTypeSpinner.setAdapter(适配器);
mapTypeSpinner.setOnItemSelectedListener(此);
//检查映射是否已实例化。如果尚未实例化,则映射将被实例化
如果(mMap==null)
{
//从布局XML传递地图片段ID,并将其强制转换为地图片段对象
//并获取GoogleMap对象。
mMap=((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
}
如果(mMap!=null)
{
默认设置();
addListenerOnChkIos();
mMap.setOnMarkerClickListener(此);
}
//showUserLocation();
}
//@凌驾
//公共布尔onCreateOptions菜单(菜单)
//{
//为菜单充气;这会将项目添加到操作栏(如果存在)。
//getMenuInflater().充气(R.menu.muc_主菜单,菜单);
//返回true;
//}
@凌驾
已选择公共视图(AdapterView arg0、视图arg1、内部arg2、长arg3)
{
//TODO自动生成的方法存根
int position=mapTypeSpinner.getSelectedItemPosition();
开关(位置)
{
案例0:
mMap.setMapType(GoogleMap.MAP\u TYPE\u NORMAL);
打破
案例1:
mMap.setMapType(GoogleMap.MAP\u TYPE\u地形);
打破
案例2:
mMap.setMapType(GoogleMap.MAP_TYPE_卫星);
打破
}
}
@凌驾
未选择公共无效(AdapterView arg0)
{
//TODO自动生成的方法存根
}
私有void showUserLocation()
{
//获取位置管理器
locMan=(LocationManager)getSystemService(Context.LOCATION\u服务);
//获取最后位置
Location lastLoc=locMan.getlastnownlocation(LocationManager.NETWORK\u PROVIDER);
双纬度=lastLoc.getLatitude();
double lng=lastLoc.getLongitude();
//创建板条
LatLng lastLatLng=新LatLng(lat,lng);
//删除任何现有标记
如果(marUser!=null)marUser.remove();
//创建和设置标记属性
marUser=mMap.addMarker(新的MarkerOptions()
.位置(最后一个)
.title(“您在这里”)
.icon(BitmapDescriptorFactory.fromResource(icoUser));
//移动到位置
mMap.animateCamera(CameraUpdateFactory.newLatLng(lastLatLng),1000,空);
}
public void defaultSetting()
{
//设置贴图类型
mMap.setMapType(GoogleMap.MAP\u TYPE\u NORMAL);
//将地图的默认位置设置为格拉斯哥
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(newlatlng(defaultLat,defaultLng),12.0f),null);
//mMap.animateCamera(CameraUpdateFactory.newLatLng(newLatLng(55.85432829452839,-4.268357989501965)),3000,空);
//mMap.addMarker(newmarkeroptions().position(newlatlng(defaultLat,defaultLng)).title(“hello”);
添加标记();
}
公共void addMarkers()
{
对于(int i=0;i<11;i++)
{
red=mMap.addMarker(新MarkerOptions().position(新LatLng(55.88069,-4.34025)).icon(BitmapDescriptorFactory.fromResource(icoscottoun));
标记。添加(红色);
blue=mMap.addMarker(新的MarkerOptions().position(新的LatLng(55.85360,-4.30454)).icon(BitmapDescriptorFactory.fromResource(icoIbrox));
标记。添加(蓝色);
green=mMap.addMarker(新的MarkerOptions().position(新的LatLng(55.86070,-4.28761)).icon(BitmapDescriptorFactory.fromResource(icoSecc));
标记。添加(绿色);
黄色=mMap.addMarker(新的MarkerOptions().position(新的LatLng(55.86782,-4.28875)).icon(BitmapDescriptorFactory.fromResource(icokevingrove));
标记。添加(黄色);
brown=mMap.addMarker(新的MarkerOptions().position(新的LatLng(55.84496,-4.23671)).icon(BitmapDescriptorFactory.fromResource(ICOchockey));
标记。添加(棕色);
pink=mMap.addMarker(新的MarkerOptions().position(新的LatLng(55.84496,-4.23671)).icon(BitmapDescriptorFactory.fromResource(icoVeledrome));
标记。添加(粉红色);
紫色=mMap.addMarker(新标记选项().position(新车床)(55.84959,-4.20555)).icon(位图描述符工厂.f
public boolean onMarkerClick(Marker marker)
if(marker.equals(marker.getId()))
private List<Marker> markers = new ArrayList<Marker>();
@Override
public boolean onMarkerClick(Marker marker)
{
{
// Create custom dialog object
final Dialog dialog = new Dialog(MucMainActivity.this);
// Include dialog.xml file
dialog.setContentView(R.layout.custom);
// Set dialog title
dialog.setTitle(marker.getTitle());
// set values for custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.textDialog);
text.setText(marker.getSnippet());
ImageView image = (ImageView) dialog.findViewById(R.id.imageDialog);
image.setImageResource(R.drawable.biking);
dialog.show();
Button declineButton = (Button) dialog.findViewById(R.id.declineButton);
// if decline button is clicked, close the custom dialog
declineButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// Close dialog
dialog.dismiss();
}
});
}
return true;
}