如何显示在我的Android在谷歌地图位置? [英] How to show my Current location in Google Map in android?
问题描述
在我的应用我使用谷歌地图API第2版。我禁用使用 map.setMyLocationEnabled(假)我的位置按钮;
,我想增加新的按钮,做同样的工作。如何使用另一个按钮来显示相同的方式,我的位置按钮会做我的当前位置?
原因:
在我的应用我想用行动吧覆盖在原来的地图应用程序。但是,当我这样做我的位置按钮进入操作栏下。所以我不得不删除默认我的位置按钮,把我自己的。现在我也不怎么移动地图我目前的位置,当我点击新建按钮。
请帮忙。我是新来的谷歌地图。先谢谢了。
In my application I am using Google map API v2. I disabled the My Location Button using map.setMyLocationEnabled(false);
and I wanted to add new button to do the same job. How can I use another button to show my current location in the same way as My Location Button would do?
Reason:
In my application I wanted to use the action Bar overlay as in the original Maps Application. But when I do this My Location Button goes under the action bar. so i had to remove the default my Location button and put my own. now I don't how to move the map to my current location when i click new button.
Please Help. I am new to Google maps. Thanks in advance.
推荐答案
这样做我自己太多,但创造我自己的标题栏,并添加一个新的按钮。下面code可能有助于(无附加XML,您可以排序的自己)。在你的活动添加这个方法:
did this myself too, but creating my own header bar and adding a new button. The following code might help (no XML attached, you can sort that yourself). Within your activity add this method:
public GeoPoint where(ArrayList<String> coordinates) {
double lat = Double.parseDouble((String) coordinates.get(0));
double lng = Double.parseDouble((String) coordinates.get(1));
GeoPoint p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
return p;
}
然后,在OnCreate,加上这样的事情,在这里你已经有了的LocationManager等实例:
Then, in the onCreate, add something like this, where you already have locationManager etc. instantiated:
final Button phoneLocButton = (Button) findViewById(R.id.HomeButtonG);
phoneLocButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
locationManager = (LocationManager) getSystemService(context);
bestProvider = locationManager.getBestProvider(c, true);
loc = locationManager.getLastKnownLocation(bestProvider);
if (loc != null) {
coords.clear();
coords.add("" + loc.getLatitude());
coords.add("" + loc.getLongitude());
}
mc.animateTo(where(coords));
} catch (java.lang.IllegalStateException ill) {
Toast.makeText(getBaseContext(),
"Waiting for location fix...", Toast.LENGTH_SHORT)
.show();
}
}
});
希望这有助于。我有额外的code是中心的地图和plonks一针就可以了,但是这应该让你去?
Hope this helps. I have additional code that centers the map and plonks a pin on it, but this should get you going?
这篇关于如何显示在我的Android在谷歌地图位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!