在android中的地图上的标记监听器

时间:2014-03-10 11:00:19

标签: android google-maps

我正在开发一个应用程序,因为我在地图中使用标记显示位置当前位置和数据库位置,现在我正在尝试将侦听器添加到当前位置标记,此处侦听器应用于当前位置但其他数据库标记也受到侦听器的影响,它在地图中点击数据库标记时引发异常,我的要求是应用于当前位置的监听器请验证我在代码中出错的地方

 public class ShowMapWhenLoginActivity extends FragmentActivity implements LocationListener, 
                                            OnMarkerClickListener{
GoogleMap _googleMap;
 LatLng myPosition;
 LocationManager locationManger;
private Marker mCustomerMarker;
private double latitude;
private double langitude;

private String TITILE = "start";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_map_when_login);

    _googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(
            R.id.mapId)).getMap(); 

    LocationManager service = (LocationManager)getSystemService(LOCATION_SERVICE);

    boolean enableGPS = service.isProviderEnabled(LocationManager.GPS_PROVIDER);
    boolean enableWiFi= service.isProviderEnabled(LocationManager.NETWORK_PROVIDER);


    /*if(!enableGPS || !enableWiFi){
        Toast.makeText(ShowMapWhenLoginActivity.this, "GPS signal not found", 
           Toast.LENGTH_LONG).show();
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(intent);
    }*/

    locationManger = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String provider = service.getBestProvider(criteria, true);


    service.requestLocationUpdates(provider, 0, 0, this);


    if(_googleMap==null){
        Toast.makeText(getApplicationContext(), "Google Map Not Available",   
               Toast.LENGTH_LONG).show();
        }

    _googleMap.setOnMarkerClickListener(this);


}
@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub

    //locationManger.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,   
              this);
    //_googleMap.clear();
    ArrayList<HashMap<String, String>> arl = (ArrayList<HashMap<String, String>>) 
            getIntent().getSerializableExtra("arrayList");


    if(location!=null){
         latitude = location.getLatitude();
         langitude = location.getLongitude();
        LatLng latlang = new LatLng(latitude, langitude);

        myPosition = new LatLng(latitude, langitude);

    }
         if(arl.size()!=0){


            for(int j = 0;j<arl.size();j++){


          String lat =arl.get(j).get("lat").toString();
          String lng =arl.get(j).get("lng").toString();
          if ( !lat.trim().equals("") && !lng.trim().equals("") ) 
          {
          double Hlat = Double.parseDouble(lat.trim());
          double Hlong= Double.parseDouble(lng.trim());

          LatLng dabaseLocations =new LatLng(Hlat, Hlong);

          getOtherLocation(dabaseLocations);
          getCurrentLocation(myPosition);
          // Show current location with database locations

             }
           }
         }
     else{
             // Show  Current Location Only

            getCurrentLocation(myPosition);
    }

}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}
/*@Override
protected void onRestart() {
    // TODO Auto-generated method stub
    super.onRestart();
    Intent i = new Intent(ShowMapWhenLoginActivity.this,ShowMapWhenLoginActivity.class);
    startActivity(i);
    finish();
}*/

private void getOtherLocation(LatLng location){
     mCustomerMarker = _googleMap.addMarker(new MarkerOptions()
     .position(location)
     .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
     .flat(true));


}
private void getCurrentLocation(LatLng location){
    _googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myPosition,5)); 
    _googleMap.addMarker(new MarkerOptions().position(location).title(TITILE));

}
@Override
public boolean onMarkerClick(Marker marker) {
    // TODO Auto-generated method stub

    if((marker.getTitle()).equals(TITILE)){

        Toast.makeText(ShowMapWhenLoginActivity.this, "clicked", 100).show();
    }
    return true;
}

}

logcat的

     03-10 16:17:47.482: E/AndroidRuntime(1395): FATAL EXCEPTION: main
     03-10 16:17:47.482: E/AndroidRuntime(1395): java.lang.NullPointerException
     03-10 16:17:47.482: E/AndroidRuntime(1395):    at    
    com.technowellServices.traffic.ShowMapWhenLoginActivity.
    onMarkerClick(ShowMapWhenLoginActivity.java:172)
    03-10 16:17:47.482: E/AndroidRuntime(1395):     at    
    com.google.android.gms.maps.GoogleMap$8.a(Unknown Source)
    03-10 16:17:47.482: E/AndroidRuntime(1395):     at c    
   om.google.android.gms.maps.internal.k$a.onTransact(Unknown Source)

     03-10 16:17:47.482: E/AndroidRuntime(1395):    at    
    android.os.Binder.transact(Binder.java:310)
    03-10 16:17:47.482: E/AndroidRuntime(1395):     at ejn.a(SourceFile:84)
    03-10 16:17:47.482: E/AndroidRuntime(1395):     at maps.e.bl.a(Unknown Source)
    03-10 16:17:47.482: E/AndroidRuntime(1395):     at maps.ay.c.a(Unknown Source)
    03-10 16:17:47.482: E/AndroidRuntime(1395):     at maps.ay.n.c(Unknown Source)
    03-10 16:17:47.482: E/AndroidRuntime(1395):     at maps.ay.m.a(Unknown Source)
   03-10 16:17:47.482: E/AndroidRuntime(1395):  at maps.ay.bg.b(Unknown Source)
   03-10 16:17:47.482: E/AndroidRuntime(1395):  at maps.ay.bc.onSingleTapConfirmed(Unknown Source)
    03-10 16:17:47.482: E/AndroidRuntime(1395):     at maps.bo.g.onSingleTapConfirmed(Unknown     
   Source)
    03-10 16:17:47.482: E/AndroidRuntime(1395):     at maps.bo.i.handleMessage(Unknown Source)
   03-10 16:17:47.482: E/AndroidRuntime(1395):  at        
   android.os.Handler.dispatchMessage(Handler.java:130)

    03-10 16:17:47.482: E/AndroidRuntime(1395):     at android.os.Looper.loop(SourceFile:351)
    03-10 16:17:47.482: E/AndroidRuntime(1395):     at   
    android.app.ActivityThread.main(ActivityThread.java:3814)

    03-10 16:17:47.482: E/AndroidRuntime(1395):     at     
    java.lang.reflect.Method.invokeNative(Native Method)

    03-10 16:17:47.482: E/AndroidRuntime(1395):     at 
    java.lang.reflect.Method.invoke(Method.java:538)
    03-10 16:17:47.482: E/AndroidRuntime(1395):     at    
     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
     03-10 16:17:47.482: E/AndroidRuntime(1395):    at    
     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:659)
    03-10 16:17:47.482: E/AndroidRuntime(1395):     at dalvik.system.NativeStart.main(Native     
     Method)

1 个答案:

答案 0 :(得分:0)

试试如下:

@Override
public boolean onMarkerClick(Marker marker) {
    // TODO Auto-generated method stub

    if((marker.equals(mCustomerMarker)){

        Toast.makeText(ShowMapWhenLoginActivity.this, "clicked", 100).show();
    }
    return false;
}