问题描述
目前做一个简单的应用程序,包括谷歌地图。我想,当一个设计用户点击坐标链接到下一个活动,但它不工作,没有错误,请帮我
双纬度= 1.34503109; 双经度= 103.94008398; 经纬度的latLng =新的经纬度(纬度,经度); gMap.setOnMapClickListener(新GoogleMap.OnMapClickListener(){
@覆盖
公共无效onMapClick(经纬度的latLng){
意图I =新意图(Activity_Selecting_Bus.this,
Activity_Bus_Selected.class);
startActivity(ⅰ); }
});
如果我理解正确的话,你在你的地图设置了标记,当用户点击一个标记,你开始另一项活动。下面code,就可以(在 SupportMapFragment
)
的GetMap()。setOnMarkerClickListener(新GoogleMap.OnMarkerClickListener(){
@覆盖
公共布尔onMarkerClick(标记标记){
//做的事!
返回true;
}
});
如果您还没有标记,想监听某个位置点击使用这个代替:
的GetMap()。setOnMapClickListener(新GoogleMap.OnMapClickListener(){
@覆盖
公共无效onMapClick(经纬度的latLng){
//做的事!
}
});
在这种情况下,你可能想开始活动,当用户点击足够接近到了一定的位置。我使用这个库 http://googlemaps.github.io/android-maps-utils/一>其中含有方法
SphericalUtil.computeDistanceBetween(经纬度从,经纬度为)
这米两个经纬度
秒之间返回距离。
修改示例:
首先定义用户具有点击和活动不特定的点击启动:
私有静态最后的HashMap<经纬度,类&LT ;?扩展活动>> sTargets =新的HashMap();静态的 {
sTargets.put(新经纬度(34.0204989,-118.4117325),LosAngelesActivity.class);
sTargets.put(新经纬度(42.755942,-75.8092041),NewYorkActivity.class);
sTargets.put(新经纬度(42.352711,-83.099205),DetroitActivity.class);
}私有静态最终诠释RANGE_METERS = 200 * 1000; //200公里范围
然后,当用户点击在地图上,你计算距离,每个点。如果适合,你开始练习。
的GetMap()。setOnMapClickListener(新GoogleMap.OnMapClickListener(){
@覆盖
公共无效onMapClick(输入的LatLng){
对于(Map.Entry的<经纬度,&LT ;?延伸活性GT;>项:sTargets.entrySet()){
经纬度LL = entry.getKey();
布尔INRANGE = SphericalUtil.computeDistanceBetween(输入,LL)与LT; RANGE_METERS;
如果(INRANGE){
类&LT ;?扩展活动> CLS = entry.getValue();
意图I =新意图(getActivity(),CLS);
。getActivity()startActivity(ⅰ);
打破; //停止第一个结果后计算
}
}
}
});
Currently doing a simple app that contain google map. and i would like to link to next activity when the user click on a designed coordinate but it is not working and there is no error, please help me
double latitude = 1.34503109;
double longitude = 103.94008398;
LatLng latLng = new LatLng(latitude, longitude);
gMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
Intent i = new Intent(Activity_Selecting_Bus.this,
Activity_Bus_Selected.class);
startActivity(i);
}
});
If I understand correctly you have markers set up in your map and when user clicks on a marker you start another activity. The following code should work (in SupportMapFragment
):
getMap().setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
// do the thing!
return true;
}
});
If you don't have markers and want to listen for a certain location click use this instead:
getMap().setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
// do the thing!
}
});
In this case you probably want to start the activity when the user clicks "close enough" to a certain location. I use this library http://googlemaps.github.io/android-maps-utils/ which contains method
SphericalUtil.computeDistanceBetween(LatLng from, LatLng to)
which returns distance between two LatLng
s in meters.
EDIT Example:
First you define where the user has to click and which activity does that particular click launch:
private static final HashMap<LatLng, Class<? extends Activity>> sTargets = new HashMap();
static {
sTargets.put(new LatLng(34.0204989,-118.4117325), LosAngelesActivity.class);
sTargets.put(new LatLng(42.755942,-75.8092041), NewYorkActivity.class);
sTargets.put(new LatLng(42.352711,-83.099205), DetroitActivity.class);
}
private static final int RANGE_METERS = 200 * 1000; // 200 km range
Then when the user clicks on the map, you compute distance to each point. If it fits, you launch the activity.
getMap().setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng input) {
for(Map.Entry<LatLng,<? extends Activity>> entry : sTargets.entrySet()) {
LatLng ll = entry.getKey();
boolean inRange = SphericalUtil.computeDistanceBetween(input, ll) < RANGE_METERS;
if (inRange) {
Class<? extends Activity> cls = entry.getValue();
Intent i = new Intent(getActivity(), cls);
getActivity().startActivity(i);
break; // stop calculating after first result
}
}
}
});
这篇关于谷歌Android地图坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!