在Android应用程序中如何处理双击标记 [英] how can handle double click on marker in android application

查看:122
本文介绍了在Android应用程序中如何处理双击标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在地图上显示车辆,当我点击车辆时显示信息窗口,但我需要双击车辆,然后才会进入新页面。我不知道该怎么做,请帮助我。这里是我的代码

  public class MapsActivity扩展FragmentActivity实现OnMapReadyCallback,GoogleMap.OnMarkerClickListener {

GoogleMap mMap ;
RequestQueue requestQueue;
ArrayList< LatLng> points = null;
PolylineOptions polylineOptions;
地理编码器地理编码器;
LocationListener locationListener;
TextView电视,tv_one,tv_two;
字符串字符串;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment =(SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
points = new ArrayList< LatLng>();
polylineOptions = new PolylineOptions();
String URL =http://b2ss2c.com/gtsapi/index.php?cmd=VEHICLE_DETAILS&deviceID=salim_fiat&accountID=gts&fromDate=23-May-2016&toDate=24-May-2016 ;
final JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(URL,new Response.Listener< org.json.JSONArray>(){
@Override $ b $ public void onResponse(org.json.JSONArray response){
for(int i = 0; i< response.length(); i ++){

try {
JSONObject jsonObject1 = response.getJSONObject(i);
String speed = jsonObject1.getString(speed);
String deviceID = jsonObject1.getString(deviceID);
String height = jsonObject1.getString(altitude);
String fuelLevel = jsonObject1.getString(fuelLevel);
LatLng latLng = new LatLng(jsonObject1.getDouble(latitude),jsonObject1.getDouble(longitude));
BitmapDescriptor image = BitmapDescriptorFactory.fromResource (R.drawable.car);
mMap.setOnMarkerClickListener(MapsActivity.this);
Marker marker = mMap.addMarker(new MarkerOptions()。position(latLng).icon(image));
marker.setTitle(DeviceID:+ deviceID +\\\
+Speed:+ speed +\\\
+Altitude:+ altitude +\\\
+FuelLevel:+ fuelLevel );
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,16));
points.add(latLng);
} catch(JSONException e){
e.printStackTrace();
}
}
polylineOptions.addAll(points);
polylineOptions.width(4);
polylineOptions.color(Color.BLUE);
mMap.addPolyline(polylineOptions);
}
},new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error){

}
} );
requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonArrayRequest);
}
@Override
public boolean onMarkerClick(Marker marker){
geocoder = new Geocoder(getApplicationContext(),Locale.getDefault());
if(Geocoder.isPresent()){
try {
List< Address>地址= geocoder.getFromLocation(marker.getPosition()纬度,marker.getPosition()经度,1);
if(addresses!= null&& addresses.size()> 0){
android.location.Address address = addresses.get(0);
string = String.format(%s%s%s,address.getAddressLine(0),address.getSubLocality(),address.getLocality(),
address.getAdminArea(),地址。 getCountryName());
String title = marker.getTitle();
marker.setSnippet(title +\\\
+Address:+ string);
marker.showInfoWindow();
} else {
Toast.makeText(getApplicationContext(),Address not found,Toast.LENGTH_SHORT).show();
}
} catch(IOException e){
e.printStackTrace();
}

}
return false;
}

@Override
public void onMapReady(GoogleMap googleMap){
mMap = googleMap;

mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter(){
@Override
public View getInfoWindow(Marker marker){
return null;
}

@Override
public View getInfoContents(Marker marker){
View view = getLayoutInflater()。inflate(R.layout.info_window,null);
tv =( TextView)view.findViewById(R.id.tv_address);
tv_one =(TextView)view.findViewById(R.id.tv_one);
tv_two =(TextView)view.findViewById(R.id。 tv_two);
String snippet = marker.getSnippet();
tv_one.setText(snippet);
return view;
}
});





$ b

解决方案

目前您正在使用 onMarkerClick api启动单击标记的活动。为什么不使用定时器机制,它会记录点击次数。您可以按照以下步骤操作:


  1. 记录点击时间&标记的ID。

  2. 每当您在标记上点击事件时。检查ID是否与前一个相同,并且两个事件之间的时间都小于某个阈值。

  3. 如果是,启动您的活动。

  4. 如果不是,清除保存的ID到新的ID并记录时间。 b $ b


I am displaying a vehicles on a map,when i click on vehicle it shows info window but i need double click on vehicle then it will goes to the new page.I don't know how to do,please help me.here is my code

 public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,GoogleMap.OnMarkerClickListener {

        GoogleMap mMap;
        RequestQueue requestQueue;
        ArrayList<LatLng> points = null;
        PolylineOptions polylineOptions;
        Geocoder geocoder;
        LocationListener locationListener;
        TextView tv,tv_one,tv_two;
        String string;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_maps);
            SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map);
            mapFragment.getMapAsync(this);
            points = new ArrayList<LatLng>();
            polylineOptions = new PolylineOptions();
            String URL = "http://b2ss2c.com/gtsapi/index.php?cmd=VEHICLE_DETAILS&deviceID=salim_fiat&accountID=gts&fromDate=23-May-2016&toDate=24-May-2016";
            final JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(URL, new Response.Listener<org.json.JSONArray>() {
                @Override
                public void onResponse(org.json.JSONArray response) {
                    for (int i = 0; i < response.length(); i++) {

                        try {
                            JSONObject jsonObject1 = response.getJSONObject(i);
                            String speed=jsonObject1.getString("speed");
                            String deviceID=jsonObject1.getString("deviceID");
                            String altitude=jsonObject1.getString("altitude");
                            String fuelLevel=jsonObject1.getString("fuelLevel");
                            LatLng latLng = new LatLng(jsonObject1.getDouble("latitude"),jsonObject1.getDouble("longitude"));
                            BitmapDescriptor image = BitmapDescriptorFactory.fromResource(R.drawable.car);
                            mMap.setOnMarkerClickListener(MapsActivity.this);
                            Marker marker= mMap.addMarker(new MarkerOptions().position(latLng).icon(image));
                            marker.setTitle("DeviceID:"+deviceID+"\n"+"Speed:"+speed+"\n"+"Altitude:"+altitude+"\n"+"FuelLevel:"+fuelLevel);
                            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 16));
                            points.add(latLng);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    polylineOptions.addAll(points);
                    polylineOptions.width(4);
                    polylineOptions.color(Color.BLUE);
                    mMap.addPolyline(polylineOptions);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });
            requestQueue = Volley.newRequestQueue(this);
            requestQueue.add(jsonArrayRequest);
        }
        @Override
        public boolean onMarkerClick(Marker marker) {
            geocoder=new Geocoder(getApplicationContext(), Locale.getDefault());
            if (Geocoder.isPresent()){
                try{
                    List<Address> addresses=geocoder.getFromLocation(marker.getPosition().latitude,marker.getPosition().longitude,1);
                    if (addresses!=null && addresses.size()>0) {
                        android.location.Address address = addresses.get(0);
                        string = String.format("%s %s %s", address.getAddressLine(0),address.getSubLocality(),address.getLocality(),
                                address.getAdminArea(),address.getCountryName());
                        String title=marker.getTitle();
                        marker.setSnippet(title+"\n"+"Address:"+string);
                        marker.showInfoWindow();
                    }else {
                        Toast.makeText(getApplicationContext(),"Address not found",Toast.LENGTH_SHORT).show();
                    }
                }catch (IOException e){
                    e.printStackTrace();
                }

            }
            return false;
        }

        @Override
        public void onMapReady(GoogleMap googleMap) {
            mMap = googleMap;

            mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
                @Override
                public View getInfoWindow(Marker marker) {
                    return null;
                }

                @Override
                public View getInfoContents(Marker marker) {
                    View view=getLayoutInflater().inflate(R.layout.info_window,null);
                    tv=(TextView)view.findViewById(R.id.tv_address);
                    tv_one=(TextView)view.findViewById(R.id.tv_one);
                    tv_two=(TextView)view.findViewById(R.id.tv_two);
                    String snippet= marker.getSnippet();
                    tv_one.setText(snippet);
                    return view;
                }
            });

        }


    }

解决方案

Currently you are launching your activity on single click of marker using the onMarkerClick api. In it why dont you use a timer mechanism, which will record the number of clicks. You can do it in following steps

  1. Record time on click & id of marker.
  2. Whenever you get click event on marker. Check if ID is same as previous one and also the time between both events is less than some threshold.
  3. If yes, Launch your activity.
  4. If No, clear the saved id to new one and record time as well.

这篇关于在Android应用程序中如何处理双击标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆